blob: b6a85649a7a4314d42f4583251ee4a8414efcf05 [file] [log] [blame]
James E. Blaireff5a9d2017-06-20 00:00:37 -07001:title: Job Content
2
3Job Content
4===========
5
6Zuul jobs are implemneted as Ansible playbooks. Zuul prepares the
7repositories 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. Blair28c8e3b2017-07-17 16:27:50 -070094Any variables specified in the job definition are available as Ansible
95host variables. They are added to the `vars` section of the inventory
96file under the `all` hosts group, so they are available to all hosts.
97Simply refer to them by the name specified in the job's `vars`
98section.
99
100Secrets
101~~~~~~~
102
103Secrets also appear as variables available to Ansible. Unlike job
104variables, these are not added to the inventory file (so that the
105inventory file may be kept for debugging purposes without revealing
106secrets). But they are still available to Ansible as normal
107variables. 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
110the secret. For example, a secret defined as::
111
112 - secret:
113 name: credentials
114 data:
115 username: foo
116 password: bar
117
118Might be used in a template as::
119
120 {{ credentials.username }} {{ credentials.password }}
121
122.. TODO: xref job vars
123
James E. Blaireff5a9d2017-06-20 00:00:37 -0700124Zuul Variables
James E. Blair28c8e3b2017-07-17 16:27:50 -0700125~~~~~~~~~~~~~~
James E. Blaireff5a9d2017-06-20 00:00:37 -0700126
127Zuul supplies not only the variables specified by the job definition
James E. Blair21037782017-07-19 11:56:55 -0700128to Ansible, but also some variables from the Zuul itself.
129
James E. Blairbabefce2017-07-20 17:14:54 -0700130When a pipeline is triggered by an action, it enqueues items which may
James E. Blair21037782017-07-19 11:56:55 -0700131vary based on the pipeline's configuration. For example, when a new
132change is created, that change may be enqueued into the pipeline,
133while a tag may be enqueued into the pipeline when it is pushed.
134
135Information about these items is available to jobs. All of the items
136enqueued in a pipeline are git references, and therefore share some
137attributes in common. But other attributes may vary based on the type
138of item.
139
140All items provide the following information as Ansible variables:
141
James E. Blaira9fbb6c2017-07-20 16:07:30 -0700142**zuul.build**
James E. Blair21037782017-07-19 11:56:55 -0700143 The UUID of the build. A build is a single execution of a job.
144 When an item is enqueued into a pipeline, this usually results in
145 one build of each job configured for that item's project. However,
146 items may be re-enqueued in which case another build may run. In
147 dependent pipelines, the same job may run multiple times for the
148 same item as circumstances change ahead in the queue. Each time a
149 job is run, for whatever reason, it is acompanied with a new
150 unique id.
151
James E. Blair21037782017-07-19 11:56:55 -0700152**zuul.buildset**
153 The build set UUID. When Zuul runs jobs for an item, the collection
154 of those jobs is known as a buildset. If the configuration of items
155 ahead in a dependent pipeline changes, Zuul creates a new buildset
156 and restarts all of the jobs.
157
158**zuul.ref**
159 The git ref of the item. This will be the full path (e.g.,
160 'refs/heads/master' or 'refs/changes/...').
161
162**zuul.pipeline**
163 The name of the pipeline in which the job is being run.
164
165**zuul.job**
166 The name of the job being run.
167
James E. Blair2ef29e92017-07-21 15:25:05 -0700168**zuul.voting**
169 A boolean indicating whether the job is voting.
170
James E. Blair21037782017-07-19 11:56:55 -0700171**zuul.project**
172 The item's project. This is a data structure with the following
173 fields:
174
175**zuul.project.name**
176 The name of the project, excluding hostname. E.g., `org/project`.
177
Monty Taylor299f94b2017-07-28 17:16:36 -0500178**zuul.project.short_name**
179 The name of the project, excluding directories or organizations.
180 E.g., `project`.
181
James E. Blair21037782017-07-19 11:56:55 -0700182**zuul.project.canonical_hostname**
183 The canonical hostname where the project lives. E.g.,
184 `git.example.com`.
185
186**zuul.project.canonical_name**
187 The full canonical name of the project including hostname. E.g.,
188 `git.example.com/org/project`.
189
190**zuul.tenant**
191 The name of the current Zuul tenant.
192
James E. Blair9d46f092017-07-20 16:06:20 -0700193**zuul.jobtags**
James E. Blair21037782017-07-19 11:56:55 -0700194 A list of tags associated with the job. Not to be confused with git
195 tags, these are simply free-form text fields that can be used by the
196 job for reporting or classification purposes.
197
James E. Blair21037782017-07-19 11:56:55 -0700198**zuul.items**
James E. Blair21037782017-07-19 11:56:55 -0700199
James E. Blaira08f4592017-07-20 16:35:55 -0700200 A list of dictionaries, each representing an item being tested with
201 this change with the format:
James E. Blair21037782017-07-19 11:56:55 -0700202
James E. Blaira08f4592017-07-20 16:35:55 -0700203 **project.name**
204 The name of the project, excluding hostname. E.g., `org/project`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500205
206 **project.short_name**
207 The name of the project, excluding directories or organizations.
208 E.g., `project`.
209
James E. Blaira08f4592017-07-20 16:35:55 -0700210 **project.canonical_hostname**
211 The canonical hostname where the project lives. E.g.,
212 `git.example.com`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500213
James E. Blaira08f4592017-07-20 16:35:55 -0700214 **project.canonical_name**
215 The full canonical name of the project including hostname. E.g.,
216 `git.example.com/org/project`.
Monty Taylor299f94b2017-07-28 17:16:36 -0500217
James E. Blaira08f4592017-07-20 16:35:55 -0700218 **branch**
219 The target branch of the change (without the `refs/heads/` prefix).
Monty Taylor299f94b2017-07-28 17:16:36 -0500220
James E. Blaira08f4592017-07-20 16:35:55 -0700221 **change**
222 The identifier for the change.
Monty Taylor299f94b2017-07-28 17:16:36 -0500223
James E. Blaira08f4592017-07-20 16:35:55 -0700224 **patchset**
225 The patchset identifier for the change. If a change is revised,
226 this will have a different value.
James E. Blair21037782017-07-19 11:56:55 -0700227
228Change Items
229++++++++++++
230
231A change to the repository. Most often, this will be a git reference
232which has not yet been merged into the repository (e.g., a gerrit
233change or a GitHub pull request). The following additional variables
234are available:
235
236**zuul.branch**
237 The target branch of the change (without the `refs/heads/` prefix).
238
239**zuul.change**
240 The identifier for the change.
241
242**zuul.patchset**
243 The patchset identifier for the change. If a change is revised,
244 this will have a different value.
245
246Branch Items
247++++++++++++
248
249This represents a branch tip. This item may have been enqueued
250because the branch was updated (via a change having merged, or a
251direct push). Or it may have been enqueued by a timer for the purpose
252of verifying the current condition of the branch. The following
253additional variables are available:
254
255**zuul.branch**
256 The name of the item's branch (without the `refs/heads/` prefix).
257
258**zuul.oldrev**
259 If the item was enqueued as the result of a change merging or being
260 pushed to the branch, the git sha of the old revision will be
James E. Blair673dbd12017-07-21 10:02:49 -0700261 included here. Otherwise, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700262
263**zuul.newrev**
264 If the item was enqueued as the result of a change merging or being
265 pushed to the branch, the git sha of the new revision will be
James E. Blair673dbd12017-07-21 10:02:49 -0700266 included here. Otherwise, this variable will be undefined.
James E. Blair21037782017-07-19 11:56:55 -0700267
268Tag Items
269+++++++++
270
271This represents a git tag. The item may have been enqueued because a
272tag was created or deleted. The following additional variables are
273available:
274
275**zuul.tag**
276 The name of the item's tag (without the `refs/tags/` prefix).
277
278**zuul.oldrev**
James E. Blair673dbd12017-07-21 10:02:49 -0700279 If the item was enqueued as the result of a tag being deleted, the
280 previous git sha of the tag will be included here. If the tag was
281 created, this will be set to the value
282 0000000000000000000000000000000000000000.
James E. Blair21037782017-07-19 11:56:55 -0700283
284**zuul.newrev**
James E. Blair673dbd12017-07-21 10:02:49 -0700285 If the item was enqueued as the result of a tag being created, the
286 new git sha of the tag will be included here. If the tag was
287 deleted, this will be set to the value
288 0000000000000000000000000000000000000000.
James E. Blair21037782017-07-19 11:56:55 -0700289
290Ref Items
291+++++++++
292
293This represents a git reference that is neither a change, branch, or
294tag. Note that all items include a `ref` attribute which may be used
295to identify the ref. The following additional variables are
296available:
297
298**zuul.oldrev**
James E. Blair673dbd12017-07-21 10:02:49 -0700299 If the item was enqueued as the result of a ref being deleted, the
300 previous git sha of the ref will be included here. If the ref was
301 created, this will be set to the value
302 0000000000000000000000000000000000000000.
James E. Blair21037782017-07-19 11:56:55 -0700303
304**zuul.newrev**
James E. Blair673dbd12017-07-21 10:02:49 -0700305 If the item was enqueued as the result of a ref being created, the
306 new git sha of the ref will be included here. If the ref was
307 deleted, this will be set to the value
308 0000000000000000000000000000000000000000.
James E. Blair21037782017-07-19 11:56:55 -0700309
310Working Directory
311+++++++++++++++++
312
313Additionally, some information about the working directory and the
314executor running the job is available:
James E. Blaireff5a9d2017-06-20 00:00:37 -0700315
316**zuul.executor.hostname**
317 The hostname of the executor.
318
319**zuul.executor.src_root**
320 The path to the source directory.
321
322**zuul.executor.log_root**
323 The path to the logs directory.
324
Jamie Lennox7655b552017-03-17 12:33:38 +1100325
326.. _user_sitewide_variables:
327
328Site-wide Variables
329~~~~~~~~~~~~~~~~~~~
330
331The Zuul administrator may define variables which will be available to
332all jobs running in the system. These are statically defined and may
333not be altered by jobs. See the :ref:`Administrator's Guide
334<admin_sitewide_variables>` for information on how a site
335administrator may define these variables.
336
337
James E. Blaireff5a9d2017-06-20 00:00:37 -0700338SSH Keys
339--------
340
341Zuul starts each job with an SSH agent running and the key used to
342access the job's nodes added to that agent. Generally you won't need
343to be aware of this since Ansible will use this when performing any
344tasks on remote nodes. However, under some circumstances you may want
345to interact with the agent. For example, you may wish to add a key
346provided as a secret to the job in order to access a specific host, or
347you may want to, in a pre-playbook, replace the key used to log into
348the assigned nodes in order to further protect it from being abused by
349untrusted job content.
350
351.. TODO: describe standard lib and link to published docs for it.
352
James E. Blair88e79c02017-07-07 13:36:54 -0700353.. _return_values:
354
James E. Blair196f61a2017-06-30 15:42:29 -0700355Return Values
356-------------
357
358The job may return some values to Zuul to affect its behavior. To
359return a value, use the *zuul_return* Ansible module in a job
360playbook. For example::
361
362 tasks:
363 - zuul_return:
364 data:
365 foo: bar
366
367Will return the dictionary "{'foo': 'bar'}" to Zuul.
368
369.. TODO: xref to section describing formatting
370
371Several uses of these values are planned, but the only currently
372implemented use is to set the log URL for a build. To do so, set the
373**zuul.log_url** value. For example::
374
375 tasks:
376 - zuul_return:
377 data:
378 zuul:
379 log_url: http://logs.example.com/path/to/build/logs