Merge request pipelines

You can configure your pipeline to run every time you commit changes to a branch. This type of pipeline is called a branch pipeline.

Alternatively, you can configure your pipeline to run every time you make changes to the source branch for a merge request. This type of pipeline is called a merge request pipeline.

Branch pipelines:

Merge request pipelines:

  • Run when you:
    • Create a new merge request from a source branch with one or more commits.
    • Push a new commit to the source branch for a merge request.
    • Select Run pipeline from the Pipelines tab in a merge request. This option is only available when merge request pipelines are configured for the pipeline and the source branch has at least one commit.
  • Do not run by default. The jobs in the CI/CD configuration file must be configured to run in merge request pipelines.
  • Have access to more predefined variables.
  • Do not have access to protected variables or protected runners.

Both of these types of pipelines can appear on the Pipelines tab of a merge request.

Types of merge request pipelines

The three types of merge request pipelines are:

Prerequisites

To use merge request pipelines:

  • Your project’s CI/CD configuration file must be configured with jobs that run in merge request pipelines. To do this, you can use:
  • You must have at least the Developer role in the source project to run a merge request pipeline.
  • Your repository must be a GitLab repository, not an external repository.

Use rules to add jobs

You can use the rules keyword to configure jobs to run in merge request pipelines. For example:

job1:
  script:
    - echo "This job runs in merge request pipelines"
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

You can also use the workflow: rules keyword to configure the entire pipeline to run in merge request pipelines. For example:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

job1:
  script:
    - echo "This job runs in merge request pipelines"

job2:
  script:
    - echo "This job also runs in merge request pipelines"

Use only to add jobs

You can use the only keyword with merge_requests to configure jobs to run in merge request pipelines.

job1:
  script:
    - echo "This job runs in merge request pipelines"
  only:
    - merge_requests

Use with forked projects

Version history

External contributors who work in forks can’t create pipelines in the parent project.

A merge request from a fork that is submitted to the parent project triggers a pipeline that:

  • Is created and runs in the fork (source) project, not the parent (target) project.
  • Uses the fork project’s CI/CD configuration, resources, and project CI/CD variables.

Pipelines for forks display with the fork badge in the parent project:

Pipeline ran in fork

Run pipelines in the parent project

Project members in the parent project can choose to run a merge request pipeline for a merge request submitted from a fork project. This pipeline:

  • Is created and runs in the parent (target) project, not the fork (source) project.
  • Uses the CI/CD configuration present in the fork project’s branch
  • Uses the parent project’s CI/CD configuration, resources, and project CI/CD variables.
  • Uses the permissions of the parent project member that triggers the pipeline.

Run pipelines in fork project MRs to ensure that the post-merge pipeline passes in the parent project. Additionally, if you do not trust the fork project’s runner, running the pipeline in the parent project uses the parent project’s trusted runners.

caution
Fork merge requests can contain malicious code that tries to steal secrets in the parent project when the pipeline runs, even before merge. As a reviewer, carefully check the changes in the merge request before triggering the pipeline. GitLab shows a warning that you must accept before you can trigger the pipeline.

Prerequisites:

  • You must be a member of the parent project and have at least the Developer role.
  • The fork project must be visible to the user running the pipeline. Otherwise, the Pipelines tab does not display in the merge request.

To run a pipeline in the parent project for a merge request from a fork project:

  1. In the merge request, go to the Pipelines tab.
  2. Select Run pipeline. You must accept the warning, or the pipeline does not run.

Available predefined variables

When you use merge request pipelines, you can use:

Troubleshooting

Two pipelines when pushing to a branch

If you get duplicate pipelines in merge requests, your pipeline might be configured to run for both branches and merge requests at the same time. Adjust your pipeline configuration to avoid duplicate pipelines.

In , you can add workflow:rules to switch from branch pipelines to merge request pipelines. After a merge request is open on the branch, the pipeline switches to a merge request pipeline.

Two pipelines when pushing an invalid CI/CD configuration file

If you push an invalid CI/CD configuration to a merge request’s branch, two failed pipelines appear in the pipelines tab. One pipeline is a failed branch pipeline, the other is a failed merge request pipeline.

When the configuration syntax is fixed, no further failed pipelines should appear. To find and fix the configuration problem, you can use:

The merge request’s pipeline is marked as failed but the latest pipeline succeeded

It’s possible to have both branch pipelines and merge request pipelines in the Pipelines tab of a single merge request. This might be by configuration, or by accident.

If both types of pipelines are in one merge request, the merge request’s pipeline is not considered successful if:

  • The branch pipeline succeeds.
  • The merge request pipeline fails.

When using the merge when pipeline succeeds feature and both pipelines types are present, the merge request pipelines are checked, not the branch pipelines.