我试图将变量传递给并行/矩阵,但没有看到变量被扩展并且作业失败。这是通过环境变量在作业中设置的。我试图回显脚本中的变量并查看它显示正确的值,但不会在并行/矩阵中被替换。我错过了什么吗?
.common_deploy:
script:
- |
echo "showing the regions from environment"
echo $qa_regions
echo "showing the regions from job variable"
echo $REGIONS
parallel:
matrix:
- REGION: "${REGIONS}"
DeployToQA:
variables:
ENVIRONMENT: qa
REGIONS: $qa_regions
extends:
- .common_deploy
stage: deploy
rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
allow_failure: true
这里变量
$qa_regions
的值为“us-west-2,us-east-1”,我本来希望看到这些地区的2个工作,但我看到的工作是DeployToQA: [${REGIONS}]
正如@danielnelz 已经回答的那样,当前不支持变量扩展。
但是,对于大量并行作业,您可以使用父子管道来动态生成管道。
为了有效地解决将变量传递到并行:矩阵的需要,我配置了一个解决方案。我的方法涉及利用 envsubst、模板文件和使用工件的触发器。
# .gitlab-ci.yml
stages:
- trigger
variables:
PROJECTS: '["project1", "project2", "project3"]'
process:template:
stage: trigger
before_script:
- apt-get update
- apt-get install gettext-base
script:
- envsubst '${PROJECTS}' < template.yml > template.gitlab-ci.yml
artifacts:
paths:
- template.gitlab-ci.yml
trigger:template:
stage: trigger
trigger:
include:
- artifact: template.gitlab-ci.yml
job: process:template
forward:
pipeline_variables: true
needs:
- job: process:template
您的存储库中应该有
template.yml
文件:
# template.yml
stages:
- matrix
deploy:
stage: matrix
parallel:
matrix:
- PROJECT: ${PROJECTS}
script:
- echo "Deploying project = ${PROJECT}"
在矩阵每个作业的输出中,我们可以看到来自 echo 的消息:
deploy: [project1]
“部署项目=项目1”deploy: [project2]
“部署项目=项目2”deploy: [project3]
“部署项目=项目3”希望这有帮助!🥷