Spaces:
Runtime error

derangedsaturnian commited on
Commit
c01b4ff
·
verified ·
1 Parent(s): ae3c7f5

Create data/jenkins_docs/getting-started.adoc

Browse files
data/jenkins_docs/getting-started.adoc ADDED
@@ -0,0 +1,396 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ layout: section
3
+ title: Getting started with Pipeline
4
+ ---
5
+
6
+ ifdef::backend-html5[]
7
+ :description:
8
+ :author:
9
+ :sectanchors:
10
+ :toc:
11
+ ifdef::env-github[:imagesdir: ../resources]
12
+ ifndef::env-github[:imagesdir: ../../resources]
13
+ :hide-uri-scheme:
14
+ endif::[]
15
+ // Show 2/3 of the Blue ocean admonitions
16
+ // :pipeline-visualization-admonition: true
17
+ :pipeline-creation-admonition: true
18
+
19
+ = Getting started with Pipeline
20
+
21
+ As mentioned link:../[previously], Jenkins Pipeline is a suite of
22
+ plugins that supports implementing and integrating continuous delivery pipelines
23
+ into Jenkins. Pipeline provides an extensible set of tools for modeling
24
+ simple-to-complex delivery pipelines "as code" via the Pipeline DSL.
25
+ footnote:dsl[link:https://en.wikipedia.org/wiki/Domain-specific_language[Domain-specific language]]
26
+
27
+ This section describes how to get started with creating your Pipeline project in
28
+ Jenkins and introduces you to the various ways that a `Jenkinsfile` can be
29
+ created and stored.
30
+
31
+
32
+ == Prerequisites
33
+
34
+ To use Jenkins Pipeline, you will need:
35
+
36
+ * Jenkins 2.x or later (older versions back to 1.642.3 may work but are not
37
+ recommended)
38
+ * Pipeline plugin,
39
+ footnote:pipeline[link:https://plugins.jenkins.io/workflow-aggregator[Pipeline plugin]]
40
+ which is installed as part of the "suggested plugins" (specified when running
41
+ through the link:../../installing/linux/#setup-wizard[Post-installation setup wizard]
42
+ after link:../../installing[installing Jenkins]).
43
+
44
+ Read more about how to install and manage plugins in
45
+ link:../../managing/plugins[Managing Plugins].
46
+
47
+
48
+ == Defining a Pipeline
49
+
50
+ Both
51
+ link:../#declarative-versus-scripted-pipeline-syntax[Declarative and Scripted Pipeline]
52
+ are DSLs footnote:dsl[] to describe portions of your software delivery
53
+ pipeline. Scripted Pipeline is written in a limited form of
54
+ link:http://groovy-lang.org/semantics.html[Groovy syntax].
55
+
56
+ Relevant components of Groovy syntax will be introduced as required throughout
57
+ this documentation, so while an understanding of Groovy is helpful, it is not
58
+ required to work with Pipeline.
59
+
60
+ A Pipeline can be created in one of the following ways:
61
+
62
+ * <<through-blue-ocean>> - after setting up a Pipeline project in Blue Ocean,
63
+ the Blue Ocean UI helps you write your Pipeline's `Jenkinsfile` and commit it
64
+ to source control.
65
+ * <<through-the-classic-ui>> - you can enter a basic Pipeline directly in
66
+ Jenkins through the classic UI.
67
+ * link:#defining-a-pipeline-in-scm[In SCM] - you can write a `Jenkinsfile`
68
+ manually, which you can commit to your project's source control repository.
69
+ footnote:scm[link:https://en.wikipedia.org/wiki/Version_control[Source control management]]
70
+
71
+ The syntax for defining a Pipeline with either approach is the same, but while
72
+ Jenkins supports entering Pipeline directly into the classic UI, it is
73
+ generally considered best practice to define the Pipeline in a `Jenkinsfile`
74
+ which Jenkins will then load directly from source control.
75
+
76
+ This video provides basic instructions on how to write both Declarative and Scripted Pipelines.
77
+
78
+ .Writing a Pipeline script in Jenkins
79
+ video::TiTrcFEsj7A[youtube,width=800,height=420]
80
+
81
+ === Through Blue Ocean
82
+
83
+ If you are new to Jenkins Pipeline, the Blue Ocean UI helps you
84
+ link:../../blueocean/creating-pipelines[set up your Pipeline project], and
85
+ automatically creates and writes your Pipeline (i.e. the `Jenkinsfile`) for you
86
+ through the graphical Pipeline editor.
87
+
88
+ As part of setting up your Pipeline project in Blue Ocean, Jenkins configures a
89
+ secure and appropriately authenticated connection to your project's source
90
+ control repository. Therefore, any changes you make to the `Jenkinsfile` via
91
+ Blue Ocean's Pipeline editor are automatically saved and committed to source
92
+ control.
93
+
94
+ Read more about Blue Ocean in the link:../../blueocean[Blue Ocean] chapter and
95
+ link:../../blueocean/getting-started[Getting started with Blue Ocean] page.
96
+
97
+ include::doc/book/blueocean/_blue-ocean-status.adoc[]
98
+
99
+ === Through the classic UI
100
+
101
+ A `Jenkinsfile` created using the classic UI is stored by Jenkins itself (within
102
+ the Jenkins home directory).
103
+
104
+ To create a basic Pipeline through the Jenkins classic UI:
105
+
106
+ . If required, ensure you are logged in to Jenkins.
107
+ . From the Jenkins Dashboard, select *New Item*.
108
+ +
109
+ [.boxshadow]
110
+ image:pipeline/classic-ui-new-item.png[alt = "Jenkins Classic UI left column showing options: New Item, Build History, Project Relationship, Check File Fingerprint, Manage Jenkins and My Views.",width=35%]
111
+
112
+ . In the *Enter an item name* field, specify the name for your new Pipeline
113
+ project. +
114
+ *Caution:* Jenkins uses this item name to create directories on disk. It is
115
+ recommended to avoid using spaces in item names, since doing so may uncover
116
+ bugs in scripts that do not properly handle spaces in directory paths.
117
+ . Scroll down and click *Pipeline*, then click *OK* at the end of the page to
118
+ open the Pipeline configuration page (whose *General* tab is selected).
119
+ +
120
+ [.boxshadow]
121
+ image:pipeline/new-item-creation.png[alt = "Jenkins New Item page with item name as 'example-pipeline' and 'Pipeline' option selected from the options for item type given as follows: Freestyle project, Pipeline, Multi-configuration project, Folder, Multibranch Pipeline and Organization Folder.",width=100%]
122
+
123
+ . Click the *Pipeline* tab in the side panel of the page to scroll down to the
124
+ *Pipeline* section. +
125
+ *Note:* If instead you are defining your `Jenkinsfile` in source control,
126
+ follow the instructions in link:#defining-a-pipeline-in-scm[In SCM] below.
127
+ . In the *Pipeline* section, ensure that the *Definition* field indicates the
128
+ *Pipeline script* option.
129
+ . Enter your Pipeline code into the *Script* text area. +
130
+ For instance, copy the following Declarative example Pipeline code (below the
131
+ _Jenkinsfile ( ... )_ heading) or its Scripted version equivalent and paste
132
+ this into the *Script* text area. (The Declarative example below is used
133
+ throughout the remainder of this procedure.)
134
+ +
135
+ [pipeline]
136
+ ----
137
+ // Declarative //
138
+ pipeline {
139
+ agent any // <1>
140
+ stages {
141
+ stage('Stage 1') {
142
+ steps {
143
+ echo 'Hello world!' // <2>
144
+ }
145
+ }
146
+ }
147
+ }
148
+ // Script //
149
+ node { // <3>
150
+ stage('Stage 1') {
151
+ echo 'Hello World' // <2>
152
+ }
153
+ }
154
+ ----
155
+ <1> `agent` instructs Jenkins to allocate an executor (on any available
156
+ agent/node in the Jenkins environment) and workspace for the entire Pipeline.
157
+ <2> `echo` writes simple string in the console output.
158
+ <3> `node` effectively does the same as `agent` (above).
159
+ +
160
+ [.boxshadow]
161
+ image:pipeline/example-pipeline-in-classic-ui.png[alt = "Configure page of 'example-pipeline' with the Pipeline tab selected in the Jenkins Classic UI. The 'Hello World' script is written in the script section, and the 'Use Groovy Sandbox' option is checked.",width=100%]
162
+ +
163
+ *Note:* You can also select from canned _Scripted_ Pipeline examples from the
164
+ *try sample Pipeline* option at the top right of the *Script* text area. Be
165
+ aware that there are no canned Declarative Pipeline examples available from this
166
+ field.
167
+ . Click *Save* to open the Pipeline project/item view page.
168
+ . On this page, click *Build Now* on the left to run the Pipeline.
169
+ +
170
+ [.boxshadow]
171
+ image:pipeline/classic-ui-left-column-on-item-build-now.png[alt = "Jenkins Classic UI sidebar for 'example-pipeline', showing options: Status, Changes, Build Now, Configure, Delete Pipeline, Full Stage View, Stages, Rename and Pipeline Syntax.",width=35%]
172
+
173
+ . Under *Build History* on the left, click *#1* to access the details for this
174
+ particular Pipeline run.
175
+ . Click *Console Output* to see the full output from the Pipeline run. The
176
+ following output shows a successful run of your Pipeline.
177
+ +
178
+ [.boxshadow]
179
+ image:pipeline/hello-world-console-output.png[alt = "Jenkins Classic UI for 'example-pipeline', showing the Console Output tab for build #1, executed by the admin user, with a successful finish status.",width=90%]
180
+
181
+ +
182
+ *Notes:*
183
+
184
+ * You can also access the console output directly from the Dashboard by clicking
185
+ the colored globe to the left of the build number (e.g. *#1*).
186
+ * Defining a Pipeline through the classic UI is convenient for testing Pipeline
187
+ code snippets, or for handling simple Pipelines or Pipelines that do not
188
+ require source code to be checked out/cloned from a repository. As mentioned
189
+ above, unlike ``Jenkinsfile``s you define through Blue Ocean
190
+ (link:#through-blue-ocean[above]) or in source control
191
+ (link:#defining-a-pipeline-in-scm[below]), ``Jenkinsfile``s entered into
192
+ the *Script* text area of Pipeline projects are stored by Jenkins itself,
193
+ within the Jenkins home directory. Therefore, for greater control and
194
+ flexibility over your Pipeline, particularly for projects in source control
195
+ that are likely to gain complexity, it is recommended that you use
196
+ link:#through-blue-ocean[Blue Ocean] or
197
+ link:#defining-a-pipeline-in-scm[source control] to define your `Jenkinsfile`.
198
+
199
+
200
+ // Despite :sectanchors:, explicitly defining an anchor because it will be
201
+ // referenced from other documents
202
+ [[defining-a-pipeline-in-scm]]
203
+ === In SCM
204
+
205
+ Complex Pipelines are difficult to write and maintain within the
206
+ link:#through-the-classic-ui[classic UI's] *Script* text area of the Pipeline
207
+ configuration page.
208
+
209
+ To make this easier, your Pipeline's `Jenkinsfile` can be written in a text
210
+ editor or integrated development environment (IDE) and committed to source
211
+ control footnote:scm[] (optionally with the application code that Jenkins
212
+ will build). Jenkins can then check out your `Jenkinsfile` from source control
213
+ as part of your Pipeline project's build process and then proceed to execute
214
+ your Pipeline.
215
+
216
+ To configure your Pipeline project to use a `Jenkinsfile` from source control:
217
+
218
+ . Follow the procedure above for defining your Pipeline
219
+ link:#through-the-classic-ui[through the classic UI] until you reach step 5
220
+ (accessing the *Pipeline* section on the Pipeline configuration page).
221
+ . From the *Definition* field, choose the *Pipeline script from SCM* option.
222
+ . From the *SCM* field, choose the type of source control system of the
223
+ repository containing your `Jenkinsfile`.
224
+ . Complete the fields specific to your repository's source control system. +
225
+ *Tip:* If you are uncertain of what value to specify for a given field, click
226
+ its *?* icon to the right for more information.
227
+ . In the *Script Path* field, specify the location (and name) of your
228
+ `Jenkinsfile`. This location is the one that Jenkins checks out/clones the
229
+ repository containing your `Jenkinsfile`, which should match that of the
230
+ repository's file structure. The default value of this field assumes that your
231
+ `Jenkinsfile` is named "Jenkinsfile" and is located at the root of the
232
+ repository.
233
+
234
+ When you update the designated repository, a new build is triggered, as long as
235
+ the Pipeline is configured with an SCM polling trigger.
236
+ ////
237
+ XXX: The above contains a reference to ""Pipeline script from SCM" dropdown
238
+ which needs to be renamed in a future release of Pipeline:
239
+ https://issue-redirect.jenkins.io/issue/40550
240
+ ////
241
+
242
+ [TIP]
243
+ ====
244
+ Since Pipeline code (i.e. Scripted Pipeline in particular) is written in
245
+ Groovy-like syntax, if your IDE is not correctly syntax highlighting your
246
+ `Jenkinsfile`, try inserting the line `#!/usr/bin/env groovy` at the top of the
247
+ `Jenkinsfile`,
248
+ footnote:shebang[link:https://en.wikipedia.org/wiki/Shebang_(Unix)[Shebang (general definition)]]
249
+ footnotegroovy_shebang:[link:http://groovy-lang.org/syntax.html#_shebang_line[Shebang line (Groovy syntax)]]
250
+ which may rectify the issue.
251
+ ====
252
+
253
+
254
+ == Built-in Documentation
255
+
256
+ Pipeline ships with built-in documentation features to make it
257
+ easier to create Pipelines of varying complexities. This built-in documentation
258
+ is automatically generated and updated based on the plugins installed in the
259
+ Jenkins controller.
260
+
261
+ The built-in documentation can be found globally at `$\{YOUR_JENKINS_URL}/pipeline-syntax`.
262
+ The same documentation is also linked as *Pipeline Syntax* in the side-bar for any
263
+ configured Pipeline project.
264
+
265
+ [.boxshadow]
266
+ image:pipeline/classic-ui-left-column-on-item-pipeline-syntax.png[alt = "Jenkins Classic UI sidebar for 'example-pipeline', showing options: Status, Changes, Build Now, Configure, Delete Pipeline, Full Stage View, Stages, Rename and Pipeline Syntax.",width=35%]
267
+
268
+
269
+ [[snippet-generator]]
270
+ === Snippet Generator
271
+
272
+ The built-in "Snippet Generator" utility is helpful for creating bits of
273
+ code for individual steps, discovering new steps provided by plugins, or
274
+ experimenting with different parameters for a particular step.
275
+
276
+ The Snippet Generator is dynamically populated with a list of the steps
277
+ available to the Jenkins controller. The number of steps available is dependent
278
+ on the plugins installed which explicitly expose steps for use in Pipeline.
279
+
280
+ To generate a step snippet with the Snippet Generator:
281
+
282
+ . Navigate to the *Pipeline Syntax* link (referenced above) from a configured Pipeline, or at `$\{YOUR_JENKINS_URL}/pipeline-syntax`.
283
+ . Select the desired step in the *Sample Step* dropdown menu
284
+ . Use the dynamically populated area below the *Sample Step* dropdown to configure the selected step.
285
+ . Click *Generate Pipeline Script* to create a snippet of Pipeline which can be
286
+ copied and pasted into a Pipeline.
287
+
288
+
289
+ ////
290
+ XXX: The above contains a reference to "Generate Pipeline Script" button which
291
+ needs to be renamed in a future release of Pipeline:
292
+ https://issue-redirect.jenkins.io/issue/40550
293
+ ////
294
+
295
+ [.boxshadow]
296
+ image:pipeline/snippet-generator.png[alt = "Jenkins Classic UI for 'example-pipeline', showing the Pipeline Syntax page with the Snippet Generator tab selected. The snippet generated for the stage named 'Deploy' is displayed in the script block.",width=100%]
297
+
298
+ To access additional information and/or documentation about the step selected,
299
+ click on the help icon (indicated by the red arrow in the image above).
300
+
301
+ === Global Variable Reference
302
+
303
+ In addition to the Snippet Generator, which only surfaces steps, Pipeline also
304
+ provides a built-in "*Global Variable Reference*." Like the Snippet Generator,
305
+ it is also dynamically populated by plugins. Unlike the Snippet Generator
306
+ however, the Global Variable Reference only contains documentation for
307
+ *variables* provided by Pipeline or plugins, which are available for
308
+ Pipelines.
309
+
310
+ The variables provided by default in Pipeline are:
311
+
312
+ env::
313
+
314
+ Exposes environment variables, for example:
315
+ `env.PATH` or `env.BUILD_ID`. Consult the built-in global variable reference at
316
+ `$\{YOUR_JENKINS_URL}/pipeline-syntax/globals#env`
317
+ for a complete, and up to date, list of environment variables
318
+ available in Pipeline.
319
+
320
+ params::
321
+
322
+ Exposes all parameters defined for the Pipeline as a read-only
323
+ link:http://groovy-lang.org/syntax.html#_maps[Map],
324
+ for example: `params.MY_PARAM_NAME`.
325
+
326
+ currentBuild::
327
+
328
+ May be used to discover information about the currently executing Pipeline,
329
+ with properties such as `currentBuild.result`, `currentBuild.displayName`,
330
+ etc. Consult the built-in global variable reference at
331
+ `$\{YOUR_JENKINS_URL}/pipeline-syntax/globals`
332
+ for a complete, and up to date, list of properties available on `currentBuild`.
333
+
334
+ This video reviews using the `currentBuild` variable in Jenkins Pipeline.
335
+
336
+ video::gcUORgHuna4[youtube,width=800,height=420]
337
+
338
+ [[directive-generator]]
339
+ === Declarative Directive Generator
340
+
341
+ While the Snippet Generator helps with generating steps for a Scripted
342
+ Pipeline or for the `steps` block in a `stage` in a Declarative Pipeline, it
343
+ does not cover the link:../syntax#declarative-sections[sections] and
344
+ link:../syntax#declarative-directives[directives] used to define a Declarative Pipeline.
345
+ The "Declarative Directive Generator" utility helps with that.
346
+ Similar to the <<snippet-generator>>, the Directive Generator allows you
347
+ to choose a Declarative directive, configure it in a form, and generate the
348
+ configuration for that directive, which you can then use in your Declarative Pipeline.
349
+
350
+ To generate a Declarative directive using the Declarative Directive Generator:
351
+
352
+ . Navigate to the *Pipeline Syntax* link (referenced above) from a configured Pipeline,
353
+ and then click on the *Declarative Directive Generator* link in the sidepanel,
354
+ or go directly to `$\{YOUR_JENKINS_URL}/directive-generator`.
355
+ . Select the desired directive in the dropdown menu
356
+ . Use the dynamically populated area below the dropdown to configure the selected directive.
357
+ . Click *Generate Directive* to create the directive's configuration to copy
358
+ into your Pipeline.
359
+
360
+ The Directive Generator can generate configuration for nested directives,
361
+ such as conditions inside a `when` directive, but it cannot generate Pipeline steps.
362
+ For the contents of directives which contain steps,
363
+ such as `steps` inside a `stage` or conditions like `always` or `failure` inside `post`,
364
+ the Directive Generator adds a placeholder comment instead.
365
+ You will still need to add steps to your Pipeline by hand.
366
+
367
+ [pipeline]
368
+ ----
369
+ // Declarative //
370
+ stage('Stage 1') {
371
+ steps {
372
+ // One or more steps need to be included within the steps block.
373
+ }
374
+ }
375
+ // Script //
376
+ ----
377
+
378
+
379
+ == Further Reading
380
+
381
+ This section merely scratches the surface of what can be done with Jenkins
382
+ Pipeline, but should provide enough of a foundation for you to start
383
+ experimenting with a test Jenkins controller.
384
+
385
+ In the next section, <<jenkinsfile#, The Jenkinsfile>>, more Pipeline steps
386
+ will be discussed along with patterns for implementing successful, real-world,
387
+ Jenkins Pipelines.
388
+
389
+
390
+ === Additional Resources
391
+
392
+ * link:../../../pipeline/steps[Pipeline Steps Reference],
393
+ encompassing all steps provided by plugins distributed in the Jenkins Update
394
+ Center.
395
+ * link:../../../pipeline/examples[Pipeline Examples], a
396
+ community-curated collection of copyable Pipeline examples.