jenkins pipeline
目录:
Pipeline介绍
Pipeline脚本是由Groovy语言实现, 支持两种语法
- Declarative 声明式(在Pipeline plugin 2.5中引入)
- Scripted Pipeline 脚本式
# Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Build') {
steps {
//
}
}
stage('Test') {
steps {
//
}
}
stage('Deploy') {
steps {
//
}
}
}
}
# Jenkinsfile (Scripted Pipeline)
node {
stage('Build') {
//
}
stage('Test') {
//
}
stage('Deploy') {
//
}
}
语法
agent
必须存在,agent必须在pipeline块内的顶层定义,但stage内是否使用使可选的
- 参数:any/none/label/node/docker/dockerfile
- 常用选项:label/cuetomWorkspace/reuseNode
agent { label 'my-label' }
agent {
node {
label 'my-label'
customWorkspace '/some/other/path'
}
}
agent {
docker {
image 'nginx:1.12.2'
label 'my-label'
args '-v /tmp:/tmp'
}
}
post
post不是必须的,用于pipeline的最外层或者stage{}中
pipeline {
agent any
stages {
stage('Example'){
steps {
echo 'Hello world'
}
}
}
post {
always {
echo 'say goodbay'
}
}
}
stages
stages是必须的,包括顺序执行的一个或多个stage命令,在pipeline内仅能使用一次,通常位于agent/options后面
steps
steps是必须的,steps位于stage指令块内部,包括一个或多个step。仅有一个step的情况下可以忽略关键字step及其{},例子如上
options
options不是必须的,预定义pipeline专有的配置信息,仅可定义一次
pipeline {
agent any
options{
timeout(time:1,unit: 'HOURS')
}
...
}
environment
environment 不是必须的,environment定义了一组全局的环境变量键值对,存在于pipeline{}或者stage指令内。执行特殊方法
parameters
parameters不是必须的,定义参数化构建的参数可选参数booleanParam,choice,file,text,password,run,string
paramenters {
choice(name:'PerformMavenRelease',choices:'False\nTrue',description:'desc')
password(name:'CredsToUse',description:'Apassword to build with',defaultValue:'')
}
environment {
BUILD_USR_CHOICE="${params.PerformMavenRelease}"
BUILD_USR_CREDS="${params.CredsToUse}"
}
triggers
triggers不是必须的 定义pipeline被自动触发的方式选项 cron、pollSCM、upstream
triggers {cron('H 4/* 0 0 1-5')}
triggers {pollSCM('H 4/* 0 0 1-5')}
triggers {upstream(upstreamProjects:'job1,job2',threshold:hudson.model.Result.SUCCESS)}
credentials
credentials可以获取jenkins中预定义的凭证明文内容
environment {CC='clang'}
environment {AN_ACCESS_KEY = credentials('my-prefined-secret-text')}
steps {sh 'printenv'}
参数传递
局部自定义变量
def username = 'Jenkins'
echo "Hello Mr.${username}"
局部环境变量
withEnv(['MYTOOL_HOME=/usr/local/mytool']){
sh '$MYTOOL_HOME/bin/start'
}
全局环境变量
environment {CC='clang'}
echo "Compiler is ${env.CC}"
全局参数化构建变量
parameters {string(name:'Jenkins',defaultValue:'Hello',description:'How should I greet the world')}
ehco "${params.Greeting} World!"
流程控制
判断
when仅用于stage内部
when的内置条件为:
- when {branch 'master'}
- when {environment name:'DEPLOY_TO',value:'production'}
#当有环境变量 name 为 DEPLOY_TO 值是 production 条件成立
- when {expression {return params.DEBUG_BUILD}}
#表达式返回值为真时
- when {not {branch 'master'}}
- when {allOf {branch 'master'; environment name:'DEBUG_TO',value:'production'}}
#allOf 所有条件都满足时
- when {anyOf {branch 'master' ; branch 'staging'}}
#anyOf有一个条件满足时即可
判断和异常处理
流程控制if/else条件
node {
stage('Example'){
if(env.BRANCH_NAME == 'master'){
echo 'I only execute on the master branch'
}else {
echo 'Iexecute elsewhere'
}
}
}
异常处理
try/catch/finally
node{
stage('Example'){
try{
sh 'exit 1'
}
catch (exc) {
echo 'something failed,I should sound the klaxons!'
throw
}
}
}
循环
for循环仅存在域脚本式pipeline中,但是可以通过在声明式pipeline中调用script step来执行
pipeline {
agent any
stages {
stage('Example'){
steps{
echo 'Hello world!'
script {
def browsers = ['chrome','firefox']
for (int i = 0;i < browers.size();++i){
echo "Testing the ${browsers[i]} browser"
}
}
}
}
}
}
更多
参考官方文档