Is there a way to NOT run pipeline when merge request is created

When I create a new merge request, it runs all the stages that COULD run on a merge request branch regardless if they are appropriate. I would prefer to not run any stages on a new branch creation as i know all the tests pass on that existing commit hash on main.

Is there a way to not run ANY pipelines on a merge request creation?

You can accomplish this using rules.

For example:

job:
  script: echo "I <3 the GitLab Community!"
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
3 Likes

thanks greg for your response!

here’s a section of my gitlab-ci config


create-build-image:
  stage: create_build_image
  image: docker
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - if: ('$CI_PIPELINE_SOURCE == "merge_request_event"' && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH)
      changes:
        - build/Dockerfile.gitlab
        - .gitlab-ci.yml
      when: always

this runs every time i create a new merge request branch.

i want this to only run when i commit a change to one of those two files on a merge request branch. i noticed that i had to put in the $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH to make sure it didn’t run when i merged to main

i have those 2 lines in there, but how do i do this? it takes over 30 minutes to build this docker image and i almost never change the config.

hi!

is there someone i can talk to directly about this issue that i’m seeing?

thanks!
torvik

There’s a problem with the logic of your rules statements.

IF this CI pipeline is triggered by a merge request THEN never run create-build-image
OR
IF (this CI pipeline is triggered by a merge request) AND (CI commit branch name (variable not available in merge requests) is not default branch) THEN always run create-build-image

I’m not sure I understand what you’re going for, but if you want a pipeline that only runs on a merge to default branch if there was a change detected to Dockerfile or .gitlab-ci.yml, I think you’d want a rule like this:

job:
  rules:
    - if '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH'
      changes:
        - Dockerfile
        - .gitlab-ci.yml

Reference:

cool!

what if i only want to run the pipeline when i’ve pushed changes to those files to the merge request branch? and not when i merge to main.

I’m not sure I understand what you mean by merge request branch.

Merge requests are comprised of two branches - a source branch and a target branch. Can you clarify what you mean by “merge request branch”? Do you mean “any non-default branch with a merge request open”?

The answers for what you’re looking to do will ultimately be found here:

Those are the only two resources I’ve needed to answer your questions.

alright.

i think i can put in something about $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME != $CI_DEFAULT_BRANCH…

when i click on Create Merge Request it creates a merge request branch off of main. this triggers a merge request event, but i’m guessing the source branch name in that event will be main

when i push changes to that branch, it triggers a merge request event with source and target branch being my merge request branch. this is when i want to run this pipeline. this is currently happening.

when i click on Merge to close the merge request, it triggers a merge request event. the target branch name is main. i don’t want to run this pipeline then. this is currenting happening as expected.

so if i fiddle with the $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == $CI_DEFAULT_BRANCH i might be able to handle the first action.

@gitlab-greg

hello!

alright, so i’ve modified the rules section of one of my pipelines.

  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      changes:
        - build/Dockerfile.gitlab
        - .gitlab-ci.yml
      when: always

this works nicely to prevent the pipeline from building when i merge a merge request into the main branch. but it still fires when i create a new merge request. it shouldn’t be triggering a merge request pipeline, and it isn’t a new commit. So, how can i prevent this pipeline from running when i create a new branch?

thanks!
torvik

is there a way to disable pipelines for when merge requests are accepted ?

  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "develop"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_PIPELINE_SOURCE != "push" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_REF_NAME == "master"'
    - if: '$CI_PIPELINE_SOURCE != "push" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_REF_NAME == "develop"' 

these are the rules i have for my job

1 Like