devopsgitlab-cipipelinepull-requestmerge-request

Gitlab ci rule for merge request event not works as expected


I have a gitlab pipeline and I'm trying to set a rule for a merge request event, i want to fire the rule when i have a merge request and the source branch is different from main and develop. I do that in my job.

  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^develop|^main/

If i just do that it works

    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

The variable $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME is available.

But it seems that the regex doesn't works or the &&


Solution

  • In my question the regex was wrong. To resolve my problem i do that I just simply split the evalution in two steps

    1. I check if the source is a merge request event and it is on branch develop or main. In that case i will not execute the pipeline.

    otherwise i will go to the step 2 that will evaluate the merge request event, but for other branches different from main or develop

      rules:
        - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_COMMIT_BRANCH =~ /^develop|^main/
          when: never
        - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
          changes:
            - 'yarn.lock'