在GitLab持续集成与持续交付(CI/CD)中注入application.properties的值

huangapple go评论81阅读模式
英文:

Inject application.properties value in GitLab CICD

问题

目前我的技术栈是Java和Spring Boot。

我正在使用application-dev.properties文件来存储AWS访问密钥和秘密密钥。

application-dev.properties文件中,我有以下内容来注入这些密钥:

#这个属性提供访问密钥详情
com.abc.sqs.accesskey = AWS_ACCESS_KEY
#这个属性提供秘密密钥详情
com.abc.sqs.secretkey = AWS_SECRET_KEY

现在在GitLab的CICD配置文件.gitlab-ci.yml中,当我尝试对应用程序的.jar文件进行冒烟测试时,我有如下内容(阶段是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

在这里,我从GitLab的CICD环境变量中获取了$AWS_ACCESS_KEY_ID$AWS_SECRET_ACCESS_KEY,并尝试替换属性文件中的AWS_ACCESS_KEYAWS_SECRET_KEY。但是这种方式在服务器启动时无法注入。

在尝试测试.jar文件时,出现了以下异常:

Caused by: com.amazonaws.services.sqs.model.AmazonSQSException: 请求中包含的安全令牌无效。(服务:AmazonSQS;状态码:403;错误代码:InvalidClientTokenId;

请您提供建议,提前谢谢。

英文:

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.

答案1

得分: 9

如果您想覆盖属性文件中的属性,而不是使用sed,您可以简单地声明一个与属性名称类似的环境变量或JVM变量。它将具有比文件中声明的属性更高的优先级。
例如:

com.abc.sqs.accesskey = AWS_ACCESS_KEY

可以通过JVM变量变为:

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

这将覆盖属性文件的值,并在应用程序启动时可用。

英文:

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.

huangapple
  • 本文由 发表于 2020年7月27日 19:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63114703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定