Skip to content

Allow CI `only`/`except` to be combined with `rules` when null

Leaminn Ma requested to merge 215662-null-only-except-with-rules-valid into master

What does this MR do and why?

Per #215662 (closed), users would like to be able to override an extended job's only/except or rules keyword with null. Currently, the problem is that the CI validator is giving a validation error that prevents only/except and rules to be used together even if their values are nulled.

This MR implements the following:

  • Updated the FE CI json schema so that you can set rules to null
  • Updated the relevant validation statement so that it only applies when rules is not null
  • Modified the disallowed_keys validator to ignore keys with null values

Note that a blank value or ~ is equivalent to the Yaml null. In our examples below, we are explicitly setting the value as null.

Screenshots or screen recordings

  • Example config:
job-a:
  rules:
    - if: '$CI_MERGE_REQUEST_ID'
  script: echo "foo"

job-b:
  extends: job-a
  rules: null
  only:
    refs:
      - master
  except:
    refs:
      - merge_requests
  script: echo "bar"
  • Old Behaviour
    • Shows error message jobs:job-b config key may not be used with `rules`: only, except
    • rules: null has an FE validation error

Screenshot_2022-12-14_at_2.23.06_PM

  • New Behaviour
    • No validation errors

Screenshot_2022-12-14_at_2.25.20_PM

How to set up and validate locally

  1. Test the following config examples in your Project's CI/CD editor.
  • Config Example 1: rules combined with null only/except
.base-job:
  only:
    refs:
      - master
  except:
    variables:
      - $CI_JOB_NAME != "" # Always true, so .base-job should never run
  script: echo "base-job"
  
job1:
  extends: .base-job
  only: null
  except: null
  rules:
    - if: $CI_JOB_NAME == ""
  script: echo "job1"

job2:
  extends: .base-job
  only: null
  except: null
  rules:
    - if: $CI_JOB_NAME == "job2"
  script: echo "job2"
  • Config Example 2: only/except combined with null rules
.base-job:
  rules:
    - if: $CI_JOB_NAME == "" # Always false, so .base-job should never run
  script: echo "base-job"
  
job1:
  extends: .base-job
  rules: null
  only:
    refs:
      - master
  except:
    variables:
      - $CI_JOB_NAME != ""
  script: echo "job1"

job2:
  extends: .base-job
  rules: null
  only:
    refs:
      - master
  except:
    variables:
      - $CI_JOB_NAME == ""
  script: echo "job2"
  1. Observe that they do not produce validation errors.
  2. Commit the change (to run the pipeline) and see that only job2 runs, which indicates the .base-job's only/except/rules values are nulled and the overidden values take effect.
  3. Also observe that if you replace the null with a non-null value, then the validation error message displays as expected.

Screenshot_2022-12-14_at_2.49.32_PM

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 #215662 (closed)

Edited by Leaminn Ma

Merge request reports

Loading