2019-05-30 02:23:46 -04:00
|
|
|
---
|
|
|
|
type: tutorial
|
|
|
|
---
|
|
|
|
|
2017-05-18 06:55:29 -04:00
|
|
|
# Triggering pipelines through the API
|
2015-12-21 08:53:08 -05:00
|
|
|
|
2017-05-18 06:55:29 -04:00
|
|
|
> **Notes**:
|
2018-09-06 12:52:18 -04:00
|
|
|
>
|
2018-10-14 18:55:12 -04:00
|
|
|
> - [Introduced](https://about.gitlab.com/2015/08/22/gitlab-7-14-released/) in GitLab 7.14.
|
2018-09-06 12:52:18 -04:00
|
|
|
> - GitLab 8.12 has a completely redesigned job permissions system. Read all
|
2019-03-10 21:47:01 -04:00
|
|
|
> about the [new model and its implications](../../user/project/new_ci_build_permissions_model.md#pipeline-triggers).
|
2016-09-22 08:45:35 -04:00
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
Triggers can be used to force a pipeline rerun of a specific `ref` (branch or
|
|
|
|
tag) with an API call.
|
2015-12-21 08:53:08 -05:00
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
## Authentication tokens
|
|
|
|
|
|
|
|
The following methods of authentication are supported.
|
|
|
|
|
|
|
|
### Trigger token
|
|
|
|
|
|
|
|
A unique trigger token can be obtained when [adding a new trigger](#adding-a-new-trigger).
|
|
|
|
|
2019-03-11 23:37:05 -04:00
|
|
|
DANGER: **Danger:**
|
|
|
|
Passing plain text tokens in public projects is a security issue. Potential
|
|
|
|
attackers can impersonate the user that exposed their trigger token publicly in
|
2019-04-08 08:32:38 -04:00
|
|
|
their `.gitlab-ci.yml` file. Use [variables](../variables/README.md#gitlab-cicd-environment-variables)
|
2019-03-11 23:37:05 -04:00
|
|
|
to protect trigger tokens.
|
|
|
|
|
2019-05-05 11:06:37 -04:00
|
|
|
### CI job token
|
|
|
|
|
|
|
|
You can use the `CI_JOB_TOKEN` [variable][predef] (used to authenticate
|
|
|
|
with the [GitLab Container Registry][registry]) in the following cases.
|
|
|
|
|
2019-07-08 04:50:38 -04:00
|
|
|
#### When used with multi-project pipelines **(PREMIUM)**
|
2019-05-05 11:06:37 -04:00
|
|
|
|
|
|
|
> **Note**:
|
|
|
|
The use of `CI_JOB_TOKEN` for multi-project pipelines was [introduced][ee-2017]
|
|
|
|
in [GitLab Premium][ee] 9.3.
|
|
|
|
|
|
|
|
This way of triggering can only be used when invoked inside `.gitlab-ci.yml`,
|
|
|
|
and it creates a dependent pipeline relation visible on the
|
|
|
|
[pipeline graph](../multi_project_pipelines.md#overview). For example:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
build_docs:
|
|
|
|
stage: deploy
|
|
|
|
script:
|
|
|
|
- curl --request POST --form "token=$CI_JOB_TOKEN" --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/pipeline
|
|
|
|
only:
|
|
|
|
- tags
|
|
|
|
```
|
|
|
|
|
|
|
|
Pipelines triggered that way also expose a special variable:
|
|
|
|
`CI_PIPELINE_SOURCE=pipeline`.
|
|
|
|
|
|
|
|
Read more about the [pipelines trigger API][trigapi].
|
|
|
|
|
2019-07-08 04:50:38 -04:00
|
|
|
#### When a pipeline depends on the artifacts of another pipeline **(PREMIUM)**
|
2019-05-05 11:06:37 -04:00
|
|
|
|
2019-08-28 02:06:34 -04:00
|
|
|
> The use of `CI_JOB_TOKEN` in the artifacts download API was [introduced][ee-2346] in [GitLab Premium][ee] 9.5.
|
2019-05-05 11:06:37 -04:00
|
|
|
|
|
|
|
With the introduction of dependencies between different projects, one of
|
|
|
|
them may need to access artifacts created by a previous one. This process
|
|
|
|
must be granted for authorized accesses, and it can be done using the
|
|
|
|
`CI_JOB_TOKEN` variable that identifies a specific job. For example:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
build_submodule:
|
|
|
|
image: debian
|
|
|
|
stage: test
|
|
|
|
script:
|
|
|
|
- apt update && apt install -y unzip
|
|
|
|
- curl --location --output artifacts.zip "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/master/download?job=test&job_token=$CI_JOB_TOKEN"
|
|
|
|
- unzip artifacts.zip
|
|
|
|
only:
|
|
|
|
- tags
|
|
|
|
```
|
|
|
|
|
|
|
|
This allows you to use that for multi-project pipelines and download artifacts
|
|
|
|
from any project to which you have access as this follows the same principles
|
|
|
|
with the [permission model][permissions].
|
|
|
|
|
|
|
|
Read more about the [jobs API](../../api/jobs.md#download-the-artifacts-archive).
|
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
## Adding a new trigger
|
2015-12-21 08:53:08 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
You can add a new trigger by going to your project's
|
2017-09-09 09:44:14 -04:00
|
|
|
**Settings ➔ CI/CD** under **Triggers**. The **Add trigger** button will
|
2017-02-13 11:59:57 -05:00
|
|
|
create a new token which you can then use to trigger a rerun of this
|
|
|
|
particular project's pipeline.
|
2015-12-24 18:29:27 -05:00
|
|
|
|
|
|
|
Every new trigger you create, gets assigned a different token which you can
|
|
|
|
then use inside your scripts or `.gitlab-ci.yml`. You also have a nice
|
|
|
|
overview of the time the triggers were last used.
|
|
|
|
|
|
|
|
![Triggers page overview](img/triggers_page.png)
|
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
## Revoking a trigger
|
2015-12-24 18:29:27 -05:00
|
|
|
|
|
|
|
You can revoke a trigger any time by going at your project's
|
2017-09-09 09:44:14 -04:00
|
|
|
**Settings ➔ CI/CD** under **Triggers** and hitting the **Revoke** button.
|
2017-06-08 05:59:43 -04:00
|
|
|
The action is irreversible.
|
2015-12-24 18:29:27 -05:00
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
## Triggering a pipeline
|
2015-12-24 18:29:27 -05:00
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
> **Notes**:
|
2018-09-06 12:52:18 -04:00
|
|
|
>
|
|
|
|
> - Valid refs are only the branches and tags. If you pass a commit SHA as a ref,
|
|
|
|
> it will not trigger a job.
|
2016-12-01 06:35:24 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
To trigger a job you need to send a `POST` request to GitLab's API endpoint:
|
2015-12-24 18:29:27 -05:00
|
|
|
|
|
|
|
```
|
2017-03-05 14:58:08 -05:00
|
|
|
POST /projects/:id/trigger/pipeline
|
2015-12-24 18:29:27 -05:00
|
|
|
```
|
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
The required parameters are the [trigger's `token`](#authentication-tokens)
|
|
|
|
and the Git `ref` on which the trigger will be performed. Valid refs are the
|
|
|
|
branch and the tag. The `:id` of a project can be found by
|
2017-09-09 09:44:14 -04:00
|
|
|
[querying the API](../../api/projects.md) or by visiting the **CI/CD**
|
2017-06-08 05:59:43 -04:00
|
|
|
settings page which provides self-explanatory examples.
|
2015-12-24 18:29:27 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
When a rerun of a pipeline is triggered, the information is exposed in GitLab's
|
|
|
|
UI under the **Jobs** page and the jobs are marked as triggered 'by API'.
|
2015-12-24 18:29:27 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
![Marked rebuilds as on jobs page](img/builds_page.png)
|
2015-12-24 18:29:27 -05:00
|
|
|
|
|
|
|
---
|
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
You can see which trigger caused the rebuild by visiting the single job page.
|
|
|
|
A part of the trigger's token is exposed in the UI as you can see from the image
|
2015-12-24 18:29:27 -05:00
|
|
|
below.
|
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
![Marked rebuilds as triggered on a single job page](img/trigger_single_build.png)
|
2015-12-24 18:29:27 -05:00
|
|
|
|
|
|
|
---
|
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
By using cURL you can trigger a pipeline rerun with minimal effort, for example:
|
2015-12-24 18:29:27 -05:00
|
|
|
|
|
|
|
```bash
|
2016-08-08 03:47:17 -04:00
|
|
|
curl --request POST \
|
|
|
|
--form token=TOKEN \
|
|
|
|
--form ref=master \
|
2017-03-05 14:58:08 -05:00
|
|
|
https://gitlab.example.com/api/v4/projects/9/trigger/pipeline
|
2015-12-24 18:29:27 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
In this case, the project with ID `9` will get rebuilt on `master` branch.
|
|
|
|
|
2016-04-27 10:41:37 -04:00
|
|
|
Alternatively, you can pass the `token` and `ref` arguments in the query string:
|
|
|
|
|
|
|
|
```bash
|
2016-08-08 03:47:17 -04:00
|
|
|
curl --request POST \
|
2017-03-05 14:58:08 -05:00
|
|
|
"https://gitlab.example.com/api/v4/projects/9/trigger/pipeline?token=TOKEN&ref=master"
|
2016-04-27 10:41:37 -04:00
|
|
|
```
|
2015-12-25 06:37:46 -05:00
|
|
|
|
|
|
|
You can also benefit by using triggers in your `.gitlab-ci.yml`. Let's say that
|
|
|
|
you have two projects, A and B, and you want to trigger a rebuild on the `master`
|
|
|
|
branch of project B whenever a tag on project A is created. This is the job you
|
|
|
|
need to add in project's A `.gitlab-ci.yml`:
|
2015-12-24 18:29:27 -05:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
build_docs:
|
|
|
|
stage: deploy
|
|
|
|
script:
|
2017-03-05 14:58:08 -05:00
|
|
|
- "curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/pipeline"
|
2015-12-24 18:29:27 -05:00
|
|
|
only:
|
|
|
|
- tags
|
|
|
|
```
|
2015-12-21 08:53:08 -05:00
|
|
|
|
2017-02-13 11:59:57 -05:00
|
|
|
Now, whenever a new tag is pushed on project A, the job will run and the
|
2015-12-25 06:37:46 -05:00
|
|
|
`build_docs` job will be executed, triggering a rebuild of project B. The
|
|
|
|
`stage: deploy` ensures that this job will run only after all jobs with
|
|
|
|
`stage: test` complete successfully.
|
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
## Triggering a pipeline from a webhook
|
2015-12-25 06:37:46 -05:00
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
> **Notes**:
|
2018-09-06 12:52:18 -04:00
|
|
|
>
|
|
|
|
> - Introduced in GitLab 8.14.
|
|
|
|
> - `ref` should be passed as part of the URL in order to take precedence over
|
|
|
|
> `ref` from the webhook body that designates the branch ref that fired the
|
|
|
|
> trigger in the source repository.
|
|
|
|
> - `ref` should be URL-encoded if it contains slashes.
|
2015-12-25 06:37:46 -05:00
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
To trigger a job from a webhook of another project you need to add the following
|
|
|
|
webhook URL for Push and Tag events (change the project ID, ref and token):
|
|
|
|
|
|
|
|
```
|
|
|
|
https://gitlab.example.com/api/v4/projects/9/ref/master/trigger/pipeline?token=TOKEN
|
|
|
|
```
|
|
|
|
|
|
|
|
## Making use of trigger variables
|
|
|
|
|
|
|
|
You can pass any number of arbitrary variables in the trigger API call and they
|
|
|
|
will be available in GitLab CI so that they can be used in your `.gitlab-ci.yml`
|
|
|
|
file. The parameter is of the form:
|
|
|
|
|
|
|
|
```
|
|
|
|
variables[key]=value
|
|
|
|
```
|
|
|
|
|
2018-12-07 08:31:53 -05:00
|
|
|
This information is also exposed in the UI. Please note that _values_ are only viewable by Owners and Maintainers.
|
2017-06-08 05:59:43 -04:00
|
|
|
|
|
|
|
![Job variables in UI](img/trigger_variables.png)
|
|
|
|
|
|
|
|
Using trigger variables can be proven useful for a variety of reasons:
|
2015-12-25 06:37:46 -05:00
|
|
|
|
2018-10-10 23:11:32 -04:00
|
|
|
- Identifiable jobs. Since the variable is exposed in the UI you can know
|
2015-12-25 06:37:46 -05:00
|
|
|
why the rebuild was triggered if you pass a variable that explains the
|
|
|
|
purpose.
|
2018-10-10 23:11:32 -04:00
|
|
|
- Conditional job processing. You can have conditional jobs that run whenever
|
2015-12-25 06:37:46 -05:00
|
|
|
a certain variable is present.
|
|
|
|
|
|
|
|
Consider the following `.gitlab-ci.yml` where we set three
|
|
|
|
[stages](../yaml/README.md#stages) and the `upload_package` job is run only
|
|
|
|
when all jobs from the test and build stages pass. When the `UPLOAD_TO_S3`
|
|
|
|
variable is non-zero, `make upload` is run.
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
stages:
|
|
|
|
- test
|
|
|
|
- build
|
|
|
|
- package
|
|
|
|
|
|
|
|
run_tests:
|
2018-12-08 02:45:46 -05:00
|
|
|
stage: test
|
2015-12-25 06:37:46 -05:00
|
|
|
script:
|
|
|
|
- make test
|
|
|
|
|
|
|
|
build_package:
|
|
|
|
stage: build
|
|
|
|
script:
|
|
|
|
- make build
|
|
|
|
|
|
|
|
upload_package:
|
|
|
|
stage: package
|
|
|
|
script:
|
|
|
|
- if [ -n "${UPLOAD_TO_S3}" ]; then make upload; fi
|
|
|
|
```
|
|
|
|
|
|
|
|
You can then trigger a rebuild while you pass the `UPLOAD_TO_S3` variable
|
|
|
|
and the script of the `upload_package` job will run:
|
|
|
|
|
|
|
|
```bash
|
2016-08-08 03:47:17 -04:00
|
|
|
curl --request POST \
|
|
|
|
--form token=TOKEN \
|
|
|
|
--form ref=master \
|
|
|
|
--form "variables[UPLOAD_TO_S3]=true" \
|
2017-03-05 14:58:08 -05:00
|
|
|
https://gitlab.example.com/api/v4/projects/9/trigger/pipeline
|
2015-12-25 06:37:46 -05:00
|
|
|
```
|
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
## Using cron to trigger nightly pipelines
|
2017-05-18 06:55:29 -04:00
|
|
|
|
|
|
|
>**Note:**
|
|
|
|
The following behavior can also be achieved through GitLab's UI with
|
|
|
|
[pipeline schedules](../../user/project/pipelines/schedules.md).
|
|
|
|
|
|
|
|
Whether you craft a script or just run cURL directly, you can trigger jobs
|
|
|
|
in conjunction with cron. The example below triggers a job on the `master`
|
|
|
|
branch of project with ID `9` every night at `00:30`:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
30 0 * * * curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/pipeline
|
|
|
|
```
|
|
|
|
|
2017-06-08 05:59:43 -04:00
|
|
|
## Legacy triggers
|
|
|
|
|
|
|
|
Old triggers, created before GitLab 9.0 will be marked as legacy.
|
|
|
|
|
|
|
|
Triggers with the legacy label do not have an associated user and only have
|
|
|
|
access to the current project. They are considered deprecated and will be
|
2019-08-28 02:06:34 -04:00
|
|
|
removed with one of the future versions of GitLab.
|
2017-06-08 05:59:43 -04:00
|
|
|
|
|
|
|
[ee-2017]: https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/2017
|
2019-05-05 11:06:37 -04:00
|
|
|
[ee-2346]: https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/2346
|
2018-07-02 20:37:24 -04:00
|
|
|
[ee]: https://about.gitlab.com/pricing/
|
2017-06-08 05:59:43 -04:00
|
|
|
[variables]: ../variables/README.md
|
2019-01-30 05:20:50 -05:00
|
|
|
[predef]: ../variables/README.md#predefined-environment-variables
|
2019-09-17 08:06:48 -04:00
|
|
|
[registry]: ../../user/packages/container_registry/index.md
|
2019-05-05 11:06:37 -04:00
|
|
|
[permissions]: ../../user/permissions.md#job-permissions
|
|
|
|
[trigapi]: ../../api/pipeline_triggers.md
|