Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple `rules` clause in GitLab CI config

Tags:

gitlab-ci

I have many rules in my CI config, and also many anchors. It's not an offence to the linter to mention rules: multiple times per job, but the same linter does not help with testing whether multiple rules: clauses add up and in which order.

So the trouble is, now I have to repeat the slightly changing set of rules in every job.

.build-rules:                      &build-rules
  rules:
    - if: '$DEPLOY_TAG'
      when: never
    - if: '$CI_COMMIT_REF_NAME == "master"'
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_COMMIT_REF_NAME =~ "/^v[0-9]+\.[0-9]+.*$/"'

job_with_changed_rule:
  <<:               *build-rules
  rules:
    - if: '$DEPLOY_TAG'
  script:
    - do something

job_with_another_rule:
  <<:               *build-rules
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
  script:
    - do something else
like image 251
Denis Pisarev Avatar asked Oct 28 '25 04:10

Denis Pisarev


1 Answers

Before Gitlab 14.3 the only thing you could do was placing extends block

But now you can re-use rules from other jobs with !reference tag.

For more details see this docs

like image 55
Dmitrii R3knit Avatar answered Oct 31 '25 12:10

Dmitrii R3knit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!