带有 maven 的 gitlab CI/CD 没有在 application.properties 中设置环境变量

gitlab CI/CD with maven doesn't setup environment variables in 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 settings

中设置的变量示例

这里是我在 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

更新: 我做了一些小调查,并在 spring 启动后 class 测试变量:

  @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


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

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

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

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

尝试以下步骤并确保每个步骤都正常工作:

  1. 检查您的分支是否受到保护。如果它不受保护,您将无法访问定义的环境变量。

  2. 使用export命令检查环境变量是否设置正确:

    script:
        - export
        # - <other commands>
    
  3. 在您的 POM 文件中将过滤设置为 true。

  4. 将application.properties更改为以下内容:

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