Jenkins Pipeline 语法

Pipeline语法

先讲Declarative Pipeline,所有声明式管道都必须包含在pipeline块中:

1
2
3
pipeline {
/* insert Declarative Pipeline here */
}

 

块里面的语句和表达式都是Groovy语法,遵循以下规则:

  1. 最顶层规定就是pipeline { }
  2. 语句结束不需要分好,一行一条语句
  3. 块中只能包含SectionsDirectivesSteps或者赋值语句
  4. 属性引用语句被当成是无参方法调用,比如input实际上就是方法input()调用

接下来我详细讲解下SectionsDirectivesSteps这三个东西

Sections

Sections在声明式管道中包含一个或多个DirectivesSteps

post

post section 定义了管道执行结束后要进行的操作。支持在里面定义很多Conditions块: always,changedfailuresuccess 和 unstable。 这些条件块会根据不同的返回结果来执行不同的逻辑。

  • always:不管返回什么状态都会执行
  • changed:如果当前管道返回值和上一次已经完成的管道返回值不同时候执行
  • failure:当前管道返回状态值为”failed”时候执行,在Web UI界面上面是红色的标志
  • success:当前管道返回状态值为”success”时候执行,在Web UI界面上面是绿色的标志
  • unstable:当前管道返回状态值为”unstable”时候执行,通常因为测试失败,代码不合法引起的。在Web UI界面上面是黄色的标志
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Declarative //
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
post { ①
always { ②
echo 'I will always say Hello again!'
}
}
}

stages

由一个或多个stage指令组成,stages块也是核心逻辑的部分。 我们建议对于每个独立的交付部分(比如Build,Test,Deploy)都应该至少定义一个stage指令。比如:

1
2
3
4
5
6
7
8
9
10
11
// Declarative //
pipeline {
agent any
stages { ①
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}

 

steps

stage中定义一系列的step来执行命令。

1
2
3
4
5
6
7
8
9
10
11
// Declarative //
pipeline {
agent any
stages {
stage('Example') {
steps { ①
echo 'Hello World'
}
}
}
}

 

Directives

jenkins中的各种指令

agent

agent指令指定整个管道或某个特定的stage的执行环境。它的参数可用使用:

  1. any - 任意一个可用的agent
  2. none - 如果放在pipeline顶层,那么每一个stage都需要定义自己的agent指令
  3. label - 在jenkins环境中指定标签的agent上面执行,比如agent { label 'my-defined-label' }
  4. node - agent { node { label 'labelName' } } 和 label一样,但是可用定义更多可选项
  5. docker - 指定在docker容器中运行
  6. dockerfile - 使用源码根目录下面的Dockerfile构建容器来运行

environment

environment定义键值对的环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Declarative //
pipeline {
agent any
environment { ①
CC = 'clang'
}
stages {
stage('Example') {
environment { ②
AN_ACCESS_KEY = credentials('my-prefined-secret-text') ③
}
steps {
sh 'printenv'
}
}
}
}

 

options

还能定义一些管道特定的选项,介绍几个常用的:

  • skipDefaultCheckout - 在agent指令中忽略源码checkout这一步骤。
  • timeout - 超时设置options { timeout(time: 1, unit: 'HOURS') }
  • retry - 直到成功的重试次数options { retry(3) }
  • timestamps - 控制台输出前面加时间戳options { timestamps() }

parameters

参数指令,触发这个管道需要用户指定的参数,然后在step中通过params对象访问这些参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Declarative //
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
}
}
}
}

 

triggers

触发器指令定义了这个管道何时该执行,一般我们会将管道和GitHub、GitLab、BitBucket关联, 然后使用它们的webhooks来触发,就不需要这个指令了。如果不适用webhooks,就可以定义两种cronpollSCM

  • cron - linux的cron格式triggers { cron('H 4/* 0 0 1-5') }
  • pollSCM - jenkins的poll scm语法,比如triggers { pollSCM('H 4/* 0 0 1-5') }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Declarative //
pipeline {
agent any
triggers {
cron('H 4/* 0 0 1-5')
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}

stage

stage指令定义在stages块中,里面必须至少包含一个steps指令,一个可选的agent指令,以及其他stage相关指令。

1
2
3
4
5
6
7
8
9
10
11
// Declarative //
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}

 

tools

定义自动安装并自动放入PATH里面的工具集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Declarative //
pipeline {
agent any
tools {
maven 'apache-maven-3.0.1' ①
}
stages {
stage('Example') {
steps {
sh 'mvn --version'
}
}
}
}

 

注:① 工具名称必须预先在Jenkins中配置好了 → Global Tool Configuration.

内置条件

  • branch - 分支匹配才执行 when { branch 'master' }
  • environment - 环境变量匹配才执行 when { environment name: 'DEPLOY_TO', value: 'production' }
  • expression - groovy表达式为真才执行 expression { return params.DEBUG_BUILD } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Declarative //
pipeline {
agent any
stages {
stage('Example Build') {
steps {
echo 'Hello World'
}
}
stage('Example Deploy') {
when {
branch 'production'
}
echo 'Deploying'
}
}
}

Steps

这里就是实实在在的执行步骤了,每个步骤step都具体干些什么东西, 前面的SectionsDirectives算控制逻辑和环境准备,这里的就是真实执行步骤。

这部分内容最多不可能全部讲完,官方Step指南 包含所有的东西。

Declared PipelineScripted Pipeline都能使用这些step,除了下面这个特殊的script

一个特殊的step就是script,它可以让你在声明管道中执行脚本,使用groovy语法,这个非常有用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Declarative //
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
script {
def browsers = ['chrome', 'firefox']
for (int i = 0; i < browsers.size(); ++i) {
echo "Testing the ${browsers[i]} browser"
}
}
script {
// 一个优雅的退出pipeline的方法,这里可执行任意逻辑
if( $VALUE1 == $VALUE2 ) {
currentBuild.result = 'SUCCESS'
return
}
}
}
}
}
}

最后列出来一个典型的Scripted Pipeline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
node('master') {
checkout scm

stage('Build') {
docker.image('maven:3.3.3').inside {
sh 'mvn --version'
}
}

stage('Deploy') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}

 

可以看到,Scripted Pipeline没那么多东西,就是定义一个node, 里面多个stage,里面就是使用Groovy语法执行各个step了,非常简单和清晰,也非常灵活。

两种Pipeline比较

Declarative Pipeline相对简单,而且不需要学习groovy语法,对于日常的一般任务完全够用, 而Scripted Pipeline可通过Groovy语言的强大特性做任何你想做的事情。

转自:https://www.xncoding.com/2017/03/22/fullstack/jenkins02.html

posted @ 2018-01-08 18:13  fuhaizi  阅读(1817)  评论(0编辑  收藏  举报