管道Pipeline
本章介绍Jenkins管理的相关内容,从运行管道到编写管道代码,甚至管道扩展。
本章适用于所有级别的Jenkins用户,但是初学者需要参考《使用Jenkins》部分理解本章涉及到的相关概念。
如果还没有了解基本的Jenkins术语和特性,请先查看《Jenkins入门手册》。
什么是Jenkins管道?
Jenkins管道 (简称管道)是一组支持在Jenkins中实现和集成持续发布管道的插件。
一个持续发布管道是一个可以从用户和客户的版本控制系统中获取软件的过程的自动化表达式。软件的每次修改(通过版本控制提交)都会为发布带来一组复杂的处理过程。这个过程包括使用稳定和可重复的方式构建软件,就像通过测试和部署的多个连续阶段构建软件一样。
管道通过《管道领域特定语言(DSL)语法》提供Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines "as code" via the Pipeline Domain Specific Language (DSL) syntax. [1: Domain-Specific Language]
通常,Jenkins管道的定义写在文本文件中,其会放在工程的源码库中。这是”管道即源码“的基础;持续发布管道可以像应用程序的其他源码一样管理和查看。 创建Jenkins文件有如下优点:
• 自动为所有的分支和拉取请求创建管道;
• 跟踪管道的执行;
• 真正的单来源;
• Single source of truth [3: en.wikipedia.org/wiki/Single_Source_of_Truth] for the Pipeline, which
can be viewed and edited by multiple members of the project.
虽然定义管理的语法,不管是web UI还是定义Jenkins文件,但是最佳实践还是在Jenkins文件中定义管道,并将其放入代码控制中。如下是一个Jenkins文件的示例:
// Declarative //
pipeline {
agent any ①
stages {
stage\('Build'\) { ② steps { ③ sh 'make' ④ }
}
stage('Test'){
steps { sh 'make check' junit 'reports/\*\*/\*.xml' ⑤ }
}
stage('Deploy') {
steps { sh 'make publish' }
}
}
}
// Script //
node {
stage('Build') {
sh 'make'
}
stage('Test') {
sh 'make check'
junit 'reports/**/*.xml'
}
stage('Deploy') {
sh 'make publish'
}
}
① 代理表示Jenkins将分配执行器和管道部分的工作区。
② stage描述管道的一个阶段。
③ steps描述运行这个阶段的各个步骤。
④ sh执行给定的命令行。
⑤ junit是一个由plugin:junit[JUnit plugin]提供的步骤插件,用来集成测试报告。
为什么使用管道?
Jenkins is, fundamentally, an automation engine which supports a number of automation patterns.
Pipeline adds a powerful set of automation tools onto Jenkins, supporting use cases that span from
simple continuous integration to comprehensive continuous delivery pipelines. By modeling a
series of related tasks, users can take advantage of the many features of Pipeline:
• Code: Pipelines are implemented in code and typically checked into source control, giving
teams the ability to edit, review, and iterate upon their delivery pipeline.
• Durable: Pipelines can survive both planned and unplanned restarts of the Jenkins master.
• Pausable: Pipelines can optionally stop and wait for human input or approval before
continuing the Pipeline run.
• Versatile: Pipelines support complex real-world continuous delivery requirements, including
the ability to fork/join, loop, and perform work in parallel.
• Extensible: The Pipeline plugin supports custom extensions to its DSL [1: Domain-Specific
Language] and multiple options for integration with other plugins.
While Jenkins has always allowed rudimentary forms of chaining Freestyle Jobs together to
perform sequential tasks, [4: Additional plugins have been used to implement complex behaviors
utilizing Freestyle Jobs such as the Copy Artifact, Parameterized Trigger, and Promoted Builds
plugins] Pipeline makes this concept a first-class citizen in Jenkins.
Building on the core Jenkins value of extensibility, Pipeline is also extensible both by users with
Pipeline Shared Libraries and by plugin developers. [5: plugin:github-organization-folder[GitHub
Organization Folder plugin]]
The flowchart below is an example of one continuous delivery scenario easily modeled in Jenkins
Pipeline:
Figure 1. Pipeline Flow
管道术语
步骤(step)
单个的、基本的步骤告诉Jenkins如何做。例如,要执行make命令,使用sh步骤:sh 'make'.当插件扩展到管道DSL,通常意味着插件执行一个新步骤。
节点(node)
大部分管道完成一个或多个声明的节点步骤。
在节点中定义工作步骤,将完成如下事情:
- Schedules the steps contained within the block to run by adding an item to the Jenkins
queue. As soon as an executor is free on a node, the steps will run.
- 为从源码版本管理检出的源码创建工作区(为每个管道指定单独的文件夹)完成工作.
注意:根据Jenkins配置,一些工作区在非激活时期不会自动清空工作区。See tickets and discussion
linked from JENKINS-2111 for more information.
阶段(Stage)
stage is a step for defining a conceptually distinct subset of the entire Pipeline, for example:
"Build", "Test", and "Deploy", which is used by many plugins to visualize or present Jenkins
Pipeline status/progress. [6: Blue Ocean, Pipeline Stage View plugin]