GitLab CI从UI触发带变量流水线时未跳过指定步骤
GitLab流水线意外执行任务的原因与解决方法
问题场景
我有如下GitLab流水线配置:
stages: - argocd-delete - argocd-update update-custom-argocd-with-new-group: stage: argocd-update image: alpine:latest before_script: ... script: ... rules: - changes: - manifests/customer-resources/* - when: never delete-users-from-argocd: stage: argocd-delete image: alpine:latest variables: CUSTOMER_NAME: "" GROUP_NAME: "" before_script: ... script: ... rules: - if: '$CUSTOMER_NAME!="" && $GROUP_NAME!=""' when: always - when: never
提交代码时,若manifests/customer-resources/目录下有变更,update-custom-argocd-with-new-group任务会执行,无变更则跳过,符合预期。但从GitLab界面传入CUSTOMER_NAME和GROUP_NAME变量触发流水线时,本应只执行delete-users-from-argocd任务,却意外执行了另一个任务。
原因分析
手动触发流水线时,changes规则的判断逻辑是基于触发时所选分支的最新提交与上一次流水线运行时的提交之间的文件变更。如果这两次提交之间manifests/customer-resources/目录下存在变更,即使是手动传参触发,update-custom-argocd-with-new-group的changes规则也会被满足,导致任务执行。
简言之:手动传参触发不会忽略changes规则,只要分支历史中有对应目录的变更,任务就会启动。
解决方法
修改update-custom-argocd-with-new-group的rules,增加一个前置判断:当传入了CUSTOMER_NAME和GROUP_NAME变量时,直接跳过该任务。
修改后的完整配置:
stages: - argocd-delete - argocd-update update-custom-argocd-with-new-group: stage: argocd-update image: alpine:latest before_script: ... script: ... rules: - if: '$CUSTOMER_NAME!="" && $GROUP_NAME!=""' when: never # 手动传参触发时直接跳过 - changes: - manifests/customer-resources/* - when: never delete-users-from-argocd: stage: argocd-delete image: alpine:latest variables: CUSTOMER_NAME: "" GROUP_NAME: "" before_script: ... script: ... rules: - if: '$CUSTOMER_NAME!="" && $GROUP_NAME!=""' when: always - when: never
修改后效果:
- 代码提交触发时,仍按
changes规则判断是否执行update-custom-argocd-with-new-group; - 手动传入
CUSTOMER_NAME和GROUP_NAME触发时,第一条规则匹配,直接跳过该任务,仅执行delete-users-from-argocd。
内容的提问来源于stack exchange,提问作者MStikh
相关产品推荐
相关产品推荐

