如果 gitlab 管道中的条件为真,则运行依赖项作业

时间:2021-01-04 14:59:31

标签: github gitlab devops gitlab-ci

如果条件为真,我想运行依赖作业。我们在 git lab 中是否有这种可行性。当我使用 DEPLOY 变量手动触发测试作业时,依赖项应该运行,否则跳过依赖项。我不想在构建阶段保持状态。

build:
 stage: build
 when: manual
 script:
   - echo build
test:
  stage: test
  when: manual
  dependencies:
    - build
    if [ $deploy = 'true' ]
  script: 
   - echo test

1 个答案:

答案 0 :(得分:2)

Gitlab 文档是一个很好的起点,尤其是 rules section

<块引用>

rules 关键字可用于包含或排除管道中的作业。

规则按顺序评估,直到第一次匹配。匹配时, 作业是包含在管道中还是从管道中排除,具体取决于 配置。如果包含,则该作业还添加了某些属性 到它。

这意味着您可以为这种逻辑参与使用规则,在您的情况下,它看起来像

build:
 stage: build
 when: manual
 script:
   - echo build
test:
  stage: test
  needs: ['build'] # dependency on previous build stage
  script: 
   - echo test
  rules:
   - if: '$deploy == "true"' # when true, than run automatically
   - if: '$deploy != "true"' # when not true, than run only manually
     when: manual

我不确定第二条规则是否需要。但我强烈建议您查看 GitLab 文档中的以下指令: