【发布时间】:2020-06-25 13:39:33
【问题描述】:
我想通过从 pom.xml 文件中获取值来设置一些变量。这些变量需要是全局变量,因为它们将在多个阶段和作业中使用。
根据 gitlab-ci 文档,我可以通过两种不同的方式设置全局变量:
-
使用变量语句:
variable: pom_artifactID: $(grep -m1 '<artifactId>' pom.xml | cut -d '<' -f2 |cut -d '>' -f2) -
使用“之前”脚本:
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)"
第二个也不起作用,因为“before_script”依赖于“grep”命令,并且我的管道中使用的一些 docker 映像具有旧版本的 grep。
还有另一种方法来设置全局变量或在阶段和作业之间传递变量吗?
【问题讨论】:
标签: global-variables gitlab-ci