首页 > 解决方案 > 带有 maven 的 gitlab CI/CD 不会在 application.properties 中设置环境变量

问题描述

我正在尝试使用 maven 构建 CI/CD 管道。我遇到的问题是在 application.properties 我设置了这样的变量:

database.MongoPass=${MONGO_PASS}
database.Secret=${SECRET}
database.connectionString=${ATLAS_STRING}
spring.data.mongodb.uri=${ATLAS_STRING}

而且我无法将它们设置为 gitlab。每次如果 gitlab 将一直构建包我无法运行它,因为连接字符串错误我得到错误: “连接字符串无效。连接字符串必须以 'mongodb://' 或 'mongodb+srv://' 开头”

这里是我在 gitlab CI/CD 设置中设置的变量示例

变量示例

在这里我尝试在 gitlab CI/CD echo 中运行的代码可以正确运行并显示正确的变量值我尝试的每个 mvn 脚本都不起作用

 script:
    - echo $SECRET
    - echo $MONGO_PASS
    - echo $ATLAS_STRING
    - mvn install -B #  (I hope that application properties automatically get variables from gitlab env) 
    - mvn -DSECRET=$SECRET -DMONGO_PASS=$MONGO_PASS -DATLAS_STRING=$ATLAS_STRING clean install -B #  (I found this solution on stack) 
    - mvn -Dspring-boot.run.arguments=--database.Secret=$SECRET,--database.MongoPass=$MONGO_PASS,--spring.data.mongodb.uri=$ATLAS_STRING clean install -B #  (if I change here env variables for normal string it wont't build on gitlab) 

我不知道我应该做什么,我不想在我的仓库中保存变量,也不知道该怎么做。有人可以给我建议吗?mvn 脚本在每次运行后在工件中构建 jar 文件我下载它并运行以使用命令对其进行测试

java -jar filename.jar

更新: 我做了小调查,并在春季启动后进行了测试变量的类:

  @PostConstruct
    public void test() {
        log.info("VARIABLES TEST");
        log.info("properties.getSecret(): {}", properties.getSecret());
        log.info("properties.getConnectionString(): {}", properties.getConnectionString());
        log.info("properties.getMongoPass(): {}", properties.getMongoPass());
    }

并且变量都没有设置:

properties.getSecret(): ${SECRET}
properties.getConnectionString(): ${ATLAS_STRING}
properties.getMongoPass(): ${MONGO_PASS}

gitlab-ci.yml:

image: maven:3.8.1-jdk-11

build_artifact:
  stage: build
  script:
    - export
#    - mvn install -B -P no-tests
    - mvn -DSECRET=$SECRET -DMONGO_PASS=$MONGO_PASS -DATLAS_STRING=$ATLAS_STRING clean install -B -P no-tests #  (I found this solution on stack)
#    - mvn -Dspring-boot.run.arguments=--database.Secret=$SECRET,--database.MongoPass=$MONGO_PASS,--spring.data.mongodb.uri=$ATLAS_STRING clean install -B -P no-tests #  (if I change here env variables for normal string it wont't build on gitlab)
  artifacts:
    paths:
      - target/*.jar
    expire_in: 10 minutes

示例管道结果:

Running with gitlab-runner 14.4.0-rc1 (bc99a056)
  on docker-auto-scale ed2dce3a
Preparing the "docker+machine" executor
00:23
Using Docker executor with image maven:3.8.1-jdk-11 ...
Pulling docker image maven:3.8.1-jdk-11 ...
Using docker image sha256:5b508b1fe19e290255c9e077a1c7af028a576cabb70eab4abdfee574599f729f for maven:3.8.1-jdk-11 with digest maven@sha256:aaf506d47cd2ec8f62fc1ff74065eda5614738e8ea61bad9b32da0360b9498cd ...
Preparing environment
00:01
Running on runner-ed2dce3a-project-16772800-concurrent-0 via runner-ed2dce3a-srm-1634103033-dfd4e8e6...
Getting source from Git repository
00:03
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/**/***/.git/
Created fresh repository.
Checking out 60bf3869 as feature/branch
Skipping Git submodules setup
Executing "step_script" stage of the job script
$ mvn -DSECRET=$SECRET -DMONGO_PASS=$MONGO_PASS -DATLAS_STRING=$ATLAS_STRING clean install -B -P no-tests
***
Downloading all dependencies 
***
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:00 min
[INFO] Finished at: 2021-10-13T05:34:25Z
[INFO] ------------------------------------------------------------------------
Uploading artifacts for successful job
00:07
Uploading artifacts...
target/*.jar: found 1 matching files and directories 
Uploading artifacts as "archive" to coordinator... ok  id=1674250996 responseStatus=201 Created token=z2qnoeL8
Cleaning up project directory and file based variables
00:00
Job succeeded


标签: javamavengitlabcicd

解决方案


有根据的猜测:您尚未为 application.properties 属性文件启用Maven 过滤

如果没有过滤,这些占位符将不会被替换。

所以在你的 pom 文件中有这样的东西:

<project>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

推荐阅读