Search code examples
gitlabgitlab-ci

GitlabCI and pipeline for a new branch


By default, Gitlab CI starts the pipeline when a new branch is created (based on an existing one, not even the main one). I don't understand how to turn this off? It should not run the pipeline and jobs when creating a new branch. Only when pushing into branches and open MR.

I have this now, but it doesn't work.

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_PIPELINE_SOURCE == "push"

Solution

  • You can check CI_COMMIT_BEFORE_SHA which will be 0000000000000000000000000000000000000000 when creating a new branch. Something like:

    workflow:
      rules:
        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
        # uncomment if you want tag pipelines 
        # - if: $CI_COMMIT_TAG
        - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
          when: never
        - if: $CI_PIPELINE_SOURCE == "push"
    

    Alternatively, if you are pushing a new branch from the CLI, you can use a git push option to skip the pipeline:

    git push -o ci.skip