管道入门
Jenkins管道是一组完成持续交付的执行和集成的Jenkins插件。 Pipeline provides an extensible set of tools for modeling simple-tocomplex delivery pipelines "as code" via the Pipeline DSL. [7: Domain-Specific Language]
本节介绍Jenkins管道的关键概念,并介绍在运行的Jenkins实例中定义和执行管道的基本方法。
先决条件
要使用Jenkins管道,需要:
• Jenkins 2.x或以上版本(1.642.3之前的版本不推荐使用)
• 管道插件[8: Pipeline plugin]
要学习如何安装和管理插件,请参考Managing Plugins.
定义管道
管道脚本使用Groovy编写。Scripted Pipeline is written in Groovy. The relevant bits of Groovy syntax will be introduced as
necessary in this document, so while an understanding of Groovy is helpful, it is not required to work with Pipeline.
基本管道将采用如下方式创建:
• 在Jenkins的web界面上直接输入脚本。
• 通过创建Jenkins文件从工程源码库中检出。
The syntax for defining a Pipeline with either approach is the same, but while Jenkins supports
entering Pipeline directly into the web UI, it’s generally considered best practice to define the
Pipeline in a Jenkinsfile which Jenkins will then load directly from source control. [9:
en.wikipedia.org/wiki/Source_control_management]
在Web UI中定义管道
To create a basic Pipeline in the Jenkins web UI, follow these steps:
• 在Jenkins主页中单击【New Item】按钮.
• 输入管道的名称,选择管道,并单击【OK】.
注意:Jenkins用管道名称在磁盘上创建目录。管道名称中包含空格的话,路径会有问题。
- 在【Script】文本框,输入管道,单击【Save】
- 单击【Build Now】 to run the Pipeline.
- Click #1 under "Build History" and then click Console Output to see the full output from the Pipeline.
The example above shows a successful run of a basic Pipeline created in the Jenkins web UI, using two steps.
Jenkinsfile (Scripted Pipeline)
node {
echo
'
Hello World
'
}
node allocates an executor and workspace in the Jenkins environment. |
|
---|---|
echo writes simple string in the Console Output. |
Defining a Pipeline in SCM
Complex Pipelines are hard to write and maintain within the text area of the Pipeline configuration page. To make this easier, Pipeline can also be written in a text editor and checked into source control as a Jenkinsfile
which Jenkins can load via the Pipeline Script from SCM option.
To do this, select Pipeline script from SCM when defining the Pipeline.
With the Pipeline script from SCM option selected, you do not enter any Groovy code in the Jenkins UI; you just indicate by specifying a path where in source code you want to retrieve the pipeline from. When you update the designated repository, a new build is triggered, as long as the Pipeline is configured with an SCM polling trigger.
The first line of a Jenkinsfile should be #!/usr/bin/env groovy [4][5] which text editors, IDEs, GitHub, etc will use to syntax highlight the Jenkinsfile properly as Groovy code. |
|
---|---|
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 instance.
The built-in documentation can be found globally at: localhost:8080/pipeline-syntax/, assuming you have a Jenkins instance running on localhost port 8080. The same documentation is also linked as Pipeline Syntax in the side-bar for any configured Pipeline project.
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 instance. 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 localhost:8080/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.
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
Environment variables accessible from Scripted Pipeline, for example: env.PATH
or env.BUILD_ID
. Consult the built-in Global Variable Reference 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 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 for a complete, and up to date, list of properties available on currentBuild
.
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 instance.
In the next section, The Jenkinsfile, more Pipeline steps will be discussed along with patterns for implementing successful, real-world, Jenkins Pipelines.
Additional Resources
Pipeline Steps Reference, encompassing all steps provided by plugins distributed in the Jenkins Update Center.
Pipeline Examples, a community-curated collection of copyable Pipeline examples.
3.en.wikipedia.org/wiki/Source_control_management