簡體   English   中英

如何從 Spring Boot 代碼中讀取 Gitlab (.gitlab-ci.yml ) 環境變量?

[英]How to read Gitlab (.gitlab-ci.yml ) environment variable from Spring Boot code?

我正在嘗試在構建應用程序時從 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}

查看詳細介紹此占位符符號的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM