从一项作业触发管道时如何传递 CICD 变量?
我有一项工作需要触发另一项工作。 触发管道的作业是这样的
deploy.with-modelmesh-crd-upgrades:
image: my-image
environment: $GCP_PROJECT
stage: deploy
when: manual
variables:
ZONE: europe-west1-b
script:
- !reference [ .deploy_auth, script ]
- gcloud container clusters get-credentials ewx-k8s --zone $ZONE --project ${CI_ENVIRONMENT_NAME}
# Triggering deploy.uninstall-ml-serving-helm-release-and-deploy
- |
response_body=$(mktemp) # Create a temporary file and store its name in $response_body
response_code=$(curl --request POST \
--fail \
--form token=$TRIGGER_TOKEN \
--form ref=$CI_COMMIT_REF_NAME \
--form "variables[TRIGGERED_FROM]=deploy.with-modelmesh-crd-upgrades" \
--write-out "%{http_code}" \
--verbose \
--output "$response_body" \
"https://gitlab.com/api/v4/projects/47198141/trigger/pipeline")
echo "Checking the response:"
cat $response_body
echo $TRIGGERED_FROM
if [[ "$response_code" -ne 201 ]]; then
cat $response_body
echo "Failed to trigger deploy.uninstall-ml-serving-helm-release-and-deploy job with response code: $response_code."
rm $response_body
exit 1
fi
rm $response_body
如果我们基本上检查我的curl命令是
curl --request POST \
--fail \
--form token=$TRIGGER_TOKEN \
--form ref=$CI_COMMIT_REF_NAME \
--form "variables[TRIGGERED_FROM]=deploy.with-modelmesh-crd-upgrades" \
--write-out "%{http_code}" \
--verbose \
--output "$response_body" \
"https://gitlab.com/api/v4/projects/project-id/trigger/pipeline"
这就是我想要从上面触发的工作。我有以下配置:
deploy.uninstall-ml-serving-helm-release-and-deploy:
# only for modelmesh module
image:
name: hashicorp/terraform:1.5.5
entrypoint: [ "" ]
environment: $GCP_PROJECT
stage: deploy
script:
- echo "TRIGGERED_FROM = $TRIGGERED_FROM"
- env
- export GOOGLE_CREDENTIALS=$(base64 -d ${GCP_SA_KEY_B64})
- cd terraform
- terraform init
- terraform workspace select ${CI_ENVIRONMENT_NAME} || terraform workspace new ${CI_ENVIRONMENT_NAME}
# Destroying resources
- |
destroy_cmd="terraform destroy -input=false -auto-approve --var-file=env/${CI_ENVIRONMENT_NAME}.tfvars --var=ml_deployer_image_tag=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHA::7} --var=ray_image_tag=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHA::7}"
for target in \
"module.modelmesh[0].helm_release.modelmesh" \
"google_compute_disk.modelmesh-disc" \
do
destroy_cmd="$destroy_cmd -target=\"$target\""
done
eval $destroy_cmd
# Apply changes
- terraform apply -input=false -auto-approve --var-file=env/${CI_ENVIRONMENT_NAME}.tfvars --var=ml_deployer_image_tag=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHA::7} --var=ray_image_tag=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHA::7}
rules:
- if: '$TRIGGERED_FROM == "deploy.with-modelmesh-crd-upgrades"'
when: always
- when: manual
请注意,这里的想法是传递正在设置的
TRIGGERED_FROM
变量
deploy.with-modelmesh-crd-upgrades
作业,在curl命令运行时,并在deploy.uninstall-ml-serving-helm-release-and-deploy
作业上请求该变量,因为我有这个定义的规则:
rules:
- if: '$TRIGGERED_FROM == "deploy.with-modelmesh-crd-upgrades"'
when: always
- when: manual
问题是,当我在curl命令中声明
TRIGGERED_FROM
环境变量时,它没有被设置,在deploy.with-modelmesh-crd-upgrades
作业中,执行该命令时似乎存在问题。
我还尝试简化curl命令来做到这一点:
- 'curl --fail --request POST --form token=$TRIGGER_TOKEN --form ref=$CI_COMMIT_REF_NAME --form "variables[TRIGGERED_FROM]=deploy.with-modelmesh-crd-upgrades" "https://gitlab.com/api/v4/projects/47198141/trigger/pipeline"'
但不幸的是,
TRIGGERED_FROM
环境变量根本没有被设置。
检查文档,看来我的两种curl命令方法似乎与这里所说的方式一致:https://docs.gitlab.com/ee/ci/triggers/index.html#pass-cicd-variables -在 API 调用中
可能出了什么问题?为什么
TRIGGERED_FROM
环境变量根本没有被设置。?
我遇到了同样的问题,解决方案是在curl命令中=号后面的变量周围加上引号,如下所示:
--from variables[TRIGGERED_FROM]="deploy.with-modelmesh-crd-upgrades"
很烦人,但这是使其发挥作用的唯一方法。