【问题标题】:How to read Gitlab (.gitlab-ci.yml ) environment variable from Spring Boot code?如何从 Spring Boot 代码中读取 Gitlab (.gitlab-ci.yml) 环境变量?
【发布时间】:2021-01-28 11:48:36
【问题描述】:

我正在尝试在构建应用程序时从 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

 }
}

我做错了吗?我将不胜感激。

【问题讨论】:

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


    【解决方案1】:

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

    sf:
     apiKey: ${SF_API_KEY}
    

    看看docs,这个占位符符号的详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2018-04-12
      • 2017-11-30
      • 1970-01-01
      • 2023-02-22
      • 2020-02-14
      相关资源
      最近更新 更多