2021-06-29 14:07:04 -04:00
---
stage: Verify
group: Pipeline Execution
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
---
2022-01-27 04:14:40 -05:00
# Merge request pipelines **(FREE)**
> [Renamed](https://gitlab.com/gitlab-org/gitlab/-/issues/351192) from `pipelines for merge requests` to `merge request pipelines` in GitLab 14.8.
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
You can configure your [pipeline ](index.md ) to run every time you commit changes to a branch.
This type of pipeline is called a *branch pipeline* .
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
Alternatively, you can configure your pipeline to run every time you make changes to the
2022-01-27 04:14:40 -05:00
source branch for a merge request. This type of pipeline is called a *merge request pipeline* .
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
Branch pipelines:
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
- Run when you push a new commit to a branch.
- Are the default type of pipeline.
- Have access to [some predefined variables ](../variables/predefined_variables.md ).
2022-05-06 20:08:31 -04:00
- Have access to [protected variables ](../variables/index.md#protected-cicd-variables ) and [protected runners ](../runners/configure_runners.md#prevent-runners-from-revealing-sensitive-information ).
2021-06-29 14:07:04 -04:00
2022-01-27 04:14:40 -05:00
Merge request pipelines:
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
- Run when you:
2022-03-30 05:08:12 -04:00
- Create a new merge request from a source branch with one or more commits.
2022-01-17 04:15:55 -05:00
- 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
2022-03-30 05:08:12 -04:00
is only available when merge request pipelines are configured for the pipeline
and the source branch has at least one commit.
2022-01-17 04:15:55 -05:00
- Do not run by default. The jobs in the CI/CD configuration file [must be configured ](#prerequisites )
2022-01-27 04:14:40 -05:00
to run in merge request pipelines.
2022-01-17 04:15:55 -05:00
- Have access to [more predefined variables ](#available-predefined-variables ).
2022-05-06 20:08:31 -04:00
- Do not have access to [protected variables ](../variables/index.md#protected-cicd-variables ) or [protected runners ](../runners/configure_runners.md#prevent-runners-from-revealing-sensitive-information ).
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
Both of these types of pipelines can appear on the **Pipelines** tab of a merge request.
2021-06-29 14:07:04 -04:00
2022-01-27 04:14:40 -05:00
## Types of merge request pipelines
2021-06-29 14:07:04 -04:00
2022-01-27 04:14:40 -05:00
The three types of merge request pipelines are:
2021-06-29 14:07:04 -04:00
2022-01-27 04:14:40 -05:00
- Merge request pipelines, which run on the changes in the merge request's
2022-03-15 05:07:33 -04:00
source branch. [Introduced ](https://gitlab.com/gitlab-org/gitlab/-/issues/352939 )
in GitLab 14.9, these pipelines display a `merge request` label to indicate that the
2022-01-17 04:15:55 -05:00
pipeline ran only on the contents of the source branch, ignoring the target branch.
2022-03-15 05:07:33 -04:00
In GitLab 14.8 and earlier, the label is `detached` .
2022-01-27 04:14:40 -05:00
- [Merged results pipelines ](merged_results_pipelines.md ), which run on
2022-01-17 04:15:55 -05:00
the result of combining the source branch's changes with the target branch.
- [Merge trains ](merge_trains.md ), which run when merging multiple merge requests
at the same time. The changes from each merge request are combined into the
target branch with the changes in the earlier enqueued merge requests, to ensure
they all work together.
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
## Prerequisites
2021-06-29 14:07:04 -04:00
2022-01-27 04:14:40 -05:00
To use merge request pipelines:
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
- Your project's [CI/CD configuration file ](../yaml/index.md ) must be configured with
2022-01-27 04:14:40 -05:00
jobs that run in merge request pipelines. To do this, you can use:
2022-01-17 04:15:55 -05:00
- [`rules` ](#use-rules-to-add-jobs ).
- [`only/except` ](#use-only-to-add-jobs ).
2022-01-30 19:14:49 -05:00
- You must have at least the Developer role in the
2022-01-27 04:14:40 -05:00
source project to run a merge request pipeline.
2022-01-17 04:15:55 -05:00
- Your repository must be a GitLab repository, not an [external repository ](../ci_cd_for_external_repos/index.md ).
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
## Use `rules` to add jobs
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
You can use the [`rules` ](../yaml/index.md#rules ) keyword to configure jobs to run in
2022-01-27 04:14:40 -05:00
merge request pipelines. For example:
2021-06-29 14:07:04 -04:00
```yaml
2022-01-17 04:15:55 -05:00
job1:
script:
2022-01-27 04:14:40 -05:00
- echo "This job runs in merge request pipelines"
2022-01-17 04:15:55 -05:00
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
2021-06-29 14:07:04 -04:00
```
2022-01-17 04:15:55 -05:00
You can also use the [`workflow: rules` ](../yaml/index.md#workflowrules ) keyword
2022-01-27 04:14:40 -05:00
to configure the entire pipeline to run in merge request pipelines. For example:
2021-06-29 14:07:04 -04:00
```yaml
2022-01-17 04:15:55 -05:00
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
job1:
2021-06-29 14:07:04 -04:00
script:
2022-01-27 04:14:40 -05:00
- echo "This job runs in merge request pipelines"
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
job2:
2021-06-29 14:07:04 -04:00
script:
2022-01-27 04:14:40 -05:00
- echo "This job also runs in merge request pipelines"
2021-06-29 14:07:04 -04:00
```
2022-01-17 04:15:55 -05:00
## Use `only` to add jobs
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
You can use the [`only` ](../yaml/index.md#onlyrefs--exceptrefs ) keyword with `merge_requests`
2022-01-27 04:14:40 -05:00
to configure jobs to run in merge request pipelines.
2021-06-29 14:07:04 -04:00
```yaml
2022-01-17 04:15:55 -05:00
job1:
script:
2022-01-27 04:14:40 -05:00
- echo "This job runs in merge request pipelines"
2022-01-17 04:15:55 -05:00
only:
- merge_requests
2021-06-29 14:07:04 -04:00
```
2022-01-17 04:15:55 -05:00
## Use with forked projects
2021-06-29 14:07:04 -04:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/217451) in GitLab 13.3.
> - [Moved](https://about.gitlab.com/blog/2021/01/26/new-gitlab-product-subscription-model/) to GitLab Premium in 13.9.
2022-01-17 04:15:55 -05:00
External contributors who work in forks can't create pipelines in the parent project.
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
A merge request from a fork that is submitted to the parent project triggers a
pipeline that:
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
- 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.
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
Pipelines for forks display with the **fork** badge in the parent project:
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
![Pipeline ran in fork ](img/pipeline_fork_v13_7.png )
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
### Run pipelines in the parent project **(PREMIUM)**
2022-05-17 17:08:51 -04:00
Project members in the parent project can trigger a merge request pipeline
2022-01-17 04:15:55 -05:00
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.
2022-05-17 17:08:51 -04:00
- Uses the CI/CD configuration present in the fork project's branch.
- Uses the parent project's CI/CD settings, resources, and project CI/CD variables.
2022-01-17 04:15:55 -05:00
- 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.
2021-06-29 14:07:04 -04:00
WARNING:
Fork merge requests can contain malicious code that tries to steal secrets in the
2022-01-17 04:15:55 -05:00
parent project when the pipeline runs, even before merge. As a reviewer, carefully
2022-05-17 17:08:51 -04:00
check the changes in the merge request before triggering the pipeline. If you trigger
the pipeline by selecting **Run pipeline** or applying a suggestion, GitLab shows
a warning that you must accept before the pipeline runs. If you trigger the pipeline
by using any other method, including the API, [`/rebase` quick action ](../../user/project/quick_actions.md#issues-merge-requests-and-epics ),
or [**Rebase** option ](../../user/project/merge_requests/methods/index.md#rebasing-in-semi-linear-merge-methods ),
**no warning displays**.
2021-06-29 14:07:04 -04:00
2022-02-16 10:14:05 -05:00
Prerequisites:
- You must be a member of the parent project and have at least the [Developer role ](../../user/permissions.md ).
2022-03-18 14:07:26 -04:00
- The fork project must be [visible ](../../user/public_access.md ) to the
2022-02-16 10:14:05 -05:00
user running the pipeline. Otherwise, the **Pipelines** tab does not display
in the merge request.
2022-05-17 17:08:51 -04:00
To use the UI to run a pipeline in the parent project for a merge request from a fork project:
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
1. In the merge request, go to the **Pipelines** tab.
2022-05-17 17:08:51 -04:00
1. Select **Run pipeline** . You must read and accept the warning, or the pipeline does not run.
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
## Available predefined variables
2021-06-29 14:07:04 -04:00
2022-01-27 04:14:40 -05:00
When you use merge request pipelines, you can use:
2022-01-17 04:15:55 -05:00
- All the same [predefined variables ](../variables/predefined_variables.md ) that are
available in branch pipelines.
- [Additional predefined variables ](../variables/predefined_variables.md#predefined-variables-for-merge-request-pipelines )
2022-01-27 04:14:40 -05:00
available only to jobs in merge request pipelines. These variables contain
2022-01-17 04:15:55 -05:00
information from the associated merge request, which can be when calling the
[GitLab Merge Request API endpoint ](../../api/merge_requests.md ) from a job.
## Troubleshooting
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
### Two pipelines when pushing to a branch
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
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 ](../jobs/job_control.md#avoid-duplicate-pipelines ).
2021-06-29 14:07:04 -04:00
2021-10-08 05:11:26 -04:00
In [GitLab 13.7 and later ](https://gitlab.com/gitlab-org/gitlab/-/issues/201845 ),
2022-01-27 04:14:40 -05:00
you can add `workflow:rules` to [switch from branch pipelines to merge request pipelines ](../yaml/workflow.md#switch-between-branch-pipelines-and-merge-request-pipelines ).
2021-06-29 14:07:04 -04:00
After a merge request is open on the branch, the pipeline switches to a merge request pipeline.
2022-01-17 04:15:55 -05:00
### 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,
2022-01-27 04:14:40 -05:00
the other is a failed merge request pipeline.
2022-01-17 04:15:55 -05:00
When the configuration syntax is fixed, no further failed pipelines should appear.
To find and fix the configuration problem, you can use:
- The [pipeline editor ](../pipeline_editor/index.md ).
- The [CI lint tool ](../lint.md ).
### The merge request's pipeline is marked as failed but the latest pipeline succeeded
2022-01-27 04:14:40 -05:00
It's possible to have both branch pipelines and merge request pipelines in the
2022-01-17 04:15:55 -05:00
**Pipelines** tab of a single merge request. This might be [by configuration ](../yaml/workflow.md#switch-between-branch-pipelines-and-merge-request-pipelines ),
or [by accident ](#two-pipelines-when-pushing-to-a-branch ).
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
If both types of pipelines are in one merge request, the merge request's pipeline
is not considered successful if:
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
- The branch pipeline succeeds.
2022-01-27 04:14:40 -05:00
- The merge request pipeline fails.
2021-06-29 14:07:04 -04:00
2022-01-17 04:15:55 -05:00
When using the [merge when pipeline succeeds ](../../user/project/merge_requests/merge_when_pipeline_succeeds.md )
2022-01-27 04:14:40 -05:00
feature and both pipelines types are present, the merge request pipelines are checked,
2022-01-17 04:15:55 -05:00
not the branch pipelines.