Search code examples
continuous-integrationgitlabgitlab-cigitlab-ci-runner

gitlab-ci: exclude merge requests from specific branch


I would like gitlab-ci to ignore merge from a specific branch.

Example: I have 3 branches. master, development & test.

If I push the code directly or merge the code from any branch to master/development, the pipelines get executed.

Now, I don't want pipeline to get running when I merge from test branch specifically.

I would like to exclude merge from test branch to development or master.


Solution

  • This can be done by specifying an if-rule for the jobs you want to exclude. For example:

    docker build:
      script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
      rules:
        - if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME != "test" || ($CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "test" and ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "development"))'
    

    The 2 variables CI_MERGE_REQUEST_SOURCE_BRANCH_NAME and CI_MERGE_REQUEST_TARGET_BRANCH_NAME are automatically defined only in the case of a merge request.