最基本的语法包含
- pipeline:所有有效的声明式流水线必须包含在一个 pipeline 块中
- stages:包含一系列一个或多个stage指令
- stage:stage包含在stages中进行,比如某个阶段
- steps:在阶段中具体得执行操作,一个或多个步骤
agent
指定了整个流水线特定的机器,比如多台slave节点,可以被定义在pipeline块顶层也可以根据stage层定义使用
(jenkins提供比较常用的流水线语法生成,输入对应的参数会自动生产)

- any:在任意可用代理上执行流水线阶段。agent any
- label:在配置标签得jenkins环境中可用代理上执行。agent {label ‘my slave’}
- node:agent { node { label ‘labelName’ } } 和 agent { label ‘labelName’ } 一样, 但是 node 允许额外的选项 (比如 customWorkspace )
pipeline定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| pipeline { agent { label 'test1' }
stages { stage('Hello') { steps { echo 'Hello World' } } } }
|
stage阶段定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| pipeline { agent any
stages { stage('Hello') { agent { label 'test1' } steps { echo 'Hello World' } } } }
|
post
post部分定义一个或多个steps,这些阶段根据流水线完成情况而运行
- always:无论流水线完成状态如何,都继续运行
- changed:当前流水线完成状态与
之前不同时,运行该步骤
- failure:当前流水线完成状态
失败时,运行该步骤
- success:当前流水线完成状态
成功时,运行该步骤
- unstable:当前流水线完成状态
不稳定时,运行该步骤
- aborted:当前流水线完成状态
中断时,运行该步骤
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('Hello') { steps { echo 'Hello World' } } } post { always { echo "无论如何都运行" } failure { echo "失败后运行" } success { echo "执行成功后运行" } aborted { echo "异常中断运行" } } }
|
模拟不同状态
always + success

always + aborted

always + failure

stages
包含一个或多个stage指令,至少包含一个
stage
一个stage标识一个阶段
steps
定义一些列一个或多个步骤操作
1 2 3 4 5 6 7 8 9 10 11 12 13
| pipeline { agent any
stages { stage('Hello') { steps { echo 'Hello World' sh 'date' sh 'pwd' } } } }
|
environment
定义为所有步骤的环境变量,全局变量/局部变量取决于environment在流水线的那个位置
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| pipeline { agent any environment { CC = '全局变量' }
stages { stage('Hello') { environment { VV = '局部变量' } steps { echo "this is ${CC}" echo "this is ${VV}" } } } }
|
结果

options
允许从流水线内部配置特定的选项,比如timestamps
- disableConcurrentBuilds:禁止并发构建
- skipStagesAfterUnstable:构建状态
不稳定时,跳过
- timeout:运行超时时间
- retry:失败时,重新构建的次数
- timestamps:控制台时间输出
- ansiColor:日志输出中启用ANSI颜色代码的支持
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| pipeline { agent any options { ansiColor('xterm') disableConcurrentBuilds() retry(3) timeout(time: 30, unit: 'SECONDS') timestamps() skipStagesAfterUnstable() }
stages { stage('Hello') { steps { echo "hello world" echo "\033[32mThis text is green\033[0m" } } } }
|
结果

parameters
提供用户在触发流水线时应该提供的参数列表,参数化构建
- string:字符串类型参数
- booleanParam:布尔参数
- choice:从预定义选项列表中选择
- password:密码参数
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| pipeline { agent any parameters { string (defaultValue: 'master', description: '主分支区分', name: 'BRANCH_NAME') choice (choices: ['dev', 'prod', 'pre'], description: '环境区分', name: 'ENVIRONMENT') }
stages { stage('Example') { steps { checkout scmGit(branches: [[name: '${BRANCH_NAME}']], extensions: [], userRemoteConfigs: [[credentialsId: 'ae008f10-597f-4332-b2f7-790c9b01074e', url: 'https://gitee.com/liujiangxu/dubbo-demo-web.git']]) } } stage('deploy') { steps { script { echo "Deploying to ${ENVIRONMENT} environment" } } } } }
|
结果


triggers
定义流水线被重新触发的自动化方式
- cron:执行间隔,类似任务计划
- pollSCM:在设置间隔中,jenkins会检查新的源代码更新,如果更新,会被触发
- upstream:其他工程构建后触发
cron示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| pipeline { agent any triggers { cron 'H(29-30) */4 * * * ' }
stages { stage('Example') { steps { echo 'Hello World' } } } }
|
upstream示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| pipeline { agent any triggers { upstream 'ceshi-pip' }
stages { stage('Example') { steps { echo '当ceshi-pip任务执行后,触发我自动执行' } } } }
|
pollSCM示例
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
| pipeline { agent any triggers { pollSCM '* * * * *' }
stages { stage('checkout') { steps { checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'ae008f10-597f-4332-b2f7-790c9b01074e', url: 'https://gitee.com/liujiangxu/dubbo-demo-web.git']]) echo "源代码已更新" } } stage('build') { steps { sh 'mvn clean package -DskipTests=true' } } stage('deploy') { steps { echo "实际部署步骤,如执行脚本部署等操作" } } } }
|
定义自动安装和放置PATH的工具一部分,根据全局工具配置选项自定义选择(例如不同业务需要不同的jdk8或者jdk11)如果 agent none 指定,则忽略该操作
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| pipeline { agent any tools { jdk 'openjdk11.0.18' maven 'maven1' }
stages { stage('Example') { steps { sh 'mvn --version' sh 'java -version' } } } }
|
when
指令允许流水线根据给定的条件决定是否执行该阶段。when必须包含一个条件,当包含多个条件时,所有子条件必须返回true才会执行
- branch:用于检查当前构建的分支是否与指定的分支模式匹配时执行
- environment:用于检查环境变量是否满足给定的名称和值。如果环境变量存在且值匹配时执行
- not:用于反转另一个条件的结果。如果内部条件不满足(即返回false)时执行
- allOf:用于组合多个条件,只有当所有子条件都满足时(即都返回true)时执行
- anyOf:用于组合多个条件,但与allOf不同,只要至少一个子条件满足(即返回true),就会执行
bracnch示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| pipeline { agent any
stages { stage('build on master') { when { branch 'master' } steps { echo '构建为master分支时执行' } } } }
|
environment示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| pipeline { agent any stages { stage('deploy to prod') { environment { DEPLOY_X = 'dev' } when { environment name: 'DEPLOY_X', value: 'prod' } steps { echo '当变量参数为prod环境时执行' } } } }
|
not示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| pipeline { agent any environment { DEPLOY_X = "dev" } stages { stage('not build on master') { when { not { environment name: 'DEPLOY_X', value: 'prod' } } steps { echo '当变量参数不为prod环境时执行' } } } }
|
allOf 示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| pipeline { agent any environment { DEPLOY_X = "prod" } stages { stage('not build on master') { when { allOf { environment name: 'DEPLOY_X', value: 'prod' not { branch 'master' } } }
steps { echo '当环境变量等于prod 和 分支不属于master时执行(即所有条件都满足)' } } } }
|
anyOf 示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| pipeline { agent any environment { DEPLOY_X = "prod" } stages { stage('not build on master') { when { anyOf { branch 'master' environment name: 'DEPLOY_X', value: 'prod' } }
steps { echo '当环境变量等于prod 或者 分支属于master时执行(即所有条件满足一项即可)' } } } }
|