运行 任务的条件依赖

condition to run task with dependence

在gitlab ci管道中,我知道如何设置阶段之间的依赖关系。

deploy:
  stage: deploy
  needs: [iac]
  ...

我也知道设置过滤器,

iac:
  stage: iac
  only:
    changes:
      - iac/*
  ...

但是如何将依赖和过滤器一起设置。条件是:

阶段iac:如果文件夹iac下的任何文件发生变化,需要运行这个阶段,如果没有变化,跳过

阶段deploy:取决于阶段iac

但是当阶段iac被跳过时(没有变化),流水线将会混乱。

'deploy' job needs 'iac' job, but 'iac' is not in any previous stage

您的 deploy 阶段每次都在寻找 iac 阶段,您需要对这两个阶段应用相同的规则。您可以使用 rules keywords because :

only and except are not being actively developed. rules is the preferred keyword to control when to add jobs to pipelines.

因此您可以定义一个全局规则并将其扩展到您要过滤的职位:

.rules:iac-changes:
  rules:
    - changes:
      - iac/*

deploy:
  stage: deploy
  extends:
    - .rules:iac-changes
  needs: [iac]
  ...

iac:
  stage: iac
  extends:
    - .rules:iac-changes
  ...

如果你想 运行 deploy 舞台,你可以 needs optional.