Jenkins(三)

Jenkins(三)

pipeline语法

pipeline语法可以分为

  • 「声明式流水线」

  • 「脚本化流水线」

但是由于脚本化流水线需要学习Groovy,所以我放弃了。

虽然放弃了通篇学习一整门语言,但是为了在声明式流水线中使用简单的逻辑操作还是需要学习一点Groovy的基础内容。

基础Groovy

  • 定义变量
1
2
def x = "abc"
def y = 1
  • Groovy语句最后的分号不是必须的
  • 方法调用可以参略括号
  • 支持命名参数
1
2
3
4
5
def func(String a, String b){
return a + " " + b
}
// 调用
func b = "Lee", a = "Bruce"
  • 支持默认参数值
1
2
3
4
def sayHello(String name = "humans"){
print "hello ${name}"
}
sayHello()
  • 支持单引号,双引号
    • 双引号支持插值
    • 单引号不支持插值
1
2
3
def name = 'world'
print "hello ${name}" //输出:hello world
print 'hello ${name}' //输出:hello ${name}
  • 支持三引号

    • 三双引号支持插值
    • 三单引号不支持插值
  • 支持闭包

1
2
3
4
5
6
7
8
9
10
11
12
def func = {print "hello world"} //定义闭包
func() //调用闭包

// 定义一个接收闭包参数的pipeline函数
def pipeline(a){
a()
}
pipeline(func) // 调用pipeline
// 把func直接写入⬇️
pipeline({print "hello world"})
// 括号可以省略⬇️
pipeline{print "hello world"}
  • 闭包的另类用法
1
2
3
4
5
6
7
8
9
10
def stage(String name, a){
println name //输出字符串name
a() //调用闭包函数a
}
// 正常使用stage函数⬇️
stage("stage name",{print "hello world"})
// Groovy提供的另一种使用stage函数的方法⬇️
stage("stage name"){
print "hello world"
}

pipeline的组成

Jenkins pipeline其实就是基于Groovy语言实现的一种DSL(领域特定语言),用于描述整条流水线上如何进行的。

流水线的内容包括:执行编译,打包,测试,输出测试报告等。

1
2
3
4
5
6
7
8
9
10
pipeline {
agent any
stages {
stage('build'){
steps{
echo "hello world"
}
}
}
}
  • pipeline:整条流水线
  • stages:流水线可以有多个stages,每个stages至少包含一个stage
  • stage:阶段,每个阶段都必须有名称,例如build
  • steps:具体步骤,ech就是一个步骤。在一个stage中有且只有一个steps
  • agent:执行位置,在某个(物理机,虚拟机,Docker)环境中执行

步骤

类似于echo,sh等步骤,Jenkins有很多插件可以通过一定的修改来当作步骤使用

适配Jenkins pipeline的插件列表:

https://github.com/jenkinsci/pipeline-plugin/blob/master/COMPATIBILITY.md

通过「流水线语法」可以可视化的进行插件步骤的编辑与生成,降低学习成本。

流水线语法自动生成1流水线语法自动生成2

post部分

在pipeline执行之后会执行post部分

根据pipeline的完成状态,post可以分成不同的条件块

  • always:完成后就执行
  • changed:完成状态与上一次不一致执行
  • fixed:失败或不稳定执行
  • regression:上一次成功,这一次失败/不稳定/中止(aborted)时执行
  • aborted:中止(aborted)时执行
  • failure:失败时执行
  • success:成功时执行
  • unstable:不稳定时执行
  • cleanup:清理条件块。无论完成状态是什么
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
pipeline{
agent any
stages{
stage('build'){
steps{
echo "build stage"
}
post{
always{
echo "stage post alwasy"
}
}
}
}
post{
changed{
echo "pipeline post changed"
}
always{
echo "pipeline post alwasy"
}
success{
echo "pipeline post success"
}
}
}

支持的命令

  • Environment:用于设置环境变量,可定义在stage或pipeline部分

  • tools:自动下载安装指定的工具,并将其加入PATH变量中,可定义在stage或pipeline部分

  • input:暂停pipeline,提示输入内容,定义在stage部分

  • options:配置Jenkins pipeline本身的选项,可以定义在stage或pipeline中。

    • buildDiscarder:保存历史构建数
    1
    2
    3
    options{
    buildDiscarder(logRotator(numToKeepStr:10))
    }
    • disableConcurrentBuilds:禁止同时执行
    1
    2
    3
    options{
    disableConcurrentBuilds()
    }
    • overrideIndexTriggers
    • skipDefaultCheckout
    • skipStagesAfterUnstable
    • checkoutToSubdirectory:指定代码检出路径
    1
    2
    3
    options{
    checkoutToSubdirectory('subdir')
    }
    • newContainerPerStage:当agent为docker或dockerfile时,指定在同一个Jenkins节点上,每个stage分别运行在一个新的Docker容器中,而不是所有stage都运行在一个容器中。
    1
    2
    3
    options{
    newContainerPerStage()
    }
    • timeout:运行的超时时间
    1
    2
    3
    options{
    timeout(time:10,unit:'HOURS')
    }
    • retry:在失败时,重新尝试整个管道的指定次数
    1
    2
    3
    options{
    retry(4)
    }
    • timestamps:显示运行时候的时间
  • parallel:并行执行多个step

  • parameters:参数化构建部分

  • triggers:触发器(定时/钩子)

  • when:当满足条件时才执行

没有例子的直接百度咯,仿照着写多测测就行了。

在声明式流水线中使用脚本

在一些不得不用代码的地方使用script就可以写Groovy代码了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pipeline{
agent any
stages{
stage('Example'){
steps{
script{
def browsers = ['chrome','firefox']
for (int i = 0; i < browsers.size(); ++i){
echo "Testing the ${browsers[i]} browsers"
}
}
}
}
}
}

修改Jenkinsfile并推送至GitHub。

运行

 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!