默认将所有 GitLab CI 作业设为手动

问题描述 投票:0回答:1

是否可以在 GitLab CI 管道手册中制作所有作业,而无需向所有作业添加

rules: when: manual
?我基本上希望所有作业都有触发它们的特定规则,但在任何情况下都能够根据需要手动运行它们。

我理解使用

workflow: rules:
指定一般规则的可能性,但是,据我了解,这些规则适用于整个管道,而我想在作业级别这样做,以便我可以手动触发任何未在管道中运行的作业。

如果可能有帮助,在我的管道定义中,我有:

workflow:
  rules:
    - if: $CI_COMMIT_TAG # Tagged commit
      variables:
        ...
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH # Commit on main
      variables:
        ...
    - if: $CI_COMMIT_REF_PROTECTED == "true" # Commit on protected branch
      variables:
        ...
    - if: $CI_PIPELINE_SOURCE == "push"
      when: never # Deactivate on-commit pipelines
    - when: always # Catch events like scheduled pipelines, merge request pipelines

我使用的是 GitLab 17.5。


向所有作业添加

when: manual
规则是不可行的,因为某些作业(有多个作业取决于它们)使用
!reference
标签来导入规则:

job1:
  rules:
    - changes:
      - somedir/**/*
    - when: manual

job2:
  rules:
    - changes:
      - *.txt
    - when: manual

job3:
  rules:
    - !reference [job1, rules]
    - !reference [job2, rules] # <-- will never be reached, because job1's `when: manual` prevents it
gitlab-ci gitlab-ci.yml
1个回答
0
投票

唯一可以默认继承的关键字是

default
关键字,它与
rules
不兼容。但该示例可以用其他格式编写来实现此目的。

解决方案 1:使用 YAML 特定功能,例如

anchors (&)
aliases (*)
map merging (<<)

job1:
  script:
    - echo "job 1"
  rules:
    - &rule_a
      changes:
        - somedir/**/*
    - when: manual

job2:
  script:
    - echo "job 2"
  rules:
    - &rule_b
      changes:
        - '*.txt'
    - when: manual

job3:
  script:
    - echo "job 3"
  rules:
    - *rule_a
    - *rule_b
    - when: manual

解决方案 2:使用

!reference

.common-rules:
  when-manual:
    - when: manual
.change-list-a:
  - somedir/**/*
.change-list-b:
  - '*.txt'

job1:
  script:
    - echo "job 1"
  rules:
    - changes: !reference [.change-list-a]
    - !reference [.common-rules, when-manual]

job2:
  script:
    - echo "job 2"
  rules:
    - changes: !reference [.change-list-b]
    - !reference [.common-rules, when-manual]

job3:
  script:
    - echo "job 3"
  rules:
    - changes: !reference [.change-list-a]
    - changes: !reference [.change-list-b]
    - !reference [.common-rules, when-manual]
© www.soinside.com 2019 - 2024. All rights reserved.