Skip to content

Add support for `when: never` on conditional includes

Leaminn Ma requested to merge add-include-rules-when-never into master

What does this MR do and why?

This MR adds support for when: never on conditional includes.

The file is not included into the pipeline if:

  • If no rules match.
  • If a rule matches and has when: never

If there are no rules, the file is included as expected. Also note that when: null behaves the same as not specifying when:.

Feature flag: ci_support_include_rules_when_never

Implementation:

Resolves #348146 (closed).

How to set up and validate locally

  1. Add two config files to your project root:

config1.yml:

job1:
  script: echo

config2.yml:

job2:
  script: echo
  1. Prior to turning on the Feature Flag, first observe that the when: value does not have any effect. Update your .gitlab-ci.yml file with the following content and run the pipeline.
include:
  - local: config1.yml
    rules:
      - if: $VAR == null
        when: never
  - local: config2.yml
    rules:
      - if: $VAR == null
        when: never

job0:
  script: echo

All 3 jobs are included in the pipeline.

Screenshot Screenshot_2023-06-06_at_4.42.59_PM
  1. Turn on the Feature Flag: Feature.enable(:ci_support_include_rules_when_never).
  2. Re-run the pipeline and observe that only the base job0 is present.
Screenshot Screenshot_2023-06-06_at_4.45.01_PM
  1. Test the following config. Observe that the rule precedence is followed (first matching rule takes effect) and the when: value works as expected. Only job0 and job2 should be present in the pipeline.
include:
  - local: config1.yml
    rules:
      - if: $VAR == null
        when: never
      - if: $VAR == null
  - local: config2.yml
    rules:
      - if: $VAR != null
        when: never
      - exists:
        - non-existing.yml
      - if: $VAR == null

job0:
  script: echo
Screenshot Screenshot_2023-06-06_at_4.50.43_PM

Also note that when: works with the exists condition as well. Example:

include:
  - local: config1.yml
    rules:
      - exists:
          - config1.yml
        when: never
      - if: $VAR == null
  - local: config2.yml
    rules:
      - if: $VAR != null
        when: never
      - exists:
          - non-existing.yml
      - exists:
          - config1.yml

job0:
  script: echo

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #348146 (closed)

Edited by Leaminn Ma

Merge request reports