workflow-agent / data /jenkins_docs /getting-started.adoc
derangedsaturnian's picture
Create data/jenkins_docs/getting-started.adoc
c01b4ff verified
Raw History Blame Contribute Delete
17.7 kB
---
layout: section
title: Getting started with Pipeline
---
ifdef::backend-html5[]
:description:
:author:
:sectanchors:
:toc:
ifdef::env-github[:imagesdir: ../resources]
ifndef::env-github[:imagesdir: ../../resources]
:hide-uri-scheme:
endif::[]
// Show 2/3 of the Blue ocean admonitions
// :pipeline-visualization-admonition: true
:pipeline-creation-admonition: true
= Getting started with Pipeline
As mentioned link:../[previously], Jenkins Pipeline is a suite of
plugins that supports implementing and integrating continuous delivery pipelines
into Jenkins. Pipeline provides an extensible set of tools for modeling
simple-to-complex delivery pipelines "as code" via the Pipeline DSL.
footnote:dsl[link:https://en.wikipedia.org/wiki/Domain-specific_language[Domain-specific language]]
This section describes how to get started with creating your Pipeline project in
Jenkins and introduces you to the various ways that a `Jenkinsfile` can be
created and stored.
== Prerequisites
To use Jenkins Pipeline, you will need:
* Jenkins 2.x or later (older versions back to 1.642.3 may work but are not
recommended)
* Pipeline plugin,
footnote:pipeline[link:https://plugins.jenkins.io/workflow-aggregator[Pipeline plugin]]
which is installed as part of the "suggested plugins" (specified when running
through the link:../../installing/linux/#setup-wizard[Post-installation setup wizard]
after link:../../installing[installing Jenkins]).
Read more about how to install and manage plugins in
link:../../managing/plugins[Managing Plugins].
== Defining a Pipeline
Both
link:../#declarative-versus-scripted-pipeline-syntax[Declarative and Scripted Pipeline]
are DSLs footnote:dsl[] to describe portions of your software delivery
pipeline. Scripted Pipeline is written in a limited form of
link:http://groovy-lang.org/semantics.html[Groovy syntax].
Relevant components of Groovy syntax will be introduced as required throughout
this documentation, so while an understanding of Groovy is helpful, it is not
required to work with Pipeline.
A Pipeline can be created in one of the following ways:
* <<through-blue-ocean>> - after setting up a Pipeline project in Blue Ocean,
the Blue Ocean UI helps you write your Pipeline's `Jenkinsfile` and commit it
to source control.
* <<through-the-classic-ui>> - you can enter a basic Pipeline directly in
Jenkins through the classic UI.
* link:#defining-a-pipeline-in-scm[In SCM] - you can write a `Jenkinsfile`
manually, which you can commit to your project's source control repository.
footnote:scm[link:https://en.wikipedia.org/wiki/Version_control[Source control management]]
The syntax for defining a Pipeline with either approach is the same, but while
Jenkins supports entering Pipeline directly into the classic UI, it is
generally considered best practice to define the Pipeline in a `Jenkinsfile`
which Jenkins will then load directly from source control.
This video provides basic instructions on how to write both Declarative and Scripted Pipelines.
.Writing a Pipeline script in Jenkins
video::TiTrcFEsj7A[youtube,width=800,height=420]
=== Through Blue Ocean
If you are new to Jenkins Pipeline, the Blue Ocean UI helps you
link:../../blueocean/creating-pipelines[set up your Pipeline project], and
automatically creates and writes your Pipeline (i.e. the `Jenkinsfile`) for you
through the graphical Pipeline editor.
As part of setting up your Pipeline project in Blue Ocean, Jenkins configures a
secure and appropriately authenticated connection to your project's source
control repository. Therefore, any changes you make to the `Jenkinsfile` via
Blue Ocean's Pipeline editor are automatically saved and committed to source
control.
Read more about Blue Ocean in the link:../../blueocean[Blue Ocean] chapter and
link:../../blueocean/getting-started[Getting started with Blue Ocean] page.
include::doc/book/blueocean/_blue-ocean-status.adoc[]
=== Through the classic UI
A `Jenkinsfile` created using the classic UI is stored by Jenkins itself (within
the Jenkins home directory).
To create a basic Pipeline through the Jenkins classic UI:
. If required, ensure you are logged in to Jenkins.
. From the Jenkins Dashboard, select *New Item*.
+
[.boxshadow]
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%]
. In the *Enter an item name* field, specify the name for your new Pipeline
project. +
*Caution:* Jenkins uses this item name to create directories on disk. It is
recommended to avoid using spaces in item names, since doing so may uncover
bugs in scripts that do not properly handle spaces in directory paths.
. Scroll down and click *Pipeline*, then click *OK* at the end of the page to
open the Pipeline configuration page (whose *General* tab is selected).
+
[.boxshadow]
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%]
. Click the *Pipeline* tab in the side panel of the page to scroll down to the
*Pipeline* section. +
*Note:* If instead you are defining your `Jenkinsfile` in source control,
follow the instructions in link:#defining-a-pipeline-in-scm[In SCM] below.
. In the *Pipeline* section, ensure that the *Definition* field indicates the
*Pipeline script* option.
. Enter your Pipeline code into the *Script* text area. +
For instance, copy the following Declarative example Pipeline code (below the
_Jenkinsfile ( ... )_ heading) or its Scripted version equivalent and paste
this into the *Script* text area. (The Declarative example below is used
throughout the remainder of this procedure.)
+
[pipeline]
----
// Declarative //
pipeline {
agent any // <1>
stages {
stage('Stage 1') {
steps {
echo 'Hello world!' // <2>
}
}
}
}
// Script //
node { // <3>
stage('Stage 1') {
echo 'Hello World' // <2>
}
}
----
<1> `agent` instructs Jenkins to allocate an executor (on any available
agent/node in the Jenkins environment) and workspace for the entire Pipeline.
<2> `echo` writes simple string in the console output.
<3> `node` effectively does the same as `agent` (above).
+
[.boxshadow]
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%]
+
*Note:* You can also select from canned _Scripted_ Pipeline examples from the
*try sample Pipeline* option at the top right of the *Script* text area. Be
aware that there are no canned Declarative Pipeline examples available from this
field.
. Click *Save* to open the Pipeline project/item view page.
. On this page, click *Build Now* on the left to run the Pipeline.
+
[.boxshadow]
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%]
. Under *Build History* on the left, click *#1* to access the details for this
particular Pipeline run.
. Click *Console Output* to see the full output from the Pipeline run. The
following output shows a successful run of your Pipeline.
+
[.boxshadow]
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%]
+
*Notes:*
* You can also access the console output directly from the Dashboard by clicking
the colored globe to the left of the build number (e.g. *#1*).
* Defining a Pipeline through the classic UI is convenient for testing Pipeline
code snippets, or for handling simple Pipelines or Pipelines that do not
require source code to be checked out/cloned from a repository. As mentioned
above, unlike ``Jenkinsfile``s you define through Blue Ocean
(link:#through-blue-ocean[above]) or in source control
(link:#defining-a-pipeline-in-scm[below]), ``Jenkinsfile``s entered into
the *Script* text area of Pipeline projects are stored by Jenkins itself,
within the Jenkins home directory. Therefore, for greater control and
flexibility over your Pipeline, particularly for projects in source control
that are likely to gain complexity, it is recommended that you use
link:#through-blue-ocean[Blue Ocean] or
link:#defining-a-pipeline-in-scm[source control] to define your `Jenkinsfile`.
// Despite :sectanchors:, explicitly defining an anchor because it will be
// referenced from other documents
[[defining-a-pipeline-in-scm]]
=== In SCM
Complex Pipelines are difficult to write and maintain within the
link:#through-the-classic-ui[classic UI's] *Script* text area of the Pipeline
configuration page.
To make this easier, your Pipeline's `Jenkinsfile` can be written in a text
editor or integrated development environment (IDE) and committed to source
control footnote:scm[] (optionally with the application code that Jenkins
will build). Jenkins can then check out your `Jenkinsfile` from source control
as part of your Pipeline project's build process and then proceed to execute
your Pipeline.
To configure your Pipeline project to use a `Jenkinsfile` from source control:
. Follow the procedure above for defining your Pipeline
link:#through-the-classic-ui[through the classic UI] until you reach step 5
(accessing the *Pipeline* section on the Pipeline configuration page).
. From the *Definition* field, choose the *Pipeline script from SCM* option.
. From the *SCM* field, choose the type of source control system of the
repository containing your `Jenkinsfile`.
. Complete the fields specific to your repository's source control system. +
*Tip:* If you are uncertain of what value to specify for a given field, click
its *?* icon to the right for more information.
. In the *Script Path* field, specify the location (and name) of your
`Jenkinsfile`. This location is the one that Jenkins checks out/clones the
repository containing your `Jenkinsfile`, which should match that of the
repository's file structure. The default value of this field assumes that your
`Jenkinsfile` is named "Jenkinsfile" and is located at the root of the
repository.
When you update the designated repository, a new build is triggered, as long as
the Pipeline is configured with an SCM polling trigger.
////
XXX: The above contains a reference to ""Pipeline script from SCM" dropdown
which needs to be renamed in a future release of Pipeline:
https://issue-redirect.jenkins.io/issue/40550
////
[TIP]
====
Since Pipeline code (i.e. Scripted Pipeline in particular) is written in
Groovy-like syntax, if your IDE is not correctly syntax highlighting your
`Jenkinsfile`, try inserting the line `#!/usr/bin/env groovy` at the top of the
`Jenkinsfile`,
footnote:shebang[link:https://en.wikipedia.org/wiki/Shebang_(Unix)[Shebang (general definition)]]
footnotegroovy_shebang:[link:http://groovy-lang.org/syntax.html#_shebang_line[Shebang line (Groovy syntax)]]
which may rectify the issue.
====
== Built-in Documentation
Pipeline ships with built-in documentation features to make it
easier to create Pipelines of varying complexities. This built-in documentation
is automatically generated and updated based on the plugins installed in the
Jenkins controller.
The built-in documentation can be found globally at `$\{YOUR_JENKINS_URL}/pipeline-syntax`.
The same documentation is also linked as *Pipeline Syntax* in the side-bar for any
configured Pipeline project.
[.boxshadow]
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%]
[[snippet-generator]]
=== Snippet Generator
The built-in "Snippet Generator" utility is helpful for creating bits of
code for individual steps, discovering new steps provided by plugins, or
experimenting with different parameters for a particular step.
The Snippet Generator is dynamically populated with a list of the steps
available to the Jenkins controller. The number of steps available is dependent
on the plugins installed which explicitly expose steps for use in Pipeline.
To generate a step snippet with the Snippet Generator:
. Navigate to the *Pipeline Syntax* link (referenced above) from a configured Pipeline, or at `$\{YOUR_JENKINS_URL}/pipeline-syntax`.
. Select the desired step in the *Sample Step* dropdown menu
. Use the dynamically populated area below the *Sample Step* dropdown to configure the selected step.
. Click *Generate Pipeline Script* to create a snippet of Pipeline which can be
copied and pasted into a Pipeline.
////
XXX: The above contains a reference to "Generate Pipeline Script" button which
needs to be renamed in a future release of Pipeline:
https://issue-redirect.jenkins.io/issue/40550
////
[.boxshadow]
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%]
To access additional information and/or documentation about the step selected,
click on the help icon (indicated by the red arrow in the image above).
=== Global Variable Reference
In addition to the Snippet Generator, which only surfaces steps, Pipeline also
provides a built-in "*Global Variable Reference*." Like the Snippet Generator,
it is also dynamically populated by plugins. Unlike the Snippet Generator
however, the Global Variable Reference only contains documentation for
*variables* provided by Pipeline or plugins, which are available for
Pipelines.
The variables provided by default in Pipeline are:
env::
Exposes environment variables, for example:
`env.PATH` or `env.BUILD_ID`. Consult the built-in global variable reference at
`$\{YOUR_JENKINS_URL}/pipeline-syntax/globals#env`
for a complete, and up to date, list of environment variables
available in Pipeline.
params::
Exposes all parameters defined for the Pipeline as a read-only
link:http://groovy-lang.org/syntax.html#_maps[Map],
for example: `params.MY_PARAM_NAME`.
currentBuild::
May be used to discover information about the currently executing Pipeline,
with properties such as `currentBuild.result`, `currentBuild.displayName`,
etc. Consult the built-in global variable reference at
`$\{YOUR_JENKINS_URL}/pipeline-syntax/globals`
for a complete, and up to date, list of properties available on `currentBuild`.
This video reviews using the `currentBuild` variable in Jenkins Pipeline.
video::gcUORgHuna4[youtube,width=800,height=420]
[[directive-generator]]
=== Declarative Directive Generator
While the Snippet Generator helps with generating steps for a Scripted
Pipeline or for the `steps` block in a `stage` in a Declarative Pipeline, it
does not cover the link:../syntax#declarative-sections[sections] and
link:../syntax#declarative-directives[directives] used to define a Declarative Pipeline.
The "Declarative Directive Generator" utility helps with that.
Similar to the <<snippet-generator>>, the Directive Generator allows you
to choose a Declarative directive, configure it in a form, and generate the
configuration for that directive, which you can then use in your Declarative Pipeline.
To generate a Declarative directive using the Declarative Directive Generator:
. Navigate to the *Pipeline Syntax* link (referenced above) from a configured Pipeline,
and then click on the *Declarative Directive Generator* link in the sidepanel,
or go directly to `$\{YOUR_JENKINS_URL}/directive-generator`.
. Select the desired directive in the dropdown menu
. Use the dynamically populated area below the dropdown to configure the selected directive.
. Click *Generate Directive* to create the directive's configuration to copy
into your Pipeline.
The Directive Generator can generate configuration for nested directives,
such as conditions inside a `when` directive, but it cannot generate Pipeline steps.
For the contents of directives which contain steps,
such as `steps` inside a `stage` or conditions like `always` or `failure` inside `post`,
the Directive Generator adds a placeholder comment instead.
You will still need to add steps to your Pipeline by hand.
[pipeline]
----
// Declarative //
stage('Stage 1') {
steps {
// One or more steps need to be included within the steps block.
}
}
// Script //
----
== Further Reading
This section merely scratches the surface of what can be done with Jenkins
Pipeline, but should provide enough of a foundation for you to start
experimenting with a test Jenkins controller.
In the next section, <<jenkinsfile#, The Jenkinsfile>>, more Pipeline steps
will be discussed along with patterns for implementing successful, real-world,
Jenkins Pipelines.
=== Additional Resources
* link:../../../pipeline/steps[Pipeline Steps Reference],
encompassing all steps provided by plugins distributed in the Jenkins Update
Center.
* link:../../../pipeline/examples[Pipeline Examples], a
community-curated collection of copyable Pipeline examples.