user9608133
user9608133

Reputation:

".gitlab-ci.yml" rule to do not run a pipeline on merge request creation

Important note before going further:

The question GitLab do not run CI/CD pipeline when creating new merge request is not a duplicate: I am asking about ".gitlab-ci.yml" rules, but that question has no answers about this.

Current GitLab (default) behavior

On an issue page I click "Create Merge Request" --> A new pipeline is started automatically.

Required behavior

Do not run a pipeline on merge request creation

My current ".gitlab-ci.yml"

.default_rules:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "web"'
      when: manual
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - "**/*.{py,c,cpp}"
        - .gitlab-ci.yml
        - poetry.lock
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH
      changes:
        - "**/*.{py,c,cpp}"
        - .gitlab-ci.yml
        - poetry.lock
    - if: $PIPELINE_TYPE == "multi-project-pipeline"

Upvotes: 1

Views: 3408

Answers (1)

user9608133
user9608133

Reputation:

SOLUTION
Do not run a job on Merge Request creation:

# .gitlab-ci.yml

your_job_name:
  rules:
    - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
      when: never

A LITTLE USEFUL BONUS
Full rules to avoid duplicate pipelines (just in case if someone needs this):

  rules:
    # Prevent creating duplicate pipelines because pipelines are already created for PUSH events.
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    # Do not run after Merge Request closing.
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: never
    # Do not run on Merge Request creation.
    - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
      when: never
    # When to run:
    - if: $CI_PIPELINE_SOURCE == "push"
      changes:
        # If the code has changed
        - "**/*.py"

Upvotes: 4

Related Questions