Kaviraj Kanagaraj
Kaviraj Kanagaraj

Reputation: 431

Gitlab-CI: gitlab ci trigger build only for merge request

Im using gitlab 8.1.4. And using gitlab-ci thats comes built-in. By default, gitlab-ci is triggering build for every push. How can make it trigger only during creation of merge request?

Thanks in advance

Upvotes: 18

Views: 7956

Answers (4)

Michael Dreher
Michael Dreher

Reputation: 1399

you would typically not distinguish how the change comes to a branch (by checking in or by merging) but on the type of branch or on the name of the branch. Use the rules to define for which branches which build shall be used.

It is a good practice to run the (full) build only on protected branches (e.g. master and release) and only a small verification build on dev branches.

rules:
    - if: '$CI_COMMIT_REF_PROTECTED == "true"

In the settings you configure which branches are protected. Normally only maintainers can modify protected branches.

You can also base the rule on the name of the branch. We use this to disable sonar-scan on branches which include the name part "nosonar". Additionally we skip it for all builds which are triggered by setting specific tags (like 'tools-v1.2.3'):

  rules:
    - if: '$CI_COMMIT_TAG !~ /^(release|tools)-v.*/ && $CI_COMMIT_TAG !~ /nosonar/'

Upvotes: 0

Derioss
Derioss

Reputation: 101

the right synthax

on stage

stage: build
script:
    - xxxxx
rules:
  - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    when: always

on all ci

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: always

Upvotes: 1

santhoshRenga
santhoshRenga

Reputation: 141

Try the following into your .gitlab-ci.yml file.

stage: build
script:
    - xxxxx
artifacts:
    paths:
        - xxxxx
tags:
    - xxxx
only:
    - merge_requests

Source: https://docs.gitlab.com/ee/ci/yaml/#only-and-except-simplified

Upvotes: 9

Stanley Shyiko
Stanley Shyiko

Reputation: 581

You could try gitlab-ci-build-on-merge-request. Gitlab issue that explores other options - https://gitlab.com/gitlab-org/gitlab-ci/issues/360.

Disclaimer: I'm the author of gitlab-ci-build-on-merge-request.

Upvotes: 3

Related Questions