index.md 4,6 КБ
Newer Older
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
1
2
# Pipelines for merge requests

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
3
> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/15310) in GitLab 11.6.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
4

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
5
Usually, when you create a new merge request, a pipeline runs on the
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
6
7
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.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
8
9
For example, unit tests, lint checks, and [Review Apps](../review_apps/index.md)
are often used in this cycle.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
10
11

With pipelines for merge requests, you can design a specific pipeline structure
Evan Read's avatar
Evan Read включено в состав коммита
12
13
14
15
for merge requests.

## Configuring pipelines for merge requests

Evan Read's avatar
Evan Read включено в состав коммита
16
17
To configure pipelines for merge requests, add the `only: merge_requests` parameter to
the jobs that you want to run only for merge requests.
Evan Read's avatar
Evan Read включено в состав коммита
18

Evan Read's avatar
Evan Read включено в состав коммита
19
20
Then, when developers create or update merge requests, a pipeline runs
every time a commit is pushed to GitLab.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
21
22

NOTE: **Note**:
Evan Read's avatar
Evan Read включено в состав коммита
23
If you use this feature with [merge when pipeline succeeds](../../user/project/merge_requests/merge_when_pipeline_succeeds.md),
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
24
pipelines for merge requests take precedence over the other regular pipelines.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
25

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
26
For example, consider the following [`.gitlab-ci.yml`](../yaml/README.md):
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

```yaml
build:
  stage: build
  script: ./build
  only:
  - branches
  - tags
  - merge_requests

test:
  stage: test
  script: ./test
  only:
  - merge_requests

deploy:
  stage: deploy
  script: ./deploy
```

Evan Read's avatar
Evan Read включено в состав коммита
48
49
50
51
52
After the merge request is updated with new commits:

- GitLab detects that changes have occurred and creates a new pipeline for the merge request.
- The pipeline fetches the latest code from the source branch and run tests against it.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
53
In the above example, the pipeline contains only `build` and `test` jobs.
Evan Read's avatar
Evan Read включено в состав коммита
54
Since the `deploy` job doesn't have the `only: merge_requests` parameter,
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
55
56
deployment jobs will not happen in the merge request.

Evan Read's avatar
Evan Read включено в состав коммита
57
Pipelines tagged with the **merge request** badge indicate that they were triggered
Evan Read's avatar
Evan Read включено в состав коммита
58
when a merge request was created or updated. For example:
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
59
60
61
62
63
64
65

![Merge request page](img/merge_request.png)

The same tag is shown on the pipeline's details:

![Pipeline's details](img/pipeline_detail.png)

Jason Lenny's avatar
Jason Lenny включено в состав коммита
66
## Excluding certain jobs
Jason Lenny's avatar
Jason Lenny включено в состав коммита
67

Evan Read's avatar
Evan Read включено в состав коммита
68
69
The behavior of the `only: merge_requests` parameter is such that _only_ jobs with
that parameter are run in the context of a merge request; no other jobs will be run.
Jason Lenny's avatar
Jason Lenny включено в состав коммита
70

Evan Read's avatar
Evan Read включено в состав коммита
71
However, you may want to reverse this behavior, having all of your jobs to run _except_
Evan Read's avatar
Evan Read включено в состав коммита
72
73
74
75
for one or two.

Consider the following pipeline, with jobs `A`, `B`, and `C`. Imagine you want:

Evan Read's avatar
Evan Read включено в состав коммита
76
77
- All pipelines to always run `A` and `B`.
- `C` to run only for merge requests.
Evan Read's avatar
Evan Read включено в состав коммита
78
79

To achieve this, you can configure your `.gitlab-ci.yml` file as follows:
Jason Lenny's avatar
Jason Lenny включено в состав коммита
80
81
82
83
84
85
86
87
88
89
90
91

``` yaml
.only-default: &only-default
  only:
    - master
    - merge_requests
    - tags

A:
  <<: *only-default
  script:
    - ...
Evan Read's avatar
Evan Read включено в состав коммита
92

Jason Lenny's avatar
Jason Lenny включено в состав коммита
93
94
95
96
97
98
99
100
101
102
103
104
B:
  <<: *only-default
  script:
    - ...

C:
  script:
    - ...
  only:
    - merge_requests
```

Evan Read's avatar
Evan Read включено в состав коммита
105
Therefore:
Evan Read's avatar
Evan Read включено в состав коммита
106

Evan Read's avatar
Evan Read включено в состав коммита
107
108
- Since `A` and `B` are getting the `only:` rule to execute in all cases, they will always run.
- Since `C` specifies that it should only run for merge requests, it will not run for any pipeline
Evan Read's avatar
Evan Read включено в состав коммита
109
  except a merge request pipeline.
Jason Lenny's avatar
Jason Lenny включено в состав коммита
110
111

As you can see, this will help you avoid a lot of boilerplate where you'd need
Jason Lenny's avatar
Jason Lenny включено в состав коммита
112
to add that `only:` rule to all of your jobs in order to make them always run. You
Jason Lenny's avatar
Jason Lenny включено в состав коммита
113
114
115
can use this for scenarios like having only pipelines with merge requests get a
Review App set up, helping to save resources.

Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
116
117
118
119
120
121
122
## 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
Evan Read's avatar
Evan Read включено в состав коммита
123
   in the parent project.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
124
1. A pipeline runs on the merge request.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
125
1. A maintainer from the parent project checks the pipeline result, and merge
Evan Read's avatar
Evan Read включено в состав коммита
126
   into a target branch if the latest pipeline has passed.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
127
128
129
130
131
132
133

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
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
134
created in the parent project, but one of the biggest reasons is security concern.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
135
External users could steal secret variables from the parent project by modifying
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
136
`.gitlab-ci.yml`, which could be some sort of credentials. This should not happen.
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
137

Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
138
We're discussing a secure solution of running pipelines for merge requests
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
139
140
that submitted from forked projects,
see [the issue about the permission extension](https://gitlab.com/gitlab-org/gitlab-ce/issues/23902).