2018-12-07 05:50:54 +00:00
|
|
|
# Pipelines for merge requests
|
|
|
|
|
2018-12-18 13:22:51 +00:00
|
|
|
> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/15310) in GitLab 11.6.
|
2018-12-07 05:50:54 +00:00
|
|
|
|
2018-12-18 13:22:51 +00:00
|
|
|
Usually, when you create a new merge request, a pipeline runs on the
|
2018-12-07 05:50:54 +00:00
|
|
|
new change and checks if it's qualified to be merged into a target branch. This
|
|
|
|
pipeline should contain only necessary jobs for checking the new changes.
|
2018-12-18 13:22:51 +00:00
|
|
|
For example, unit tests, lint checks, and [Review Apps](../review_apps/index.md)
|
|
|
|
are often used in this cycle.
|
2018-12-07 05:50:54 +00:00
|
|
|
|
|
|
|
With pipelines for merge requests, you can design a specific pipeline structure
|
|
|
|
for merge requests. All you need to do is just adding `only: [merge_requests]` to
|
|
|
|
the jobs that you want it to run for only merge requests.
|
|
|
|
Every time, when developers create or update merge requests, a pipeline runs on
|
|
|
|
their new commits at every push to GitLab.
|
|
|
|
|
|
|
|
NOTE: **Note**:
|
2018-12-18 13:22:51 +00:00
|
|
|
If you use both this feature and [Merge When Pipeline Succeeds](../../user/project/merge_requests/merge_when_pipeline_succeeds.md),
|
|
|
|
pipelines for merge requests take precedence over the other regular pipelines.
|
2018-12-07 05:50:54 +00:00
|
|
|
|
2018-12-18 13:22:51 +00:00
|
|
|
For example, consider the following [`.gitlab-ci.yml`](../yaml/README.md):
|
2018-12-07 05:50:54 +00:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
build:
|
|
|
|
stage: build
|
|
|
|
script: ./build
|
|
|
|
only:
|
|
|
|
- branches
|
|
|
|
- tags
|
|
|
|
- merge_requests
|
|
|
|
|
|
|
|
test:
|
|
|
|
stage: test
|
|
|
|
script: ./test
|
|
|
|
only:
|
|
|
|
- merge_requests
|
|
|
|
|
|
|
|
deploy:
|
|
|
|
stage: deploy
|
|
|
|
script: ./deploy
|
|
|
|
```
|
|
|
|
|
2018-12-18 13:22:51 +00:00
|
|
|
After the merge request is updated with new commits, GitLab detects that changes
|
|
|
|
have occurred and creates a new pipeline for the merge request.
|
2018-12-07 05:50:54 +00:00
|
|
|
The pipeline fetches the latest code from the source branch and run tests against it.
|
2018-12-18 13:22:51 +00:00
|
|
|
In the above example, the pipeline contains only `build` and `test` jobs.
|
|
|
|
Since the `deploy` job doesn't have the `only: [merge_requests]` rule,
|
2018-12-07 05:50:54 +00:00
|
|
|
deployment jobs will not happen in the merge request.
|
|
|
|
|
2018-12-18 13:22:51 +00:00
|
|
|
Pipelines tagged as **merge request** indicate that they were triggered
|
|
|
|
when a merge request was created or updated.
|
2018-12-07 05:50:54 +00:00
|
|
|
|
|
|
|
![Merge request page](img/merge_request.png)
|
|
|
|
|
|
|
|
The same tag is shown on the pipeline's details:
|
|
|
|
|
|
|
|
![Pipeline's details](img/pipeline_detail.png)
|
|
|
|
|
2019-02-08 07:55:56 +00:00
|
|
|
## Excluding certain jobs
|
2019-02-05 11:03:25 +00:00
|
|
|
|
|
|
|
The behavior of the `only: merge_requests` rule is such that _only_ jobs with
|
|
|
|
that rule are run in the context of a merge request; no other jobs will be run.
|
|
|
|
|
2019-02-11 23:21:12 +00:00
|
|
|
However, you may want to reverse this behaviour, having all of your jobs to run _except_
|
2019-02-11 08:08:00 +00:00
|
|
|
for one or two. Consider the following pipeline, with jobs `A`, `B`, and `C`. If you want
|
2019-02-08 07:55:41 +00:00
|
|
|
all pipelines to always run `A` and `B`, but only want `C` to run for a merge request,
|
2019-02-08 07:55:15 +00:00
|
|
|
you can configure your `.gitlab-ci.yml` file as follows:
|
2019-02-05 11:03:25 +00:00
|
|
|
|
|
|
|
``` yaml
|
|
|
|
.only-default: &only-default
|
|
|
|
only:
|
|
|
|
- master
|
|
|
|
- merge_requests
|
|
|
|
- tags
|
|
|
|
|
|
|
|
A:
|
|
|
|
<<: *only-default
|
|
|
|
script:
|
|
|
|
- ...
|
2019-02-11 23:21:12 +00:00
|
|
|
|
2019-02-05 11:03:25 +00:00
|
|
|
B:
|
|
|
|
<<: *only-default
|
|
|
|
script:
|
|
|
|
- ...
|
|
|
|
|
|
|
|
C:
|
|
|
|
script:
|
|
|
|
- ...
|
|
|
|
only:
|
|
|
|
- merge_requests
|
|
|
|
```
|
|
|
|
|
2019-02-11 08:08:23 +00:00
|
|
|
Since `A` and `B` are getting the `only:` rule to execute in all cases, they will
|
2019-02-08 07:55:03 +00:00
|
|
|
always run. `C` specifies that it should only run for merge requests, so for any
|
2019-02-05 11:03:25 +00:00
|
|
|
pipeline except a merge request pipeline, it will not run.
|
|
|
|
|
|
|
|
As you can see, this will help you avoid a lot of boilerplate where you'd need
|
2019-02-08 07:56:39 +00:00
|
|
|
to add that `only:` rule to all of your jobs in order to make them always run. You
|
2019-02-05 11:03:25 +00:00
|
|
|
can use this for scenarios like having only pipelines with merge requests get a
|
|
|
|
Review App set up, helping to save resources.
|
|
|
|
|
2018-12-07 05:50:54 +00:00
|
|
|
## Important notes about merge requests from forked projects
|
|
|
|
|
|
|
|
Note that the current behavior is subject to change. In the usual contribution
|
|
|
|
flow, external contributors follow the following steps:
|
|
|
|
|
|
|
|
1. Fork a parent project.
|
|
|
|
1. Create a merge request from the forked project that targets the `master` branch
|
|
|
|
in the parent project.
|
|
|
|
1. A pipeline runs on the merge request.
|
2018-12-18 13:22:51 +00:00
|
|
|
1. A maintainer from the parent project checks the pipeline result, and merge
|
2018-12-07 05:50:54 +00:00
|
|
|
into a target branch if the latest pipeline has passed.
|
|
|
|
|
|
|
|
Currently, those pipelines are created in a **forked** project, not in the
|
|
|
|
parent project. This means you cannot completely trust the pipeline result,
|
|
|
|
because, technically, external contributors can disguise their pipeline results
|
|
|
|
by tweaking their GitLab Runner in the forked project.
|
|
|
|
|
|
|
|
There are multiple reasons about why GitLab doesn't allow those pipelines to be
|
2018-12-07 10:30:54 +00:00
|
|
|
created in the parent project, but one of the biggest reasons is security concern.
|
2018-12-07 05:50:54 +00:00
|
|
|
External users could steal secret variables from the parent project by modifying
|
2018-12-18 13:22:51 +00:00
|
|
|
`.gitlab-ci.yml`, which could be some sort of credentials. This should not happen.
|
2018-12-07 05:50:54 +00:00
|
|
|
|
2018-12-07 10:30:54 +00:00
|
|
|
We're discussing a secure solution of running pipelines for merge requests
|
2018-12-07 05:50:54 +00:00
|
|
|
that submitted from forked projects,
|
|
|
|
see [the issue about the permission extension](https://gitlab.com/gitlab-org/gitlab-ce/issues/23902).
|