Search code examples
gitlabgitlab-cicicdgitlab-ci.yml

Gitlab CI MR pipeline runs against the rules


I have a job that runs when an MR is created from main branch to develop branch though it shouldn't according to the rules:

rules:
    - if: $CI_MERGE_REQUEST_SOURCE_BRANCH == "main" && $CI_MERGE_REQUEST_TARGET_BRANCH == "develop"
      when: never
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_PROTECTED == "true"
    - if: $CI_COMMIT_REF_PROTECTED == "true" && $CI_COMMIT_BRANCH !~ /^hotfix\/.*/
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "develop"
      changes:
      - Admin.Service/**/*

here is my workflow:

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /phase-.*/
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^bulk\/.*/
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /release-.*/ && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /bugfix.*/
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /release-.*/
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^hotfix\/.*/
    - if: $CI_PIPELINE_SOURCE == "trigger"
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_REF_PROTECTED == "true" && $CI_COMMIT_BRANCH

In my understanding the first rule should disable the job and the following rules should be ignored accordingly. Please help me figure out what I am doing wrong. How can I prevent the job from getting into the MR pipeline?


Solution

  • rules:
        - if: $CI_MERGE_REQUEST_SOURCE_BRANCH == "main" && $CI_MERGE_REQUEST_TARGET_BRANCH == "develop"
          when: never
    

    The variable names are incorrect. It should be CI_MERGE_REQUEST_SOURCE_BRANCH_NAME and CI_MERGE_REQUEST_TARGET_BRANCH_NAME.

    Corrected version:

    rules:
        - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "main" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"
          when: never
    

    See predefined variables reference.