Oleg
Oleg

Reputation: 403

How to write .gitlab-ci.yml job to run only in merge-request

How to right write job in .gitlab-ci.yml when it run only in merge requests?

test_c:
  stage: test
  script:
    - echo "This job tests something. It will only run when all jobs in the"
    - echo "build stage are complete."
  only:
    - merge_requests

This job not run in merge request, but not run and in commint in master or develop.

Upvotes: 24

Views: 61859

Answers (4)

Hunter_71
Hunter_71

Reputation: 789

If your intention is just not to run the job on specific branches, like master or dev, you may simply exclude them with except:

test_c:
  (...)
  except:
    - master
    - dev

Upvotes: 2

farch
farch

Reputation: 492

You can use the workflow to control pipeline creation. Define this keyword at the top level, with a single rules. This example shows that the pipeline will only be executed when a new merge request is created, the last when is set to never to prevent pipelines from executing when a new branch is pushed to the server or for any other type of event.

workflow:
   rules:
     - if: $CI_MERGE_REQUEST_ID
       when: always
     - when: never

Notice As mentioned in the Gitlab documentation

These pipelines are labeled as detached in the UI, and they do not have access to protected variables. Otherwise, these pipelines are the same as other pipelines.

Upvotes: 9

amBear
amBear

Reputation: 1206

Gitlab documentation recommends using 'rules' instead of 'only'. You can accomplish only merge_requests by doing the following:

test_c:
  stage: test
  script:
    - echo "This job tests something. It will only run when all jobs in the"
    - echo "build stage are complete."
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

https://docs.gitlab.com/ee/ci/yaml/#workflowrules

Upvotes: 47

Sergio Tanaka
Sergio Tanaka

Reputation: 1435

Your code is correct, commit it to master before test your merge_request pipelines please.

Upvotes: 1

Related Questions