简体   繁体   中英

Inject application.properties value in GitLab CICD

Currently my tech stack is Java, Spring Boot.

I am using application-dev.properties to keep the AWS access key and secret key.

In application-dev.properties to inject the keys I have:

#This property provide access key details
com.abc.sqs.accesskey = AWS_ACCESS_KEY
#This property provide secret key details
com.abc.sqs.secretkey = AWS_SECRET_KEY

Now from GitLab CICD.gitlab-ci.yml file while I am trying to smoke test of the application.jar I have something like this (stage is smoke test) -

smoke test:
  stage: smoke-test
  image: openjdk:12-alpine
  before_script:
    - apk add --update curl && rm -rf /var/cache/apk/*
  script:
    - ls -la ./app-service/target/
    - sed -i "s/AWS_ACCESS_KEY/$AWS_ACCESS_KEY_ID/" ./app-service/src/main/resources/application-dev.properties
    - sed -i "s/AWS_SECRET_KEY/$AWS_SECRET_ACCESS_KEY/" ./app-service/src/main/resources/application-dev.properties
    - java -jar -Dspring.profiles.active=dev ./app-service/target/app-service.jar &
    - sleep 30
    - curl http://localhost:5000/actuator/health | grep "UP"
    - curl -i -X POST http://localhost:5000/actuator/shutdown

Here I am bringing $AWS_ACCESS_KEY_ID and $AWS_SECRET_ACCESS_KEY from GitLab CICD environment variables and trying to replace AWS_ACCESS_KEY and AWS_SECRET_KEY of properties file. But this way I am not able to inject during start of the server.

While trying to test the jar getting following exception:

Caused by: com.amazonaws.services.sqs.model.AmazonSQSException: The security token included in the request is invalid. (Service: AmazonSQS; Status Code: 403; Error Code: InvalidClientTokenId;

Please need your suggestion. Advance thanks.

If you want to override properties in a properties file, instead of using sed , you can simply declare an environment variable or a JVM variable with a similar name. It will have the priority over properties declare in file. For instance:

com.abc.sqs.accesskey = AWS_ACCESS_KEY

Can become with a JVM variable:

java -jar -Dspring.profiles.active=dev -Dcom.abc.sqs.accesskey=$AWS_ACCESS_KEY_ID ./app-service/target/app-service.jar

This will override the value of the properties file, and this will be available on application startup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM