Disable pipeline for every commit in Gitlab and only run it on open merge request
Asked Answered
F

9

51

The CI pipeline runs on every commit in my Gitlab repository at work. Is there way to disable that and only run the CI pipeline on an open merge request to the master branch?

Federica answered 19/7, 2018 at 20:53 Comment(0)
A
51

There is currently no configuration option to do that. Here are some things that can be used to "disable" a pipeline build.

  • Adding [ci skip] inside the commit message will not trigger a pipeline on push.
  • Using except and only options on all jobs inside the pipeline. To avoid duplication in this case, you can use Anchors.

Update: GitLab 11.7

When pushing to GitLab you can skip triggering a pipeline by passing ci.skip option to the push command: git push -o ci.skip

Antietam answered 3/1, 2019 at 13:51 Comment(1)
Awesome information! Used the commit message option. Thanks!Surculose
F
24

Update in 2020 because solution with only and except are candidates for deprecation : https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-basic

Still in .gitlab-ci.yml you should use now rules : See https://docs.gitlab.com/ee/ci/yaml/#rules

I simply set this rule on my build job and the job is "blocked" and wait a manual trigger from the UI.

 rules:
   - when: manual

Note that we can create more advanced rules with conditions to trigger for exemple if we see a git tag.

https://docs.gitlab.com/ee/ci/yaml/#exclude-jobs-with-rules-from-certain-pipelines

Fisk answered 31/3, 2020 at 10:4 Comment(1)
Should be noted that this ccreates a manual job, that does not run immediately but requires to click a button on the GitLab UI to run. The question asked for a change to the type of trigger, from a commit to only on an open merge request. I think you’d want to do something like the example given if: $CI_PIPELINE_SOURCE == "merge_request_event" etc.Baldachin
P
11

Yes, but only if you set it on each job within the pipeline.

https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-basic

job:
  script: echo 'test'
  only:
    - merge_requests

That job will only run on commits that are part of a merge request, rather than every push of a commit to a branch. If you set every job to this setting then the pipeline will never run.

Propaedeutic answered 15/2, 2019 at 4:42 Comment(2)
This looks like the actual good answer ... Can't we add some generic rules to prevent all the CI to run ?Kareem
I don't think you can? You're better off setting some generic rules and using the yaml anchor syntax to include that in the rules for every job. Gitlab will evaluate each single job, rather than the entire pipeline as whole so you can't just set one generic rule.Propaedeutic
S
11

You could add something like this in the beginning of your .gitlab-ci.yaml

workflow:
  rules:
    - if: "$CI_COMMIT_BRANCH"
      when: never
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'

For more info look at Merge request pipelines

Seismism answered 22/3, 2022 at 22:29 Comment(0)
P
8

You can just add [ci skip] to the commit message and builds will be skipped

https://gitlab.com/gitlab-org/gitlab-ce/issues/14499

Perfume answered 11/9, 2019 at 17:48 Comment(0)
B
1

No. (Not yet !) You can follow the expected feature development here.

https://gitlab.com/gitlab-org/gitlab-ce/issues/23902

However, you can automatically trigger pipelines using "maofr"'s code

https://gitlab.com/gitlab-org/gitlab-ce/issues/23902#note_88958643

Bosh answered 23/7, 2018 at 20:7 Comment(0)
M
0

For merge we can use this guide #onlyrefs--exceptrefs

merge_requests

For pipelines created when a merge request is created or updated. Enables merge request pipelines, merged results pipelines, and merge trains.

and for generally triggering pipeline we can use only: and variables: together:

  script:
    - . diff.sh $SMS_MESSAGE
  only:
    variables:
      - $ACTION == "diff"

then triggering it is easy using curl:

curl -X POST \
    -F token=$TOKEN \     # your token
    -F ref=$REF_NAME \    # branch name
    -F "variables[ACTION]=diff" \   # variable 
    https://gitlab.com/api/v4/projects/0/trigger/pipeline # your project endpoint

This part variables[ACTION]=diff will assign diff into ACTION and it will apply to .gitlab-ci.yml and only: section.

Meshuga answered 12/8, 2021 at 7:2 Comment(0)
P
0
job:
  stage: build
  script:
    - echo "Do your build here"
  except:
    - pushes

Use except pushes to stop create a new pipeline on every pushed commits. For reference check this

Planospore answered 7/4, 2022 at 14:51 Comment(0)
S
0

Update 2022 : According to the documention of Gitlab on: https://docs.gitlab.com/ee/ci/yaml/#when

you can use when: manual to run the job only when triggered manually.

Example:

deploy_job:
  stage: deploy
  script:
    - make deploy
  when: manual

You don't need also rules for that.

Sweetandsour answered 6/7, 2022 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.