Run all jobs on a gitlab ci MR pipeline, even if some don't have a merge_request_event rule, but do not run both MR and branch pipelines

Viewed 133

In case the terminology is not standard, here is how I am using the below terms:

  • branch pipeline: A pipeline that is run when pushing to a branch.
  • MR pipeline: A pipeline that is run on a merge request, or pushes to a merge request branch.

I want to write a pipeline with two jobs, job_A and job_B. job_A should run on all pipelines. job_B should run only on merge request pipelines. One solution is to combine the workaround proposed in issue 194129, adding a workflow rule of - if: $CI with a merge_request_event rule, i.e.:

image: alpine

workflow:
  rules:
    - if: $CI

stages:
- stage_A
- stage_B

job_A:
  stage: stage_A
  script:
    - echo "Hello from Job A"

job_B:
  stage: stage_B
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  script:
    - echo "Hello from Job B"

Now my pipeline runs in full on the MR -- which is what I wanted. However, two pipelines are being run now, the branch pipeline and the MR pipeline.

I want both job_A and job_B to run on MR pipelines even though job_A doesn't have the merge_request_event rule. But, I only want one pipeline to run when an MR is open -- the MR pipeline. How can I achieve this?

2 Answers

The correct answer is found in the gitlab docs.

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH

Try using only:variables. I had the same issue when mixing only/except with rules in one pipeline.

job_B:
  stage: stage_B
  only:
    variables:
      - $CI_PIPELINE_SOURCE == "merge_request_event"
  script:
    - echo "Hello from Job B"
Related