Skip a job when a MR label is used

I would like to skip a job if a specific label has been used to label the MR which triggers the pipeline

The job is a program that updates the changelog, so for some small and irrelevant changes I want to label my MR with “no-changelog-needed” and this job should be skipped.

What I tried to do is the following, but it doesn’t work

job_name:
  stage: docs
  rules:
    - if: $CI_MERGE_REQUEST_ID
    - if: $CI_MERGE_REQUEST_LABELS !~ /no-changelog-needed/
  script:
    - some commands

I also tried to merge the 2 if rules into one with parenthesis, but for some reason the YAML syntax becomes invalid

BONUS: is it possible to re-run automatically a single job if labels are updated while the MR is still open?

without the slashes I get another error due to wrong syntax and with double quotes it still runs the job

following another example I found in the docs, also this doesn’t work (the job runs anyway)

- if: "$CI_MERGE_REQUEST_LABELS =~ /no-changelog-needed/"
  when: never

I even tried to use a label without special characters and checked that the label is contained in the variable

  rules:
    - if: $CI_MERGE_REQUEST_ID
    - if: $CI_MERGE_REQUEST_LABELS =~ /nochangelog/
      when: never
  script:
    - echo $CI_MERGE_REQUEST_LABELS

the job runs anyway and I can see

$ echo $CI_MERGE_REQUEST_LABELS
nochangelog

Single quotes enclosing the regex variable comparison are required as far as I know. Similar to Can we trigger a pipeline by merge rquests labels? - #2 by snim2

 - if: '$CI_MERGE_REQUEST_LABELS =~ /nochangelog/'
      when: never

Maybe possible through a webhook, which then triggers a pipeline again (might need updated rules for pipeline source).

It’s easier, I’ve found, to have the jobs run by default, and then filter them out, rather than try to enable them with pipeline logic. Thus, you need to invert the logic using !~, instead of =~. This should work, I use it myself to selectively disable certain jobs:

workflow:
  rules:
    - when: always

my-job:
  rules:
    - if: '$CI_MERGE_REQUEST_LABELS !~ /nochangelog/'
      when: on_success
    - when: never

Note that there are no quotation marks around the pattern /nochangelog/ - this is important. There are single quotes around the entire expression - this is also important.

You can combine conditions, for example, in the next example the job will be skipped if any of the following is true:

  • The MR title contains the text [nochangelog] or even [NoChAnGeLoG],
  • The MR has the label “nochangelog” applied,
  • Or, the environment variable NO_CHANGE_LOG has value 1, for whatever reason:
my-job:
  rules:
    - if: '$CI_MERGE_REQUEST_TITLE !~ /\[nochangelog\]/i
           && $CI_MERGE_REQUEST_LABELS !~ /nochangelog/
           && $NO_CHANGE_LOG != "1"'
      when: on_success
    - when: never

1 Like