spring-boot - 如何从 Spring Boot 代码中读取 Gitlab (.gitlab-ci.yml) 环境变量?

标签 spring-boot kotlin gitlab environment-variables gitlab-ci

我正在尝试读取正在构建应用程序时从 Gitlab 配置中设置的环境变量,我这样做是为了实现该目的:

我在 spring boot 应用程序的 application.properties.yml 中设置了一个变量:

sf:
 apiKey: $SF_API_KEY

在.gitlab-ci.yml中,我定义了要设置的变量,如下:

variables:
  SF_API_KEY: $SF_API_KEY

我想要的只是能够从我的一项服务中读取该变量,如下面的代码所示:

@Service
class MyService(@Value("\${sf.apiKey}") val apiKey: String)
{

 fun doSomething(){

 //i am seeing the variable is being set by gitlab in the build logs but 
 // it is not being read here properly 
 var result = apiKey;

 logger.info { "***check apiKey:  $apiKey" }
  //This line lgs $SF_API_KEY as a value of my variable, but not the 
  // real value

 }
}

我做错了什么吗?如果有任何帮助,我将不胜感激。

最佳答案

尝试(注意 SF_API_KEY 周围的 {}):

sf:
 apiKey: ${SF_API_KEY}

看看 docs此占位符符号详细说明的位置。

关于spring-boot - 如何从 Spring Boot 代码中读取 Gitlab (.gitlab-ci.yml) 环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64347080/

相关文章:

spring - 如何在 Spring Boot 中从 [] 读取 List<String> 属性

spring - 在 Spring Boot 管理员中未经授权

collections - 将 int 列表折叠为 kotlin 中的范围列表

android - 合成 Material 3 中的分隔符

java - 是否可以在 Spring Boot 中的两个方法之间传递模型值?

java - 在 Thymeleaf 中使用 DateFormat.SHORT

java-8 - java8 和 Kotlin 的错误

docker - GitLab Runner Docker Executor 中的缓存层 - Long Time DinD Container

javascript - Gitlab 使用 Axios 或 Curl 请求发布/放入 Node - 无法写入资源 - 400 错误

git - gitlab CI : files are not updated on FTP server even if they are changed in the last commit 中的 LFTP