18

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

4 Answers4

9

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

santhoshRenga
  • 141
  • 2
  • 6
3

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.

Stanley Shyiko
  • 561
  • 1
  • 5
  • 10
  • hi Stanley, I'm wondering, is your repo deprecated now? I got some MRs in my gitlab repo already running the gitlabci build without me doing anything – knocte Nov 25 '16 at 09:00
  • @knocte not really, no. The default behavior of GitLab CI is (still) to run a build on each "push". Imagine that you have a `feature-*` branch that might or might not be merged into `develop` based on whether the experiment is going to pan out or not. You might want to skip running builds needlessly until that branch is stabilized (i.e. MR created) (especially if each build takes significant amount of time). This is what gitlab-ci-build-on-merge-request can help you with (e.g. limit builds to `master`, `develop`, `release-*` branches + MRs). – Stanley Shyiko Nov 25 '16 at 23:55
  • the links in the answer became 404, is this link https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/270 to be the corrected now? – William Leung Jan 18 '17 at 04:00
  • @WilliamLeung not really. gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/270 talks about running build on "a merge of source and target" branches. Original ticket was about pretty much what OP asked. – Stanley Shyiko Jan 18 '17 at 19:37
1

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
Derioss
  • 86
  • 4
0

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/'
Michael Dreher
  • 1,369
  • 11
  • 17