Skip to content

Commit de05dae

Browse files
author
Gauvain Pocentek
committed
Add support for triggering a new build
Fixes #184
1 parent f5f734e commit de05dae

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

docs/gl_objects/builds.py

+6
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,9 @@ def __call__(self, chunk):
110110
# play
111111
build.play()
112112
# end play
113+
114+
# trigger run
115+
p = gl.projects.get(project_id)
116+
p.trigger_build('master', trigger_token,
117+
{'extra_var1': 'foo', 'extra_var2': 'bar'})
118+
# end trigger run

docs/gl_objects/builds.rst

+26-11
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ Builds
55
Build triggers
66
==============
77

8-
Use :class:`~gitlab.objects.ProjectTrigger` objects to manipulate build
9-
triggers. The :attr:`gitlab.Gitlab.project_triggers` and
10-
:attr:`gitlab.objects.Project.triggers` manager objects provide helper
11-
functions.
8+
Build triggers provide a way to interact with the GitLab CI. Using a trigger a
9+
user or an application can run a new build for a specific commit.
10+
11+
* Object class: :class:`~gitlab.objects.ProjectTrigger`
12+
* Manager objects: :attr:`gitlab.Gitlab.project_triggers`,
13+
:attr:`Project.triggers <gitlab.objects.Project.triggers>`
1214

1315
Examples
1416
--------
@@ -40,10 +42,11 @@ Remove a trigger:
4042
Build variables
4143
===============
4244

43-
Use :class:`~gitlab.objects.ProjectVariable` objects to manipulate build
44-
variables. The :attr:`gitlab.Gitlab.project_variables` and
45-
:attr:`gitlab.objects.Project.variables` manager objects provide helper
46-
functions.
45+
You can associate variables to builds to modify the build script behavior.
46+
47+
* Object class: :class:`~gitlab.objects.ProjectVariable`
48+
* Manager objects: :attr:`gitlab.Gitlab.project_variables`,
49+
:attr:`gitlab.objects.Project.variables`
4750

4851
Examples
4952
--------
@@ -81,13 +84,25 @@ Remove a variable:
8184
Builds
8285
======
8386

84-
Use :class:`~gitlab.objects.ProjectBuild` objects to manipulate builds. The
85-
:attr:`gitlab.Gitlab.project_builds` and :attr:`gitlab.objects.Project.builds`
86-
manager objects provide helper functions.
87+
Builds are associated to projects and commits. They provide information on the
88+
build that have been run, and methods to manipulate those builds.
89+
90+
* Object class: :class:`~gitlab.objects.ProjectBuild`
91+
* Manager objects: :attr:`gitlab.Gitlab.project_builds`,
92+
:attr:`gitlab.objects.Project.builds`
8793

8894
Examples
8995
--------
9096

97+
Build are usually automatically triggered, but you can explicitly trigger a
98+
new build:
99+
100+
Trigger a new build on a project:
101+
102+
.. literalinclude:: builds.py
103+
:start-after: # trigger run
104+
:end-before: # end trigger run
105+
91106
List builds for the project:
92107

93108
.. literalinclude:: builds.py

gitlab/objects.py

+21
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,27 @@ def share(self, group_id, group_access, **kwargs):
23862386
r = self.gitlab._raw_post(url, data=data, **kwargs)
23872387
raise_error_from_response(r, GitlabCreateError, 201)
23882388

2389+
def trigger_build(self, ref, token, variables={}, **kwargs):
2390+
"""Trigger a CI build.
2391+
2392+
See https://gitlab.com/help/ci/triggers/README.md#trigger-a-build
2393+
2394+
Args:
2395+
ref (str): Commit to build; can be a commit SHA, a branch name, ...
2396+
token (str): The trigger token
2397+
variables (dict): Variables passed to the build script
2398+
2399+
Raises:
2400+
GitlabConnectionError: If the server cannot be reached.
2401+
GitlabCreateError: If the server fails to perform the request.
2402+
"""
2403+
url = "/projects/%s/trigger/builds" % self.id
2404+
form = {r'variables[%s]' % k: v for k, v in six.iteritems(variables)}
2405+
data = {'ref': ref, 'token': token}
2406+
data.update(form)
2407+
r = self.gitlab._raw_post(url, data=data, **kwargs)
2408+
raise_error_from_response(r, GitlabCreateError, 201)
2409+
23892410

23902411
class Runner(GitlabObject):
23912412
_url = '/runners'

0 commit comments

Comments
 (0)