Jenkins pipeline

最近由于项目需要,接触到了Jenkins 2.0版本,其中最重要的特性就是提供了对pipeline的支持。
简单的来说,就是把Jenkins1.0版本中,Project中的相关配置信息,如SVN/Git的配置,Parameter的配置等都变成Code,即Pipeline as Code。
这样的优势为可以通过写代码的形式配置Project,且Jenkins中内置了常用的steps。实现了构建步骤代码化、构建过程视图化。

声明性管道与脚本管道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
声明式管道 和 脚本化管道 的主要区别在于它们的语法和灵活性

声明性管道是一个相对较新的特性,它提出了pipeline as code的概念,它使管道代码更易于读写。管道代码是在 Jenkinsfile 文件中编写的,可以将其存放到源代码管理系统(如Git)。

脚本化管道是一个传统方式。在这个管道中,Jenkinsfile 被写在 Jenkins UI实例上。

虽然这两条管道都是基于Groovy DSL的,但是脚本化的流水线使用更严格的基于Groovy的语法,因为它是Groovy基金会上构建的第一条管道。由于这个Groovy脚本并不是所有用户都想要的,所以引入声明性管道是为了提供一种更简单、更具选择性的Groovy语法。


声明性管道在标记为“pipeline”的块中定义,而脚本化管道在“node”中定义。





选择Declarative Pipeline还是Scripted Pipeline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
最开始的Pipeline plugin,支持的只有一种脚本类型,就是Scripted Pipeline;
Declarative Pipeline为Pipeline plugin在2.5版本之后新增的一种脚本类型,
与原先的Scripted Pipeline一样,都可以用来编写脚本。

使用哪一种脚本格式呢,我又纠结了,也查询了些资料。
https://stackoverflow.com/questions/43484979/jenkins-scripted-pipeline-or-declarative-pipeline
http://jenkins-ci.361315.n4.nabble.com/Declarative-pipelines-vs-scripted-td4891792.html
https://e.printstacktrace.blog/jenkins-scripted-pipeline-vs-declarative-pipeline-the-4-practical-differences/
https://medium.com/getamis/%E7%B2%BE%E9%80%9A-jenkins-pipeline-part1-e8ef48d3543e
https://medium.com/@c9s/%E7%B2%BE%E9%80%9A-jenkins-pipeline-part2-transform-node-step-%E4%BB%A5%E5%8F%8A-declarative-pipeline-4942ddf67dd5
https://medium.com/@c9s/%E7%B2%BE%E9%80%9A-jenkins-pipeline-part3-declarative-pipeline-and-its-implementation-20ee724a4a2a

最后,我还是选择了Declarative Pipeline,这也是后续Open Blue Ocean所支持的类型。
相对而言,Declarative Pipeline比较简单,如果Groovy很熟的,用Scripted Pipeline可能更顺手。
另外,Declarative Pipeline中,是可以内嵌Scripted Pipeline代码的。


Declarative Pipeline fundamentals 声明性管道与脚本管道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
In Declarative Pipeline syntax, the pipeline block defines all the work done throughout your entire Pipeline.

Jenkinsfile (Declarative Pipeline)

pipeline {
agent any // Execute this Pipeline or any of its stages, on any available agent.
stages {
stage('Build') { //Defines the "Build" stage.
steps {
// Perform some steps related to the "Build" stage.
}
}
stage('Test') { //Defines the "Test" stage.
steps {
// Perform some steps related to the "Test" stage.
}
}
stage('Deploy') { //Defines the "Deploy" stage.
steps {
// Perform some steps related to the "Deploy" stage.
}
}
}
}




Scripted Pipeline fundamentals 声明性管道与脚本管道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43


In Scripted Pipeline syntax, one or more node blocks do the core work throughout the entire Pipeline. Although this is not a mandatory requirement of Scripted Pipeline syntax, confining your Pipeline’s work inside of a node block does two things:

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.

Creates a workspace (a directory specific to that particular Pipeline) where work can be done on files checked out from source control.

Caution: Depending on your Jenkins configuration, some workspaces may not get automatically cleaned up after a period of inactivity. See tickets and discussion linked from JENKINS-2111 for more information.





Jenkinsfile (Scripted Pipeline)

node { //Execute this Pipeline or any of its stages, on any available agent.
stage('Build') { // Defines the "Build" stage. stage blocks are optional in Scripted Pipeline syntax. However, implementing stage blocks in a Scripted Pipeline provides clearer visualization of each `stage’s subset of tasks/steps in the Jenkins UI.
// Perform some steps related to the "Build" stage.
}
stage('Test') { //Defines the "Test" stage.
// Defines the "Test" stage
}
stage('Deploy') { //Defines the "Deploy" stage.
// Defines the "Deploy" stage.
}
}