Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to read an environment variable is being set from within Gitlab configuration when the application is being built, I am doing this for achieving that purpose:

I setup a variable in the application.properties.yml of my spring boot app:

sf:
 apiKey: $SF_API_KEY

In the .gitlab-ci.yml, I defined the variable to be set, as follows:

variables:
  SF_API_KEY: $SF_API_KEY

all I want, is to be able to read that variable from within one of my services, as the below code depicts:

@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

 }
}

Am I doing something wrong? I would appreciate any help.

like image 854
D.B Avatar asked May 29 '26 21:05

D.B


1 Answers

Try (note the {} around SF_API_KEY):

sf:
 apiKey: ${SF_API_KEY}

Take a look at the docs where this placeholder notation is detailed.

like image 162
Adam Arold Avatar answered Jun 01 '26 11:06

Adam Arold