Commit 46eb0ebf authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '23584-triggering-builds-from-webhooks' into 'master'

Add ref parameter for triggering builds with gitlab webhook from other project.

Currently GitLab can trigger builds from external tools like curl.
But there is no way to trigger builds with webhook from another GitLab project.
Executing webhook like `/projects/:id/trigger/builds?token=TOKEN&ref=master` results in such message from server:
```
400 No builds created
```

Problem is the ref from request body takes precedence of the ref from query string.
System tries to trigger build for nonexistent branch instead of `master`.
This is the feature of the Grape framework used to build GitLab api.

This MR adds optional `ref` parameter into the api url:
```
/projects/:id/ref/:ref/trigger/builds?token=TOKEN
```

This will lead that the ref in the url will have precedence of the ref inside request body.

This solution provides compatibility with current API urls.

Closes #23584

See merge request !7022
parents d5cd0d67 ff8194e0
module TriggersHelper module TriggersHelper
def builds_trigger_url(project_id) def builds_trigger_url(project_id, ref: nil)
"#{Settings.gitlab.url}/api/v3/projects/#{project_id}/trigger/builds" if ref.nil?
"#{Settings.gitlab.url}/api/v3/projects/#{project_id}/trigger/builds"
else
"#{Settings.gitlab.url}/api/v3/projects/#{project_id}/ref/#{ref}/trigger/builds"
end
end end
end end
...@@ -75,6 +75,16 @@ ...@@ -75,6 +75,16 @@
stage: deploy stage: deploy
script: script:
- "curl -X POST -F token=TOKEN -F ref=REF_NAME #{builds_trigger_url(@project.id)}" - "curl -X POST -F token=TOKEN -F ref=REF_NAME #{builds_trigger_url(@project.id)}"
%h5.prepend-top-default
Use webhook
%p.light
Add the following webhook to another project for Push and Tag push events.
The project will be rebuilt at the corresponding event.
%pre
:plain
#{builds_trigger_url(@project.id, ref: 'REF_NAME')}?token=TOKEN
%h5.prepend-top-default %h5.prepend-top-default
Pass build variables Pass build variables
...@@ -83,10 +93,18 @@ ...@@ -83,10 +93,18 @@
%code variables[VARIABLE]=VALUE %code variables[VARIABLE]=VALUE
to an API request. Variable values can be used to distinguish between triggered builds and normal builds. to an API request. Variable values can be used to distinguish between triggered builds and normal builds.
%pre.append-bottom-0 With cURL:
%pre
:plain :plain
curl -X POST \ curl -X POST \
-F token=TOKEN \ -F token=TOKEN \
-F "ref=REF_NAME" \ -F "ref=REF_NAME" \
-F "variables[RUN_NIGHTLY_BUILD]=true" \ -F "variables[RUN_NIGHTLY_BUILD]=true" \
#{builds_trigger_url(@project.id)} #{builds_trigger_url(@project.id)}
%p.light
With webhook:
%pre.append-bottom-0
:plain
#{builds_trigger_url(@project.id, ref: 'REF_NAME')}?token=TOKEN&variables[RUN_NIGHTLY_BUILD]=true
---
title: Make it possible to trigger builds from webhooks
merge_request: 7022
author: Dmitry Poray
...@@ -58,6 +58,22 @@ below. ...@@ -58,6 +58,22 @@ below.
See the [Examples](#examples) section for more details on how to actually See the [Examples](#examples) section for more details on how to actually
trigger a rebuild. trigger a rebuild.
## 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.
## Pass build variables to a trigger ## Pass build variables to a trigger
You can pass any number of arbitrary variables in the trigger API call and they You can pass any number of arbitrary variables in the trigger API call and they
...@@ -169,6 +185,14 @@ curl --request POST \ ...@@ -169,6 +185,14 @@ curl --request POST \
https://gitlab.example.com/api/v3/projects/9/trigger/builds https://gitlab.example.com/api/v3/projects/9/trigger/builds
``` ```
### 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
```
### Using cron to trigger nightly builds ### Using cron to trigger nightly builds
Whether you craft a script or just run cURL directly, you can trigger builds Whether you craft a script or just run cURL directly, you can trigger builds
......
...@@ -12,7 +12,7 @@ module API ...@@ -12,7 +12,7 @@ module API
requires :token, type: String, desc: 'The unique token of trigger' requires :token, type: String, desc: 'The unique token of trigger'
optional :variables, type: Hash, desc: 'The list of variables to be injected into build' optional :variables, type: Hash, desc: 'The list of variables to be injected into build'
end end
post ":id/trigger/builds" do post ":id/(ref/:ref/)trigger/builds" do
project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id]) project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
trigger = Ci::Trigger.find_by_token(params[:token].to_s) trigger = Ci::Trigger.find_by_token(params[:token].to_s)
not_found! unless project && trigger not_found! unless project && trigger
......
...@@ -54,6 +54,13 @@ describe API::API do ...@@ -54,6 +54,13 @@ describe API::API do
expect(pipeline.builds.size).to eq(5) expect(pipeline.builds.size).to eq(5)
end end
it 'creates builds on webhook from other gitlab repository and branch' do
expect do
post api("/projects/#{project.id}/ref/master/trigger/builds?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
end.to change(project.builds, :count).by(5)
expect(response).to have_http_status(201)
end
it 'returns bad request with no builds created if there\'s no commit for that ref' do it 'returns bad request with no builds created if there\'s no commit for that ref' do
post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'other-branch') post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'other-branch')
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment