blob: 6962b8f66ae4198d81f9792109c6b55c0592def2 [file] [log] [blame]
James E. Blaireff5a9d2017-06-20 00:00:37 -07001:title: Job Content
2
3Job Content
4===========
5
David Shrewsburyc50cb572017-08-04 11:55:01 -04006Zuul jobs are implemented as Ansible playbooks. Zuul prepares the
James E. Blaireff5a9d2017-06-20 00:00:37 -07007repositories used for a job, installs any required Ansible roles, and
8then executes the job's playbooks. Any setup or artifact collection
9required is the responsibility of the job itself. While this flexible
10arrangement allows for almost any kind of job to be run by Zuul,
11batteries are included. Zuul has a standard library of jobs upon
12which to build.
13
14Working Directory
15-----------------
16
17Before starting each job, the Zuul executor creates a directory to
18hold all of the content related to the job. This includes some
19directories which are used by Zuul to configure and run Ansible and
20may not be accessible, as well as a directory tree, under ``work/``,
21that is readable and writable by the job. The hierarchy is:
22
23**work/**
24 The working directory of the job.
25
26**work/src/**
27 Contains the prepared git repositories for the job.
28
29**work/logs/**
30 Where the Ansible log for the job is written; your job
31 may place other logs here as well.
32
33Git Repositories
34----------------
35
36The git repositories in ``work/src`` contain the repositories for all
37of the projects specified in the ``required-projects`` section of the
38job, plus the project associated with the queue item if it isn't
39already in that list. In the case of a proposed change, that change
40and all of the changes ahead of it in the pipeline queue will already
41be merged into their respective repositories and target branches. The
42change's project will have the change's branch checked out, as will
43all of the other projects, if that branch exists (otherwise, a
44fallback or default branch will be used). If your job needs to
45operate on multiple branches, simply checkout the appropriate branches
46of these git repos to ensure that the job results reflect the proposed
47future state that Zuul is testing, and all dependencies are present.
48Do not use any git remotes; the local repositories are guaranteed to
49be up to date.
50
James E. Blair4d5dd252017-06-23 21:40:56 +010051The repositories will be placed on the filesystem in directories
52corresponding with the canonical hostname of their source connection.
53For example::
54
55 work/src/git.example.com/project1
56 work/src/github.com/project2
57
58Is the layout that would be present for a job which included project1
59from the connection associated to git.example.com and project2 from
60GitHub. This helps avoid collisions between projects with the same
61name, and some language environments, such as Go, expect repositories
62in this format.
63
James E. Blaireff5a9d2017-06-20 00:00:37 -070064Note that these git repositories are located on the executor; in order
65to be useful to most kinds of jobs, they will need to be present on
66the test nodes. The ``base`` job in the standard library contains a
67pre-playbook which copies the repositories to all of the job's nodes.
68It is recommended to always inherit from this base job to ensure that
69behavior.
70
71.. TODO: link to base job documentation and/or document src (and logs?) directory
72
James E. Blair28c8e3b2017-07-17 16:27:50 -070073Variables
74---------
75
Jamie Lennox7655b552017-03-17 12:33:38 +110076There are several sources of variables which are available to Ansible:
77variables defined in jobs, secrets, and site-wide variables. The
78order of precedence is:
79
80* Site-wide variables
81
82* Secrets
83
84* Job variables
85
86Meaning that a site-wide variable with the same name as any other will
87override its value, and similarly, secrets override job variables of
88the same name. Each of the three sources is described below.
89
90
91Job Variables
92~~~~~~~~~~~~~
93
James E. Blaird9f0efb2017-08-02 16:07:44 -070094Any variables specified in the job definition (using the
95:attr:`job.vars` attribute) are available as Ansible host variables.
96They are added to the ``vars`` section of the inventory file under the
97``all`` hosts group, so they are available to all hosts. Simply refer
98to them by the name specified in the job's ``vars`` section.
James E. Blair28c8e3b2017-07-17 16:27:50 -070099
100Secrets
101~~~~~~~
102
James E. Blaird9f0efb2017-08-02 16:07:44 -0700103:ref:`Secrets <secret>` also appear as variables available to Ansible.
104Unlike job variables, these are not added to the inventory file (so
105that the inventory file may be kept for debugging purposes without
106revealing secrets). But they are still available to Ansible as normal
James E. Blair28c8e3b2017-07-17 16:27:50 -0700107variables. Because secrets are groups of variables, they will appear
108as a dictionary structure in templates, with the dictionary itself
109being the name of the secret, and its members the individual items in
James E. Blaird9f0efb2017-08-02 16:07:44 -0700110the secret. For example, a secret defined as:
111
112.. code-block:: yaml
James E. Blair28c8e3b2017-07-17 16:27:50 -0700113
114 - secret:
115 name: credentials
116 data:
117 username: foo
118 password: bar
119
120Might be used in a template as::
121
122 {{ credentials.username }} {{ credentials.password }}
123
James E. Blair892cca62017-08-09 11:36:58 -0700124Secrets are only available to playbooks associated with the job
125definition which uses the secret; they are not available to playbooks
126associated with child jobs or job variants.
James E. Blair28c8e3b2017-07-17 16:27:50 -0700127
James E. Blaireff5a9d2017-06-20 00:00:37 -0700128Zuul Variables
James E. Blair28c8e3b2017-07-17 16:27:50 -0700129~~~~~~~~~~~~~~
James E. Blaireff5a9d2017-06-20 00:00:37 -0700130
131Zuul supplies not only the variables specified by the job definition
James E. Blaird9f0efb2017-08-02 16:07:44 -0700132to Ansible, but also some variables from Zuul itself.
James E. Blair21037782017-07-19 11:56:55 -0700133
James E. Blairbabefce2017-07-20 17:14:54 -0700134When a pipeline is triggered by an action, it enqueues items which may
James E. Blair21037782017-07-19 11:56:55 -0700135vary based on the pipeline's configuration. For example, when a new
136change is created, that change may be enqueued into the pipeline,
137while a tag may be enqueued into the pipeline when it is pushed.
138
139Information about these items is available to jobs. All of the items
140enqueued in a pipeline are git references, and therefore share some
141attributes in common. But other attributes may vary based on the type
142of item.
143
James E. Blaird9f0efb2017-08-02 16:07:44 -0700144.. var:: zuul
James E. Blair21037782017-07-19 11:56:55 -0700145
James E. Blaird9f0efb2017-08-02 16:07:44 -0700146 All items provide the following information as Ansible variables
147 under the ``zuul`` key:
James E. Blair21037782017-07-19 11:56:55 -0700148
James E. Blaird9f0efb2017-08-02 16:07:44 -0700149 .. var:: build
James E. Blair21037782017-07-19 11:56:55 -0700150
James E. Blaird9f0efb2017-08-02 16:07:44 -0700151 The UUID of the build. A build is a single execution of a job.
152 When an item is enqueued into a pipeline, this usually results
153 in one build of each job configured for that item's project.
154 However, items may be re-enqueued in which case another build
155 may run. In dependent pipelines, the same job may run multiple
156 times for the same item as circumstances change ahead in the
157 queue. Each time a job is run, for whatever reason, it is
158 acompanied with a new unique id.
James E. Blair21037782017-07-19 11:56:55 -0700159
James E. Blaird9f0efb2017-08-02 16:07:44 -0700160 .. var:: buildset
James E. Blair21037782017-07-19 11:56:55 -0700161
James E. Blaird9f0efb2017-08-02 16:07:44 -0700162 The build set UUID. When Zuul runs jobs for an item, the
163 collection of those jobs is known as a buildset. If the
164 configuration of items ahead in a dependent pipeline changes,
165 Zuul creates a new buildset and restarts all of the jobs.
James E. Blair21037782017-07-19 11:56:55 -0700166
James E. Blaird9f0efb2017-08-02 16:07:44 -0700167 .. var:: ref
James E. Blair2ef29e92017-07-21 15:25:05 -0700168
James E. Blaird9f0efb2017-08-02 16:07:44 -0700169 The git ref of the item. This will be the full path (e.g.,
170 `refs/heads/master` or `refs/changes/...`).
James E. Blair21037782017-07-19 11:56:55 -0700171
James E. Blaird9f0efb2017-08-02 16:07:44 -0700172 .. var:: pipeline
James E. Blair21037782017-07-19 11:56:55 -0700173
James E. Blaird9f0efb2017-08-02 16:07:44 -0700174 The name of the pipeline in which the job is being run.
Monty Taylor299f94b2017-07-28 17:16:36 -0500175
James E. Blaird9f0efb2017-08-02 16:07:44 -0700176 .. var:: job
James E. Blair21037782017-07-19 11:56:55 -0700177
James E. Blaird9f0efb2017-08-02 16:07:44 -0700178 The name of the job being run.
James E. Blair21037782017-07-19 11:56:55 -0700179
James E. Blaird9f0efb2017-08-02 16:07:44 -0700180 .. var:: voting
James E. Blair21037782017-07-19 11:56:55 -0700181
James E. Blaird9f0efb2017-08-02 16:07:44 -0700182 A boolean indicating whether the job is voting.
James E. Blair21037782017-07-19 11:56:55 -0700183
James E. Blaird9f0efb2017-08-02 16:07:44 -0700184 .. var:: project
James E. Blair21037782017-07-19 11:56:55 -0700185
James E. Blaird9f0efb2017-08-02 16:07:44 -0700186 The item's project. This is a data structure with the following
187 fields:
James E. Blair21037782017-07-19 11:56:55 -0700188
James E. Blaird9f0efb2017-08-02 16:07:44 -0700189 .. var:: name
Monty Taylor299f94b2017-07-28 17:16:36 -0500190
James E. Blaird9f0efb2017-08-02 16:07:44 -0700191 The name of the project, excluding hostname. E.g., `org/project`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500192
James E. Blaird9f0efb2017-08-02 16:07:44 -0700193 .. var:: short_name
Monty Taylor299f94b2017-07-28 17:16:36 -0500194
James E. Blaird9f0efb2017-08-02 16:07:44 -0700195 The name of the project, excluding directories or
196 organizations. E.g., `project`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500197
James E. Blaird9f0efb2017-08-02 16:07:44 -0700198 .. var:: canonical_hostname
Monty Taylor299f94b2017-07-28 17:16:36 -0500199
James E. Blaird9f0efb2017-08-02 16:07:44 -0700200 The canonical hostname where the project lives. E.g.,
201 `git.example.com`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500202
James E. Blaird9f0efb2017-08-02 16:07:44 -0700203 .. var:: canonical_name
204
205 The full canonical name of the project including hostname.
206 E.g., `git.example.com/org/project`.
207
Monty Taylor9e67bb72017-08-08 15:32:06 -0500208 .. var:: src_dir
209
James E. Blairba46c062017-08-28 16:23:24 -0700210 The path to the source code relative to the work dir. E.g.,
211 `src/git.example.com/org/project`.
Monty Taylor9e67bb72017-08-08 15:32:06 -0500212
James E. Blairba46c062017-08-28 16:23:24 -0700213 .. var:: projects
214 :type: list
215
216 A list of all projects prepared by Zuul for the item. It
217 includes, at least, the item's own project. It also includes
218 the projects of any items this item depends on, as well as the
219 projects that appear in :attr:`job.required-projects`.
220
221 This is a list of dictionaries, with each element consisting of:
222
223 .. var:: name
224
225 The name of the project, excluding hostname. E.g., `org/project`.
226
227 .. var:: short_name
228
229 The name of the project, excluding directories or
230 organizations. E.g., `project`.
231
232 .. var:: canonical_hostname
233
234 The canonical hostname where the project lives. E.g.,
235 `git.example.com`.
236
237 .. var:: canonical_name
238
239 The full canonical name of the project including hostname.
240 E.g., `git.example.com/org/project`.
241
242 .. var:: src_dir
243
244 The path to the source code, relative to the work dir. E.g.,
245 `src/git.example.com/org/project`.
Monty Taylor9e67bb72017-08-08 15:32:06 -0500246
James E. Blairb3d3f2b2017-09-27 12:04:55 -0700247 .. var:: required
248
249 A boolean indicating whether this project appears in the
250 :attr:`job.required-projects` list for this job.
251
James E. Blaird9f0efb2017-08-02 16:07:44 -0700252 .. var:: tenant
253
254 The name of the current Zuul tenant.
255
James E. Blair93e57f72017-09-01 08:51:49 -0700256 .. var:: timeout
257
258 The job timeout, in seconds.
259
James E. Blaird9f0efb2017-08-02 16:07:44 -0700260 .. var:: jobtags
261
262 A list of tags associated with the job. Not to be confused with
263 git tags, these are simply free-form text fields that can be
264 used by the job for reporting or classification purposes.
265
266 .. var:: items
267 :type: list
268
269 A list of dictionaries, each representing an item being tested
270 with this change with the format:
271
272 .. var:: project
273
274 The item's project. This is a data structure with the
275 following fields:
276
277 .. var:: name
278
279 The name of the project, excluding hostname. E.g.,
280 `org/project`.
281
282 .. var:: short_name
283
284 The name of the project, excluding directories or
285 organizations. E.g., `project`.
286
287 .. var:: canonical_hostname
288
289 The canonical hostname where the project lives. E.g.,
290 `git.example.com`.
291
292 .. var:: canonical_name
293
294 The full canonical name of the project including hostname.
295 E.g., `git.example.com/org/project`.
296
Monty Taylor9e67bb72017-08-08 15:32:06 -0500297 .. var:: src_dir
298
299 The path to the source code on the remote host, relative
300 to the home dir of the remote user.
301 E.g., `src/git.example.com/org/project`.
302
James E. Blaird9f0efb2017-08-02 16:07:44 -0700303 .. var:: branch
304
305 The target branch of the change (without the `refs/heads/` prefix).
306
307 .. var:: change
308
309 The identifier for the change.
310
Monty Taylor41634442017-09-06 18:35:17 -0500311 .. var:: change_url
312
313 The URL to the source location of the given change.
314 E.g., `https://review.example.org/#/c/123456/` or
315 `https://github.com/example/example/pull/1234`.
316
James E. Blaird9f0efb2017-08-02 16:07:44 -0700317 .. var:: patchset
318
319 The patchset identifier for the change. If a change is
320 revised, this will have a different value.
James E. Blair21037782017-07-19 11:56:55 -0700321
Jesse Keatinga49ddaf2017-09-11 18:17:47 -0600322.. var:: zuul_success
323
324 Post run playbook(s) will be passed this variable to indicate if the run
325 phase of the job was successful or not. This variable is meant to be used
326 with the `boolean` filter.
327
328
James E. Blair21037782017-07-19 11:56:55 -0700329Change Items
330++++++++++++
331
332A change to the repository. Most often, this will be a git reference
333which has not yet been merged into the repository (e.g., a gerrit
334change or a GitHub pull request). The following additional variables
335are available:
336
James E. Blaird9f0efb2017-08-02 16:07:44 -0700337.. var:: zuul
338 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700339
James E. Blaird9f0efb2017-08-02 16:07:44 -0700340 .. var:: branch
James E. Blair21037782017-07-19 11:56:55 -0700341
James E. Blaird9f0efb2017-08-02 16:07:44 -0700342 The target branch of the change (without the `refs/heads/` prefix).
343
344 .. var:: change
345
346 The identifier for the change.
347
348 .. var:: patchset
349
350 The patchset identifier for the change. If a change is revised,
351 this will have a different value.
James E. Blair21037782017-07-19 11:56:55 -0700352
Monty Taylor41634442017-09-06 18:35:17 -0500353 .. var:: change_url
354
355 The URL to the source location of the given change.
356 E.g., `https://review.example.org/#/c/123456/` or
357 `https://github.com/example/example/pull/1234`.
358
James E. Blair21037782017-07-19 11:56:55 -0700359Branch Items
360++++++++++++
361
362This represents a branch tip. This item may have been enqueued
363because the branch was updated (via a change having merged, or a
364direct push). Or it may have been enqueued by a timer for the purpose
365of verifying the current condition of the branch. The following
366additional variables are available:
367
James E. Blaird9f0efb2017-08-02 16:07:44 -0700368.. var:: zuul
369 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700370
James E. Blaird9f0efb2017-08-02 16:07:44 -0700371 .. var:: branch
James E. Blair21037782017-07-19 11:56:55 -0700372
James E. Blaird9f0efb2017-08-02 16:07:44 -0700373 The name of the item's branch (without the `refs/heads/`
374 prefix).
375
376 .. var:: oldrev
377
378 If the item was enqueued as the result of a change merging or
379 being pushed to the branch, the git sha of the old revision will
380 be included here. Otherwise, this variable will be undefined.
381
382 .. var:: newrev
383
384 If the item was enqueued as the result of a change merging or
385 being pushed to the branch, the git sha of the new revision will
386 be included here. Otherwise, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700387
388Tag Items
389+++++++++
390
391This represents a git tag. The item may have been enqueued because a
392tag was created or deleted. The following additional variables are
393available:
394
James E. Blaird9f0efb2017-08-02 16:07:44 -0700395.. var:: zuul
396 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700397
James E. Blaird9f0efb2017-08-02 16:07:44 -0700398 .. var:: tag
James E. Blair21037782017-07-19 11:56:55 -0700399
James E. Blaird9f0efb2017-08-02 16:07:44 -0700400 The name of the item's tag (without the `refs/tags/` prefix).
401
402 .. var:: oldrev
403
404 If the item was enqueued as the result of a tag being deleted,
405 the previous git sha of the tag will be included here. If the
406 tag was created, this variable will be undefined.
407
408 .. var:: newrev
409
410 If the item was enqueued as the result of a tag being created,
411 the new git sha of the tag will be included here. If the tag
412 was deleted, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700413
414Ref Items
415+++++++++
416
417This represents a git reference that is neither a change, branch, or
418tag. Note that all items include a `ref` attribute which may be used
419to identify the ref. The following additional variables are
420available:
421
James E. Blaird9f0efb2017-08-02 16:07:44 -0700422.. var:: zuul
423 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700424
James E. Blaird9f0efb2017-08-02 16:07:44 -0700425 .. var:: oldrev
426
427 If the item was enqueued as the result of a ref being deleted,
428 the previous git sha of the ref will be included here. If the
429 ref was created, this variable will be undefined.
430
431 .. var:: newrev
432
433 If the item was enqueued as the result of a ref being created,
434 the new git sha of the ref will be included here. If the ref
435 was deleted, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700436
437Working Directory
438+++++++++++++++++
439
440Additionally, some information about the working directory and the
441executor running the job is available:
James E. Blaireff5a9d2017-06-20 00:00:37 -0700442
James E. Blaird9f0efb2017-08-02 16:07:44 -0700443.. var:: zuul
444 :hidden:
James E. Blaireff5a9d2017-06-20 00:00:37 -0700445
James E. Blaird9f0efb2017-08-02 16:07:44 -0700446 .. var:: executor
James E. Blaireff5a9d2017-06-20 00:00:37 -0700447
James E. Blaird9f0efb2017-08-02 16:07:44 -0700448 A number of values related to the executor running the job are
449 available:
James E. Blaireff5a9d2017-06-20 00:00:37 -0700450
James E. Blaird9f0efb2017-08-02 16:07:44 -0700451 .. var:: hostname
452
453 The hostname of the executor.
454
455 .. var:: src_root
456
457 The path to the source directory.
458
459 .. var:: log_root
460
461 The path to the logs directory.
462
463 .. var:: work_root
464
465 The path to the working directory.
Jamie Lennox7655b552017-03-17 12:33:38 +1100466
467.. _user_sitewide_variables:
468
469Site-wide Variables
470~~~~~~~~~~~~~~~~~~~
471
472The Zuul administrator may define variables which will be available to
473all jobs running in the system. These are statically defined and may
474not be altered by jobs. See the :ref:`Administrator's Guide
475<admin_sitewide_variables>` for information on how a site
476administrator may define these variables.
477
478
James E. Blaireff5a9d2017-06-20 00:00:37 -0700479SSH Keys
480--------
481
482Zuul starts each job with an SSH agent running and the key used to
483access the job's nodes added to that agent. Generally you won't need
484to be aware of this since Ansible will use this when performing any
485tasks on remote nodes. However, under some circumstances you may want
486to interact with the agent. For example, you may wish to add a key
487provided as a secret to the job in order to access a specific host, or
488you may want to, in a pre-playbook, replace the key used to log into
489the assigned nodes in order to further protect it from being abused by
490untrusted job content.
491
492.. TODO: describe standard lib and link to published docs for it.
493
James E. Blair88e79c02017-07-07 13:36:54 -0700494.. _return_values:
495
James E. Blair196f61a2017-06-30 15:42:29 -0700496Return Values
497-------------
498
499The job may return some values to Zuul to affect its behavior. To
500return a value, use the *zuul_return* Ansible module in a job
James E. Blaird9f0efb2017-08-02 16:07:44 -0700501playbook. For example:
502
503.. code-block:: yaml
James E. Blair196f61a2017-06-30 15:42:29 -0700504
505 tasks:
506 - zuul_return:
507 data:
508 foo: bar
509
510Will return the dictionary "{'foo': 'bar'}" to Zuul.
511
512.. TODO: xref to section describing formatting
513
514Several uses of these values are planned, but the only currently
515implemented use is to set the log URL for a build. To do so, set the
James E. Blaird9f0efb2017-08-02 16:07:44 -0700516**zuul.log_url** value. For example:
517
518.. code-block:: yaml
James E. Blair196f61a2017-06-30 15:42:29 -0700519
520 tasks:
521 - zuul_return:
522 data:
523 zuul:
524 log_url: http://logs.example.com/path/to/build/logs