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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
layout: section
|
| 3 |
+
title: Pipeline Development Tools
|
| 4 |
+
---
|
| 5 |
+
ifdef::backend-html5[]
|
| 6 |
+
:description:
|
| 7 |
+
:author:
|
| 8 |
+
:sectanchors:
|
| 9 |
+
:toc:
|
| 10 |
+
ifdef::env-github[:imagesdir: ../resources]
|
| 11 |
+
ifndef::env-github[:imagesdir: ../../resources]
|
| 12 |
+
:hide-uri-scheme:
|
| 13 |
+
endif::[]
|
| 14 |
+
ifdef::basebackend-dockbook[:imagesdir: doc/book/resources]
|
| 15 |
+
// Show 2/3 of the Blue ocean admonitions
|
| 16 |
+
// :pipeline-visualization-admonition: true
|
| 17 |
+
:pipeline-creation-admonition: true
|
| 18 |
+
|
| 19 |
+
= Pipeline Development Tools
|
| 20 |
+
|
| 21 |
+
Jenkins Pipeline includes
|
| 22 |
+
<<getting-started#built-in-documentation, built-in documentation>> and the
|
| 23 |
+
<<getting-started#snippet-generator, Snippet Generator>> which are key resources
|
| 24 |
+
when developing Pipelines. They provide detailed help and information that is customized
|
| 25 |
+
to the currently installed version of Jenkins and related plugins.
|
| 26 |
+
In this section, we'll discuss other tools and resources
|
| 27 |
+
that may help with development of Jenkins Pipelines.
|
| 28 |
+
|
| 29 |
+
[[linter]]
|
| 30 |
+
== Command-line Pipeline Linter
|
| 31 |
+
|
| 32 |
+
Jenkins can validate, or
|
| 33 |
+
"link:https://en.wikipedia.org/wiki/Lint_(software)[lint]",
|
| 34 |
+
a Declarative Pipeline from the command line before actually running it.
|
| 35 |
+
This can be done using a Jenkins CLI command or by making an
|
| 36 |
+
HTTP POST
|
| 37 |
+
request with appropriate parameters.
|
| 38 |
+
We recommended using the
|
| 39 |
+
<<../managing/cli#ssh, SSH interface>>
|
| 40 |
+
to run the linter. See the <<../managing/cli#, Jenkins CLI documentation>> for details on how to properly configure
|
| 41 |
+
Jenkins for secure command-line access.
|
| 42 |
+
|
| 43 |
+
.Linting via the CLI with SSH
|
| 44 |
+
[source,bash]
|
| 45 |
+
----
|
| 46 |
+
# ssh (Jenkins CLI)
|
| 47 |
+
# JENKINS_PORT=[sshd port on controller]
|
| 48 |
+
# JENKINS_HOST=[Jenkins controller hostname]
|
| 49 |
+
ssh -p $JENKINS_PORT $JENKINS_HOST declarative-linter < Jenkinsfile
|
| 50 |
+
----
|
| 51 |
+
|
| 52 |
+
.Linting via HTTP POST using `curl`
|
| 53 |
+
[source,bash]
|
| 54 |
+
----
|
| 55 |
+
# curl (REST API)
|
| 56 |
+
# These instructions assume that the security realm of Jenkins is something other than "None" and you have an account.
|
| 57 |
+
# JENKINS_URL=[root URL of Jenkins controller]
|
| 58 |
+
# JENKINS_AUTH=[your Jenkins username and an API token in the following format: your_username:api_token]
|
| 59 |
+
curl -X POST --user "$JENKINS_AUTH" -F "jenkinsfile=<Jenkinsfile" "$JENKINS_URL/pipeline-model-converter/validate"
|
| 60 |
+
----
|
| 61 |
+
|
| 62 |
+
=== Examples
|
| 63 |
+
|
| 64 |
+
Below are two examples of the Pipeline Linter in action.
|
| 65 |
+
This first example shows the output of the linter when it is passed
|
| 66 |
+
an invalid `Jenkinsfile`, one that is missing part of the `agent` declaration.
|
| 67 |
+
|
| 68 |
+
.Jenkinsfile
|
| 69 |
+
[source,groovy]
|
| 70 |
+
----
|
| 71 |
+
pipeline {
|
| 72 |
+
agent
|
| 73 |
+
stages {
|
| 74 |
+
stage ('Initialize') {
|
| 75 |
+
steps {
|
| 76 |
+
echo 'Placeholder.'
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
----
|
| 82 |
+
|
| 83 |
+
.Linter output for invalid Jenkinsfile
|
| 84 |
+
[source,bash]
|
| 85 |
+
----
|
| 86 |
+
# pass a Jenkinsfile that does not contain an "agent" section
|
| 87 |
+
ssh -p 8675 localhost declarative-linter < ./Jenkinsfile
|
| 88 |
+
Errors encountered validating Jenkinsfile:
|
| 89 |
+
WorkflowScript: 2: Not a valid section definition: "agent". Some extra configuration is required. @ line 2, column 3.
|
| 90 |
+
agent
|
| 91 |
+
^
|
| 92 |
+
|
| 93 |
+
WorkflowScript: 1: Missing required section "agent" @ line 1, column 1.
|
| 94 |
+
pipeline }
|
| 95 |
+
^
|
| 96 |
+
----
|
| 97 |
+
|
| 98 |
+
In this second example, the `Jenkinsfile` has been updated to include the
|
| 99 |
+
missing `any` on `agent`. The linter now reports that the Pipeline is valid.
|
| 100 |
+
|
| 101 |
+
.Jenkinsfile
|
| 102 |
+
[source,groovy]
|
| 103 |
+
----
|
| 104 |
+
pipeline {
|
| 105 |
+
agent any
|
| 106 |
+
stages {
|
| 107 |
+
stage ('Initialize') {
|
| 108 |
+
steps {
|
| 109 |
+
echo 'Placeholder.'
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
----
|
| 115 |
+
|
| 116 |
+
.Linter output for valid Jenkinsfile
|
| 117 |
+
[source,bash]
|
| 118 |
+
----
|
| 119 |
+
ssh -p 8675 localhost declarative-linter < ./Jenkinsfile
|
| 120 |
+
Jenkinsfile successfully validated.
|
| 121 |
+
----
|
| 122 |
+
|
| 123 |
+
== Blue Ocean Editor
|
| 124 |
+
|
| 125 |
+
The <<../blueocean/pipeline-editor#, Blue Ocean Pipeline Editor>> provides a link:https://en.wikipedia.org/wiki/WYSIWYG[WYSIWYG] way to create Declarative Pipelines.
|
| 126 |
+
The editor offers a structural view of all the stages, parallel branches, and steps in a Pipeline.
|
| 127 |
+
The editor validates Pipeline changes as they are made, eliminating many errors before they are even committed.
|
| 128 |
+
Behind the scenes it still generates Declarative Pipeline code.
|
| 129 |
+
|
| 130 |
+
include::doc/book/blueocean/_blue-ocean-status.adoc[]
|
| 131 |
+
|
| 132 |
+
[[replay]]
|
| 133 |
+
== "Replay" Pipeline Runs with Modifications
|
| 134 |
+
|
| 135 |
+
Typically a Pipeline will be defined inside of the classic Jenkins web UI,
|
| 136 |
+
or by committing to a `Jenkinsfile` in source control. Unfortunately,
|
| 137 |
+
neither approach is ideal for rapid iteration, or prototyping, of a Pipeline.
|
| 138 |
+
The "Replay" feature allows for quick modifications and execution of an existing
|
| 139 |
+
Pipeline without changing the Pipeline configuration or creating a new commit.
|
| 140 |
+
|
| 141 |
+
=== Usage
|
| 142 |
+
|
| 143 |
+
To use the "Replay" feature:
|
| 144 |
+
|
| 145 |
+
. Select a previously completed run in the build history.
|
| 146 |
+
+
|
| 147 |
+
image::pipeline/replay-previous-run.png[alt = "Build history section of a Jenkins Pipeline showing all the previously executed builds. The history includes: Build #2 and Build #3, which ran successfully; Build #4 and Build #5, which failed; and Build #6, which ran successfully. Click on the latest successful build, which is Build #6 in this case."]
|
| 148 |
+
+
|
| 149 |
+
. Click "Replay" in the left menu.
|
| 150 |
+
+
|
| 151 |
+
image::pipeline/replay-left-bar.png[alt = "Jenkins Classic UI sidebar for 'example-pipeline' build #6 showing options: Status, Changes, Console Output, Edit Build Information, Delete Build #6, Timings, Pipeline Overview, Pipeline Console, Restart from Stage, Replay, Pipeline Steps, Workspaces, and Previous Build."]
|
| 152 |
+
+
|
| 153 |
+
. Make modifications and click "Run". In this example, we changed "ruby-2.3" to "ruby-2.4".
|
| 154 |
+
+
|
| 155 |
+
image::pipeline/replay-modified.png[alt = "The replay section of the Jenkins 'example-pipeline' for build #6 displays the pipeline script, where the Ruby version has been changed from 2.3 to 2.4. A 'Pipeline Syntax' option and a blue 'Run' button is shown below the script."]
|
| 156 |
+
+
|
| 157 |
+
. Check the results of changes.
|
| 158 |
+
|
| 159 |
+
Once you are satisfied with the changes,
|
| 160 |
+
you can use Replay to view them again, copy them back to your Pipeline job
|
| 161 |
+
or `Jenkinsfile`, and then commit them using your usual engineering processes.
|
| 162 |
+
|
| 163 |
+
=== Features
|
| 164 |
+
|
| 165 |
+
* *Can be called multiple times on the same run* -
|
| 166 |
+
allows for easy parallel testing of different changes.
|
| 167 |
+
* *Can also be called on Pipeline runs that are still in-progress* -
|
| 168 |
+
As long as a Pipeline contained syntactically correct Groovy and was able to start,
|
| 169 |
+
it can be Replayed.
|
| 170 |
+
* *Referenced Shared Library code is also modifiable* - If a Pipeline run references a
|
| 171 |
+
<<shared-libraries#, Shared Library>>, the code from the shared library will
|
| 172 |
+
also be shown and modifiable as part of the Replay page.
|
| 173 |
+
* *Access Control via dedicated "Run / Replay" permission* -
|
| 174 |
+
implied by "Job / Configure". If Pipeline is not configurable (e.g. Branch Pipeline of a Multibranch) or "Job / Configure" is not granted, users still can experiment with Pipeline Definition via Replay
|
| 175 |
+
* *Can be used for Re-run* -
|
| 176 |
+
users lacking "Run / Replay" but who are granted "Job / Build" can still use Replay to run a build again with the same definition.
|
| 177 |
+
*Note*: The label switches to "Rebuild" in that case.
|
| 178 |
+
|
| 179 |
+
=== Limitations
|
| 180 |
+
|
| 181 |
+
* *Pipeline runs with syntax errors cannot be replayed* -
|
| 182 |
+
meaning their code cannot be viewed and any changes made in them cannot be retrieved.
|
| 183 |
+
When using Replay for more significant modifications, save your changes
|
| 184 |
+
to a file or editor outside of Jenkins before running them.
|
| 185 |
+
See link:https://issue-redirect.jenkins.io/issue/37589[JENKINS-37589]
|
| 186 |
+
* *Replayed Pipeline behavior may differ from runs started by other methods* -
|
| 187 |
+
For Pipelines that are not part of a Multi-branch Pipeline,
|
| 188 |
+
the commit information may differ for the original run and the Replayed run.
|
| 189 |
+
See link:https://issue-redirect.jenkins.io/issue/36453[JENKINS-36453]
|
| 190 |
+
|
| 191 |
+
== IDE Integrations
|
| 192 |
+
|
| 193 |
+
=== Eclipse Jenkins Editor
|
| 194 |
+
The `Jenkins Editor` Eclipse plugin can be found on
|
| 195 |
+
link:https://marketplace.eclipse.org/content/jenkins-editor[Eclipse Marketplace].
|
| 196 |
+
This special text editor provides some features for defining pipelines e.g:
|
| 197 |
+
|
| 198 |
+
- Validate pipeline scripts by <<#linter,Jenkins Linter Validation>>. Failures are shown as eclipse markers
|
| 199 |
+
- An Outline with dedicated icons (for declarative Jenkins pipelines )
|
| 200 |
+
- Syntax / keyword highlighting
|
| 201 |
+
- Groovy validation
|
| 202 |
+
|
| 203 |
+
NOTE: The Jenkins Editor Plugin is a third-party tool that is not supported
|
| 204 |
+
by the Jenkins Project.
|
| 205 |
+
|
| 206 |
+
=== VisualStudio Code Jenkins Pipeline Linter Connector
|
| 207 |
+
|
| 208 |
+
The `Jenkins Pipeline Linter Connector` extension for
|
| 209 |
+
link:https://code.visualstudio.com/[VisualStudio Code]
|
| 210 |
+
takes the file that you have currently opened, pushes it to your Jenkins Server and displays the validation result in VS Code.
|
| 211 |
+
|
| 212 |
+
You can find the extension from within the VS Code extension browser or at the following url: https://marketplace.visualstudio.com/items?itemName=janjoerke.jenkins-pipeline-linter-connector
|
| 213 |
+
|
| 214 |
+
The extension adds four settings entries to VS Code which select the Jenkins server you want to use for validation.
|
| 215 |
+
|
| 216 |
+
* `jenkins.pipeline.linter.connector.url` is the endpoint at which your Jenkins Server expects the POST request, containing your Jenkinsfile which you want to validate. Typically this points to __http://<your_jenkins_server:port>/pipeline-model-converter/validate__.
|
| 217 |
+
* `jenkins.pipeline.linter.connector.user` allows you to specify your Jenkins username.
|
| 218 |
+
* `jenkins.pipeline.linter.connector.pass` allows you to specify your Jenkins password.
|
| 219 |
+
* `jenkins.pipeline.linter.connector.crumbUrl` has to be specified if your Jenkins Server has CRSF protection enabled. Typically this points to __http://<your_jenkins_server:port>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)__.
|
| 220 |
+
|
| 221 |
+
=== Neovim nvim-jenkinsfile-linter plugin
|
| 222 |
+
|
| 223 |
+
The link:https://github.com/ckipp01/nvim-jenkinsfile-linter[nvim-jenkinsfile-linter]
|
| 224 |
+
Neovim plugin allows you to validate a Jenkinsfile by using the Pipeline Linter API
|
| 225 |
+
of your Jenkins controller and report any existing diagnostics in your editor.
|
| 226 |
+
|
| 227 |
+
=== Atom linter-jenkins package
|
| 228 |
+
|
| 229 |
+
The link:https://atom.io/packages/linter-jenkins[linter-jenkins] Atom package allows
|
| 230 |
+
you to validate a Jenkins file by using the Pipeline Linter API of a running Jenkins.
|
| 231 |
+
You can install it directly from the Atom package manager. It needs also to install
|
| 232 |
+
link:https://atom.io/packages/language-jenkinsfile[Jenkinsfile language support in Atom]
|
| 233 |
+
|
| 234 |
+
=== Sublime Text Jenkinsfile package
|
| 235 |
+
|
| 236 |
+
The link:https://github.com/june07/sublime-Jenkinsfile[Jenkinsfile] Sublime Text package allows
|
| 237 |
+
you to validate a Jenkinsfile by using the Pipeline Linter API of a running Jenkins controller over
|
| 238 |
+
a secure channel (SSH). You can install it directly from the Sublime Text package manager.
|
| 239 |
+
|
| 240 |
+
You can find the package from within the Sublime Text interface via the Package Control package, at GitHub, or packagecontrol.io:
|
| 241 |
+
|
| 242 |
+
* link:https://github.com/june07/sublime-Jenkinsfile[https://github.com/june07/sublime-Jenkinsfile]
|
| 243 |
+
* link:https://packagecontrol.io/packages/Jenkinsfile[https://packagecontrol.io/packages/Jenkinsfile]
|
| 244 |
+
|
| 245 |
+
[[unit-test]]
|
| 246 |
+
== Pipeline Unit Testing Framework
|
| 247 |
+
|
| 248 |
+
The link:https://github.com/jenkinsci/JenkinsPipelineUnit[Pipeline Unit Testing Framework]
|
| 249 |
+
allows you to
|
| 250 |
+
link:https://en.wikipedia.org/wiki/Unit_testing[unit test]
|
| 251 |
+
Pipelines and <<shared-libraries#, Shared Libraries>>
|
| 252 |
+
before running them in full. It provides a mock execution environment where real
|
| 253 |
+
Pipeline steps are replaced with mock objects that you can use to check for expected
|
| 254 |
+
behavior. New and rough around the edges, but promising.
|
| 255 |
+
The link:https://github.com/jenkinsci/JenkinsPipelineUnit/blob/master/README.md[README]
|
| 256 |
+
for that project contains examples and usage instructions.
|