2015-12-21 13:53:08 +00:00
|
|
|
# Triggering Builds through the API
|
|
|
|
|
2016-08-08 08:09:54 +00:00
|
|
|
> [Introduced][ci-229] in GitLab CE 7.14.
|
2015-12-21 13:53:08 +00:00
|
|
|
|
2016-09-22 12:45:35 +00:00
|
|
|
> **Note**:
|
|
|
|
GitLab 8.12 has a completely redesigned build permissions system.
|
2016-09-30 11:11:04 +00:00
|
|
|
Read all about the [new model and its implications](../../user/project/new_ci_build_permissions_model.md#build-triggers).
|
2016-09-22 12:45:35 +00:00
|
|
|
|
2016-12-01 11:35:24 +00:00
|
|
|
Triggers can be used to force a rebuild of a specific `ref` (branch or tag)
|
2015-12-24 23:29:27 +00:00
|
|
|
with an API call.
|
2015-12-21 13:53:08 +00:00
|
|
|
|
|
|
|
## Add a trigger
|
|
|
|
|
2015-12-24 23:29:27 +00:00
|
|
|
You can add a new trigger by going to your project's **Settings > Triggers**.
|
|
|
|
The **Add trigger** button will create a new token which you can then use to
|
|
|
|
trigger a rebuild of this particular project.
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
## Revoke a trigger
|
|
|
|
|
|
|
|
You can revoke a trigger any time by going at your project's
|
|
|
|
**Settings > Triggers** and hitting the **Revoke** button. The action is
|
|
|
|
irreversible.
|
|
|
|
|
|
|
|
## Trigger a build
|
|
|
|
|
2016-12-01 11:35:24 +00:00
|
|
|
> **Note**:
|
|
|
|
Valid refs are only the branches and tags. If you pass a commit SHA as a ref,
|
|
|
|
it will not trigger a build.
|
|
|
|
|
2015-12-24 23:29:27 +00:00
|
|
|
To trigger a build you need to send a `POST` request to GitLab's API endpoint:
|
|
|
|
|
|
|
|
```
|
|
|
|
POST /projects/:id/trigger/builds
|
|
|
|
```
|
|
|
|
|
|
|
|
The required parameters are the trigger's `token` and the Git `ref` on which
|
2016-12-01 11:35:24 +00:00
|
|
|
the trigger will be performed. Valid refs are the branch and the tag. The `:id`
|
|
|
|
of a project can be found by [querying the API](../../api/projects.md)
|
2015-12-24 23:29:27 +00:00
|
|
|
or by visiting the **Triggers** page which provides self-explanatory examples.
|
|
|
|
|
|
|
|
When a rebuild is triggered, the information is exposed in GitLab's UI under
|
|
|
|
the **Builds** page and the builds are marked as `triggered`.
|
|
|
|
|
|
|
|
![Marked rebuilds as triggered on builds page](img/builds_page.png)
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
You can see which trigger caused the rebuild by visiting the single build page.
|
|
|
|
The token of the trigger is exposed in the UI as you can see from the image
|
|
|
|
below.
|
|
|
|
|
|
|
|
![Marked rebuilds as triggered on a single build page](img/trigger_single_build.png)
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
See the [Examples](#examples) section for more details on how to actually
|
|
|
|
trigger a rebuild.
|
|
|
|
|
2016-11-07 17:11:54 +00:00
|
|
|
## Trigger a build from webhook
|
|
|
|
|
|
|
|
> Introduced in GitLab 8.14.
|
|
|
|
|
|
|
|
To trigger a build from webhook of another project you need to add the following
|
|
|
|
webhook url for Push and Tag push events:
|
|
|
|
|
|
|
|
```
|
|
|
|
https://gitlab.example.com/api/v3/projects/:id/ref/:ref/trigger/builds?token=TOKEN
|
|
|
|
```
|
|
|
|
|
|
|
|
> **Note**:
|
|
|
|
- `ref` should be passed as part of url in order to take precedence over `ref`
|
|
|
|
from webhook body that designates the branchref that fired the trigger in the source repository.
|
|
|
|
- `ref` should be url encoded if contains slashes.
|
|
|
|
|
2015-12-24 23:29:27 +00:00
|
|
|
## Pass build variables to a trigger
|
|
|
|
|
|
|
|
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
|
|
|
|
```
|
|
|
|
|
|
|
|
This information is also exposed in the UI.
|
|
|
|
|
|
|
|
![Build variables in UI](img/trigger_variables.png)
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
See the [Examples](#examples) section below for more details.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
Using cURL you can trigger a rebuild with minimal effort, for example:
|
|
|
|
|
|
|
|
```bash
|
2016-08-08 07:47:17 +00:00
|
|
|
curl --request POST \
|
|
|
|
--form token=TOKEN \
|
|
|
|
--form ref=master \
|
2015-12-25 11:37:46 +00:00
|
|
|
https://gitlab.example.com/api/v3/projects/9/trigger/builds
|
2015-12-24 23:29:27 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
In this case, the project with ID `9` will get rebuilt on `master` branch.
|
|
|
|
|
2016-04-27 14:41:37 +00:00
|
|
|
Alternatively, you can pass the `token` and `ref` arguments in the query string:
|
|
|
|
|
|
|
|
```bash
|
2016-08-08 07:47:17 +00:00
|
|
|
curl --request POST \
|
2016-04-27 14:41:37 +00:00
|
|
|
"https://gitlab.example.com/api/v3/projects/9/trigger/builds?token=TOKEN&ref=master"
|
|
|
|
```
|
2015-12-25 11:37:46 +00:00
|
|
|
|
|
|
|
### Triggering a build within `.gitlab-ci.yml`
|
|
|
|
|
|
|
|
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 23:29:27 +00:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
build_docs:
|
|
|
|
stage: deploy
|
|
|
|
script:
|
2016-08-08 07:47:17 +00:00
|
|
|
- "curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v3/projects/9/trigger/builds"
|
2015-12-24 23:29:27 +00:00
|
|
|
only:
|
|
|
|
- tags
|
|
|
|
```
|
2015-12-21 13:53:08 +00:00
|
|
|
|
2015-12-25 11:37:46 +00:00
|
|
|
Now, whenever a new tag is pushed on project A, the build will run and the
|
|
|
|
`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.
|
|
|
|
|
|
|
|
_**Note:** If your project is public, passing the token in plain text is
|
2015-12-26 11:56:33 +00:00
|
|
|
probably not the wisest idea, so you might want to use a
|
2015-12-25 11:37:46 +00:00
|
|
|
[secure variable](../variables/README.md#user-defined-variables-secure-variables)
|
|
|
|
for that purpose._
|
|
|
|
|
|
|
|
### Making use of trigger variables
|
|
|
|
|
|
|
|
Using trigger variables can be proven useful for a variety of reasons.
|
|
|
|
|
|
|
|
* Identifiable jobs. Since the variable is exposed in the UI you can know
|
|
|
|
why the rebuild was triggered if you pass a variable that explains the
|
|
|
|
purpose.
|
|
|
|
* Conditional job processing. You can have conditional jobs that run whenever
|
|
|
|
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:
|
|
|
|
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 07:47:17 +00:00
|
|
|
curl --request POST \
|
|
|
|
--form token=TOKEN \
|
|
|
|
--form ref=master \
|
|
|
|
--form "variables[UPLOAD_TO_S3]=true" \
|
2015-12-25 11:37:46 +00:00
|
|
|
https://gitlab.example.com/api/v3/projects/9/trigger/builds
|
|
|
|
```
|
|
|
|
|
2016-11-07 17:11:54 +00:00
|
|
|
### Using webhook to trigger builds
|
|
|
|
|
|
|
|
You can add the following webhook to another project in order to trigger a build:
|
|
|
|
|
|
|
|
```
|
|
|
|
https://gitlab.example.com/api/v3/projects/9/ref/master/trigger/builds?token=TOKEN&variables[UPLOAD_TO_S3]=true
|
|
|
|
```
|
|
|
|
|
2015-12-25 11:37:46 +00:00
|
|
|
### Using cron to trigger nightly builds
|
|
|
|
|
|
|
|
Whether you craft a script or just run cURL directly, you can trigger builds
|
|
|
|
in conjunction with cron. The example below triggers a build on the `master`
|
|
|
|
branch of project with ID `9` every night at `00:30`:
|
|
|
|
|
|
|
|
```bash
|
2016-08-08 07:47:17 +00:00
|
|
|
30 0 * * * curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v3/projects/9/trigger/builds
|
2015-12-25 11:37:46 +00:00
|
|
|
```
|
|
|
|
|
2015-12-21 13:53:08 +00:00
|
|
|
[ci-229]: https://gitlab.com/gitlab-org/gitlab-ci/merge_requests/229
|