我希望能够通过读取pom.xml文件来设置一些变量。这些变量需要是全局的,因为它们将在多个阶段和任务中被使用。
根据GitLab-CI文档,有两种不同的方法可以设置全局变量:
1.使用变量语句:
根据GitLab-CI文档,有两种不同的方法可以设置全局变量:
1.使用变量语句:
variable:
pom_artifactID: $(grep -m1 '<artifactId>' pom.xml | cut -d '<' -f2 |cut -d '>' -f2)
使用“before”脚本:
before_script:
- pom_artifactID=$(grep -m1 '<artifactId>' pom.xml | cut -d '<' -f2 |cut -d '>' -f2)
- pom_artifactVersion=$(grep -m1 '<version>' pom.xml | cut -d '<' -f2 |cut -d '>' -f2)
- pom_packaging=$(grep -m1 '<packaging>' pom.xml | cut -d '<' -f2 |cut -d '>' -f2)
- pom_finalName=$({ grep -m1 '<finalName>' pom.xml | cut -d '<' -f2 | cut -d '>' -f2; [ ${PIPESTATUS[0]} -eq 0 ] && true || echo ${pom_artifactID}-${pom_artifactVersion}.$pom_packaging}; })
第一种方法无法工作,因为gitlab-ci不会评估$(command),所以pom_artifactID
成为字面意思"$(grep -m1 '' pom.xml | cut -d '<' -f2 |cut -d '>' -f2)"。
第二种方法也无法工作,因为"before_script"依赖于"grep"命令,而我流程中使用的一些docker镜像具有旧版本的grep。
还有其他方法来设置全局变量或在阶段和作业之间传递变量吗?
pom.xml
中提取值也可以使用_mvn_命令本身来实现,只需借助_maven-help-plugin_插件的一点帮助,即APP_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
。 - undefined