Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 201
Oleg Avatar asked Sep 11 '25 12:09

Oleg


2 Answers

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

like image 74
amBear Avatar answered Sep 13 '25 17:09

amBear


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.

like image 37
farch Avatar answered Sep 13 '25 17:09

farch