Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitlab-ci rules for manual job only on merge requests

I am finding it difficult to restrict a stage to only run on MR and be manual

I have the following rules

    rules:
        - when: manual
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
        - if: '$CI_COMMIT_BRANCH'
          when: never

but this stage is still running under branches, i do not want it to run on any branch, only on MR

it is literally driving me crazy. Code shows what should happen but it just does not follow it

So what am I missing?

like image 947
uberrebu Avatar asked Sep 21 '25 00:09

uberrebu


1 Answers

From the documentation:

The job is added to the pipeline:

  • If an if, changes, or exists rule matches and also has when: on_success (default), when: delayed, or when: always.
  • If a rule is reached that is only when: on_success, when: delayed, or when: always.

The job is not added to the pipeline:

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

So in order to achieve your requirements (which are add manual job only on MR, otherwise, do not add the job) the right order should be:

rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: manual
    - when: never

This translate to: "When the first if matches -> add job manually, in all other cases -> don't add the job".

like image 124
SomoKRoceS Avatar answered Sep 23 '25 05:09

SomoKRoceS