blob: 0639b8b50970398ecd2436a65ac274b441340279 [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
James E. Blair698703c2017-09-15 20:58:30 -060086* Parent job results
87
Jamie Lennox7655b552017-03-17 12:33:38 +110088Meaning that a site-wide variable with the same name as any other will
89override its value, and similarly, secrets override job variables of
James E. Blair698703c2017-09-15 20:58:30 -060090the same name which override data returned from parent jobs. Each of
91the sources is described below.
Jamie Lennox7655b552017-03-17 12:33:38 +110092
93
94Job Variables
95~~~~~~~~~~~~~
96
James E. Blaird9f0efb2017-08-02 16:07:44 -070097Any variables specified in the job definition (using the
98:attr:`job.vars` attribute) are available as Ansible host variables.
99They are added to the ``vars`` section of the inventory file under the
100``all`` hosts group, so they are available to all hosts. Simply refer
101to them by the name specified in the job's ``vars`` section.
James E. Blair28c8e3b2017-07-17 16:27:50 -0700102
103Secrets
104~~~~~~~
105
James E. Blaird9f0efb2017-08-02 16:07:44 -0700106:ref:`Secrets <secret>` also appear as variables available to Ansible.
107Unlike job variables, these are not added to the inventory file (so
108that the inventory file may be kept for debugging purposes without
109revealing secrets). But they are still available to Ansible as normal
James E. Blair28c8e3b2017-07-17 16:27:50 -0700110variables. Because secrets are groups of variables, they will appear
111as a dictionary structure in templates, with the dictionary itself
112being the name of the secret, and its members the individual items in
James E. Blaird9f0efb2017-08-02 16:07:44 -0700113the secret. For example, a secret defined as:
114
115.. code-block:: yaml
James E. Blair28c8e3b2017-07-17 16:27:50 -0700116
117 - secret:
118 name: credentials
119 data:
120 username: foo
121 password: bar
122
123Might be used in a template as::
124
125 {{ credentials.username }} {{ credentials.password }}
126
James E. Blair892cca62017-08-09 11:36:58 -0700127Secrets are only available to playbooks associated with the job
128definition which uses the secret; they are not available to playbooks
129associated with child jobs or job variants.
James E. Blair28c8e3b2017-07-17 16:27:50 -0700130
James E. Blaireff5a9d2017-06-20 00:00:37 -0700131Zuul Variables
James E. Blair28c8e3b2017-07-17 16:27:50 -0700132~~~~~~~~~~~~~~
James E. Blaireff5a9d2017-06-20 00:00:37 -0700133
134Zuul supplies not only the variables specified by the job definition
James E. Blaird9f0efb2017-08-02 16:07:44 -0700135to Ansible, but also some variables from Zuul itself.
James E. Blair21037782017-07-19 11:56:55 -0700136
James E. Blairbabefce2017-07-20 17:14:54 -0700137When a pipeline is triggered by an action, it enqueues items which may
James E. Blair21037782017-07-19 11:56:55 -0700138vary based on the pipeline's configuration. For example, when a new
139change is created, that change may be enqueued into the pipeline,
140while a tag may be enqueued into the pipeline when it is pushed.
141
142Information about these items is available to jobs. All of the items
143enqueued in a pipeline are git references, and therefore share some
144attributes in common. But other attributes may vary based on the type
145of item.
146
James E. Blaird9f0efb2017-08-02 16:07:44 -0700147.. var:: zuul
James E. Blair21037782017-07-19 11:56:55 -0700148
James E. Blaird9f0efb2017-08-02 16:07:44 -0700149 All items provide the following information as Ansible variables
150 under the ``zuul`` key:
James E. Blair21037782017-07-19 11:56:55 -0700151
James E. Blaird9f0efb2017-08-02 16:07:44 -0700152 .. var:: build
James E. Blair21037782017-07-19 11:56:55 -0700153
James E. Blaird9f0efb2017-08-02 16:07:44 -0700154 The UUID of the build. A build is a single execution of a job.
155 When an item is enqueued into a pipeline, this usually results
156 in one build of each job configured for that item's project.
157 However, items may be re-enqueued in which case another build
158 may run. In dependent pipelines, the same job may run multiple
159 times for the same item as circumstances change ahead in the
160 queue. Each time a job is run, for whatever reason, it is
161 acompanied with a new unique id.
James E. Blair21037782017-07-19 11:56:55 -0700162
James E. Blaird9f0efb2017-08-02 16:07:44 -0700163 .. var:: buildset
James E. Blair21037782017-07-19 11:56:55 -0700164
James E. Blaird9f0efb2017-08-02 16:07:44 -0700165 The build set UUID. When Zuul runs jobs for an item, the
166 collection of those jobs is known as a buildset. If the
167 configuration of items ahead in a dependent pipeline changes,
168 Zuul creates a new buildset and restarts all of the jobs.
James E. Blair21037782017-07-19 11:56:55 -0700169
James E. Blaird9f0efb2017-08-02 16:07:44 -0700170 .. var:: ref
James E. Blair2ef29e92017-07-21 15:25:05 -0700171
James E. Blaird9f0efb2017-08-02 16:07:44 -0700172 The git ref of the item. This will be the full path (e.g.,
173 `refs/heads/master` or `refs/changes/...`).
James E. Blair21037782017-07-19 11:56:55 -0700174
James E. Blairbae8fec2017-11-21 10:19:06 -0800175 .. var:: override_checkout
176
177 If the job was configured to override the branch or tag checked
178 out, this will contain the specified value. Otherwise, it will
179 be null.
180
James E. Blaird9f0efb2017-08-02 16:07:44 -0700181 .. var:: pipeline
James E. Blair21037782017-07-19 11:56:55 -0700182
James E. Blaird9f0efb2017-08-02 16:07:44 -0700183 The name of the pipeline in which the job is being run.
Monty Taylor299f94b2017-07-28 17:16:36 -0500184
James E. Blaird9f0efb2017-08-02 16:07:44 -0700185 .. var:: job
James E. Blair21037782017-07-19 11:56:55 -0700186
James E. Blaird9f0efb2017-08-02 16:07:44 -0700187 The name of the job being run.
James E. Blair21037782017-07-19 11:56:55 -0700188
James E. Blaird9f0efb2017-08-02 16:07:44 -0700189 .. var:: voting
James E. Blair21037782017-07-19 11:56:55 -0700190
James E. Blaird9f0efb2017-08-02 16:07:44 -0700191 A boolean indicating whether the job is voting.
James E. Blair21037782017-07-19 11:56:55 -0700192
James E. Blaird9f0efb2017-08-02 16:07:44 -0700193 .. var:: project
James E. Blair21037782017-07-19 11:56:55 -0700194
James E. Blaird9f0efb2017-08-02 16:07:44 -0700195 The item's project. This is a data structure with the following
196 fields:
James E. Blair21037782017-07-19 11:56:55 -0700197
James E. Blaird9f0efb2017-08-02 16:07:44 -0700198 .. var:: name
Monty Taylor299f94b2017-07-28 17:16:36 -0500199
James E. Blaird9f0efb2017-08-02 16:07:44 -0700200 The name of the project, excluding hostname. E.g., `org/project`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500201
James E. Blaird9f0efb2017-08-02 16:07:44 -0700202 .. var:: short_name
Monty Taylor299f94b2017-07-28 17:16:36 -0500203
James E. Blaird9f0efb2017-08-02 16:07:44 -0700204 The name of the project, excluding directories or
205 organizations. E.g., `project`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500206
James E. Blaird9f0efb2017-08-02 16:07:44 -0700207 .. var:: canonical_hostname
Monty Taylor299f94b2017-07-28 17:16:36 -0500208
James E. Blaird9f0efb2017-08-02 16:07:44 -0700209 The canonical hostname where the project lives. E.g.,
210 `git.example.com`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500211
James E. Blaird9f0efb2017-08-02 16:07:44 -0700212 .. var:: canonical_name
213
214 The full canonical name of the project including hostname.
215 E.g., `git.example.com/org/project`.
216
Monty Taylor9e67bb72017-08-08 15:32:06 -0500217 .. var:: src_dir
218
James E. Blairba46c062017-08-28 16:23:24 -0700219 The path to the source code relative to the work dir. E.g.,
220 `src/git.example.com/org/project`.
Monty Taylor9e67bb72017-08-08 15:32:06 -0500221
James E. Blairba46c062017-08-28 16:23:24 -0700222 .. var:: projects
223 :type: list
224
225 A list of all projects prepared by Zuul for the item. It
226 includes, at least, the item's own project. It also includes
227 the projects of any items this item depends on, as well as the
228 projects that appear in :attr:`job.required-projects`.
229
230 This is a list of dictionaries, with each element consisting of:
231
232 .. var:: name
233
234 The name of the project, excluding hostname. E.g., `org/project`.
235
236 .. var:: short_name
237
238 The name of the project, excluding directories or
239 organizations. E.g., `project`.
240
241 .. var:: canonical_hostname
242
243 The canonical hostname where the project lives. E.g.,
244 `git.example.com`.
245
246 .. var:: canonical_name
247
248 The full canonical name of the project including hostname.
249 E.g., `git.example.com/org/project`.
250
251 .. var:: src_dir
252
253 The path to the source code, relative to the work dir. E.g.,
254 `src/git.example.com/org/project`.
Monty Taylor9e67bb72017-08-08 15:32:06 -0500255
James E. Blairb3d3f2b2017-09-27 12:04:55 -0700256 .. var:: required
257
258 A boolean indicating whether this project appears in the
259 :attr:`job.required-projects` list for this job.
260
James E. Blairbae8fec2017-11-21 10:19:06 -0800261 .. var:: checkout
262
263 The branch or tag that Zuul checked out for this project.
264 This may be influenced by the branch or tag associated with
265 the item as well as the job configuration.
266
Ian Wienandd61d98e2017-10-18 08:21:19 +1100267 .. var:: _projects
268 :type: dict
269
270 The same as ``projects`` but a dictionary indexed by the
271 ``name`` value of each entry. ``projects`` will be converted to
272 this.
273
James E. Blaird9f0efb2017-08-02 16:07:44 -0700274 .. var:: tenant
275
276 The name of the current Zuul tenant.
277
James E. Blair93e57f72017-09-01 08:51:49 -0700278 .. var:: timeout
279
280 The job timeout, in seconds.
281
James E. Blaird9f0efb2017-08-02 16:07:44 -0700282 .. var:: jobtags
283
284 A list of tags associated with the job. Not to be confused with
285 git tags, these are simply free-form text fields that can be
286 used by the job for reporting or classification purposes.
287
288 .. var:: items
289 :type: list
290
291 A list of dictionaries, each representing an item being tested
292 with this change with the format:
293
294 .. var:: project
295
296 The item's project. This is a data structure with the
297 following fields:
298
299 .. var:: name
300
301 The name of the project, excluding hostname. E.g.,
302 `org/project`.
303
304 .. var:: short_name
305
306 The name of the project, excluding directories or
307 organizations. E.g., `project`.
308
309 .. var:: canonical_hostname
310
311 The canonical hostname where the project lives. E.g.,
312 `git.example.com`.
313
314 .. var:: canonical_name
315
316 The full canonical name of the project including hostname.
317 E.g., `git.example.com/org/project`.
318
Monty Taylor9e67bb72017-08-08 15:32:06 -0500319 .. var:: src_dir
320
321 The path to the source code on the remote host, relative
322 to the home dir of the remote user.
323 E.g., `src/git.example.com/org/project`.
324
James E. Blaird9f0efb2017-08-02 16:07:44 -0700325 .. var:: branch
326
327 The target branch of the change (without the `refs/heads/` prefix).
328
329 .. var:: change
330
331 The identifier for the change.
332
Monty Taylor41634442017-09-06 18:35:17 -0500333 .. var:: change_url
334
335 The URL to the source location of the given change.
336 E.g., `https://review.example.org/#/c/123456/` or
337 `https://github.com/example/example/pull/1234`.
338
James E. Blaird9f0efb2017-08-02 16:07:44 -0700339 .. var:: patchset
340
341 The patchset identifier for the change. If a change is
342 revised, this will have a different value.
James E. Blair21037782017-07-19 11:56:55 -0700343
Jesse Keatinga49ddaf2017-09-11 18:17:47 -0600344.. var:: zuul_success
345
346 Post run playbook(s) will be passed this variable to indicate if the run
347 phase of the job was successful or not. This variable is meant to be used
348 with the `boolean` filter.
349
350
James E. Blair21037782017-07-19 11:56:55 -0700351Change Items
352++++++++++++
353
354A change to the repository. Most often, this will be a git reference
355which has not yet been merged into the repository (e.g., a gerrit
356change or a GitHub pull request). The following additional variables
357are available:
358
James E. Blaird9f0efb2017-08-02 16:07:44 -0700359.. var:: zuul
360 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700361
James E. Blaird9f0efb2017-08-02 16:07:44 -0700362 .. var:: branch
James E. Blair21037782017-07-19 11:56:55 -0700363
James E. Blaird9f0efb2017-08-02 16:07:44 -0700364 The target branch of the change (without the `refs/heads/` prefix).
365
366 .. var:: change
367
368 The identifier for the change.
369
370 .. var:: patchset
371
372 The patchset identifier for the change. If a change is revised,
373 this will have a different value.
James E. Blair21037782017-07-19 11:56:55 -0700374
Monty Taylor41634442017-09-06 18:35:17 -0500375 .. var:: change_url
376
377 The URL to the source location of the given change.
378 E.g., `https://review.example.org/#/c/123456/` or
379 `https://github.com/example/example/pull/1234`.
380
James E. Blair21037782017-07-19 11:56:55 -0700381Branch Items
382++++++++++++
383
384This represents a branch tip. This item may have been enqueued
385because the branch was updated (via a change having merged, or a
386direct push). Or it may have been enqueued by a timer for the purpose
387of verifying the current condition of the branch. The following
388additional variables are available:
389
James E. Blaird9f0efb2017-08-02 16:07:44 -0700390.. var:: zuul
391 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700392
James E. Blaird9f0efb2017-08-02 16:07:44 -0700393 .. var:: branch
James E. Blair21037782017-07-19 11:56:55 -0700394
James E. Blaird9f0efb2017-08-02 16:07:44 -0700395 The name of the item's branch (without the `refs/heads/`
396 prefix).
397
398 .. var:: oldrev
399
400 If the item was enqueued as the result of a change merging or
401 being pushed to the branch, the git sha of the old revision will
402 be included here. Otherwise, this variable will be undefined.
403
404 .. var:: newrev
405
406 If the item was enqueued as the result of a change merging or
407 being pushed to the branch, the git sha of the new revision will
408 be included here. Otherwise, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700409
410Tag Items
411+++++++++
412
413This represents a git tag. The item may have been enqueued because a
414tag was created or deleted. The following additional variables are
415available:
416
James E. Blaird9f0efb2017-08-02 16:07:44 -0700417.. var:: zuul
418 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700419
James E. Blaird9f0efb2017-08-02 16:07:44 -0700420 .. var:: tag
James E. Blair21037782017-07-19 11:56:55 -0700421
James E. Blaird9f0efb2017-08-02 16:07:44 -0700422 The name of the item's tag (without the `refs/tags/` prefix).
423
424 .. var:: oldrev
425
426 If the item was enqueued as the result of a tag being deleted,
427 the previous git sha of the tag will be included here. If the
428 tag was created, this variable will be undefined.
429
430 .. var:: newrev
431
432 If the item was enqueued as the result of a tag being created,
433 the new git sha of the tag will be included here. If the tag
434 was deleted, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700435
436Ref Items
437+++++++++
438
439This represents a git reference that is neither a change, branch, or
440tag. Note that all items include a `ref` attribute which may be used
441to identify the ref. The following additional variables are
442available:
443
James E. Blaird9f0efb2017-08-02 16:07:44 -0700444.. var:: zuul
445 :hidden:
James E. Blair21037782017-07-19 11:56:55 -0700446
James E. Blaird9f0efb2017-08-02 16:07:44 -0700447 .. var:: oldrev
448
449 If the item was enqueued as the result of a ref being deleted,
450 the previous git sha of the ref will be included here. If the
451 ref was created, this variable will be undefined.
452
453 .. var:: newrev
454
455 If the item was enqueued as the result of a ref being created,
456 the new git sha of the ref will be included here. If the ref
457 was deleted, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700458
459Working Directory
460+++++++++++++++++
461
462Additionally, some information about the working directory and the
463executor running the job is available:
James E. Blaireff5a9d2017-06-20 00:00:37 -0700464
James E. Blaird9f0efb2017-08-02 16:07:44 -0700465.. var:: zuul
466 :hidden:
James E. Blaireff5a9d2017-06-20 00:00:37 -0700467
James E. Blaird9f0efb2017-08-02 16:07:44 -0700468 .. var:: executor
James E. Blaireff5a9d2017-06-20 00:00:37 -0700469
James E. Blaird9f0efb2017-08-02 16:07:44 -0700470 A number of values related to the executor running the job are
471 available:
James E. Blaireff5a9d2017-06-20 00:00:37 -0700472
James E. Blaird9f0efb2017-08-02 16:07:44 -0700473 .. var:: hostname
474
475 The hostname of the executor.
476
477 .. var:: src_root
478
479 The path to the source directory.
480
481 .. var:: log_root
482
483 The path to the logs directory.
484
485 .. var:: work_root
486
487 The path to the working directory.
Jamie Lennox7655b552017-03-17 12:33:38 +1100488
489.. _user_sitewide_variables:
490
491Site-wide Variables
492~~~~~~~~~~~~~~~~~~~
493
494The Zuul administrator may define variables which will be available to
495all jobs running in the system. These are statically defined and may
496not be altered by jobs. See the :ref:`Administrator's Guide
497<admin_sitewide_variables>` for information on how a site
498administrator may define these variables.
499
James E. Blair698703c2017-09-15 20:58:30 -0600500Parent Job Results
501~~~~~~~~~~~~~~~~~~
502
503A job may return data to Zuul for later use by jobs which depend on
504it. For details, see :ref:`return_values`.
Jamie Lennox7655b552017-03-17 12:33:38 +1100505
James E. Blaireff5a9d2017-06-20 00:00:37 -0700506SSH Keys
507--------
508
509Zuul starts each job with an SSH agent running and the key used to
510access the job's nodes added to that agent. Generally you won't need
511to be aware of this since Ansible will use this when performing any
512tasks on remote nodes. However, under some circumstances you may want
513to interact with the agent. For example, you may wish to add a key
514provided as a secret to the job in order to access a specific host, or
515you may want to, in a pre-playbook, replace the key used to log into
516the assigned nodes in order to further protect it from being abused by
517untrusted job content.
518
519.. TODO: describe standard lib and link to published docs for it.
520
James E. Blair88e79c02017-07-07 13:36:54 -0700521.. _return_values:
522
James E. Blair196f61a2017-06-30 15:42:29 -0700523Return Values
524-------------
525
James E. Blair698703c2017-09-15 20:58:30 -0600526A job may return some values to Zuul to affect its behavior and for
527use by other jobs.. To return a value, use the ``zuul_return``
528Ansible module in a job playbook. For example:
James E. Blaird9f0efb2017-08-02 16:07:44 -0700529
530.. code-block:: yaml
James E. Blair196f61a2017-06-30 15:42:29 -0700531
532 tasks:
533 - zuul_return:
534 data:
535 foo: bar
536
James E. Blair698703c2017-09-15 20:58:30 -0600537Will return the dictionary ``{'foo': 'bar'}`` to Zuul.
James E. Blair196f61a2017-06-30 15:42:29 -0700538
539.. TODO: xref to section describing formatting
540
James E. Blair698703c2017-09-15 20:58:30 -0600541To set the log URL for a build, use *zuul_return* to set the
James E. Blaird9f0efb2017-08-02 16:07:44 -0700542**zuul.log_url** value. For example:
543
544.. code-block:: yaml
James E. Blair196f61a2017-06-30 15:42:29 -0700545
546 tasks:
547 - zuul_return:
548 data:
549 zuul:
550 log_url: http://logs.example.com/path/to/build/logs
James E. Blair698703c2017-09-15 20:58:30 -0600551
552Any values other than those in the ``zuul`` hierarchy will be supplied
553as Ansible variables to child jobs. These variables have less
554precedence than any other type of variable in Zuul, so be sure their
555names are not shared by any job variables. If more than one parent
556job returns the same variable, the value from the later job in the job
557graph will take precedence.