Pipeline语法

Pipeline语法详解

分为两种:Declarative Pipeline(声明式的pipeline)、Scripted Pipeline(脚本式的pipeline)

  • 声明pipeline必须包含在一个pipeline快内

    pipeline {
        //insert Declarative Pipeline here
    }
    

代理agent

agent 指定整个Pipeline或特定阶段将在Jenkins环境中执行的位置,必须在Pipeline顶层定义,stage内为可选

是否必填
参数 如下面所描述
允许出现在 在顶级Pipeline快或每个stage快中

参数:

  • any:在任何代理上执行pipeline,例如:agent any

  • none:当在pipeline顶层使用时,将不会为整个pipeline运行分配全局代理,并且每个stage部分将需要包含自己的agent

  • label:使用提供的标签在Jenkins环境中可用的代理上执行Pipelinestage,例如:agent {label ‘my-defined-label’}

  • node:与label相同

  • docker:用于给定的容器执行Pipeline,或stage,将被动态的提供一个预先配置成基于Docker-based Pipeline的节点,或和label参数匹配的任意参数。docker还可以接受一个args参数,可以直接将参数传递给docker run命令

    agent {
        docker {
            image 'maven:3-alpine'
            label 'my-defined-label'
            args '-v /tmp:/tmp'
        }
    }
    
  • dockerfile

post

post定义在Pipeline运行结束或stage运行结束时。

是否必填
参数 没有
允许出现的位置 在顶级pipeline快和stage快中

条件:

  • always:无论Pipeline运行状态如何,都执行
  • changed:只有当前Pipeline运行的状态与先前完成的Pipeline的状态不同时,才能运行
  • failure:仅当Pipeline处于“失败”状态时才运行,通常在web UI中用红色表示
  • success:仅当Pipeline处于“成功状态时才运行”,通常在web UI中用蓝色或绿色表示
  • unstable:只有当前Pipeline具有“不稳定“状态时才允许,通常是由测试失败、代码违例等引起的,在web UI中通常用黄色来表示
  • aborted:只有当前Pipeline处于“中止“状态时,才会运行通常是由于Pipeline被手动中止,在web UI中用灰色表示

例如:

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post { 
        always { 
            echo 'I will always say Hello again!'
        }
    }
}

stages stage

包含一个或多个阶段(stage)指令,是Pipeline描述大部分工作的地方,一个stages可包含多个stage

stages一般跟在agentoptions等后面

是否必填
参数 没有
允许出现在 Pipeline内只有一次

例如

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

步骤 steps

steps定义了在给定stage指令中执行的一系列或一个步骤

是否必填
参数 没有
允许出现在 每个stage快内

例如

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Example') {
            steps { 
                echo 'Hello World'
            }
        }
    }
}

环境 environment

environment定义一系列的键值对,这些键值对将被定义为所有step或特定stagestep的环境变量具体取决于environment指令位于Pipeline中的位置

是否必填
参数 没有
允许出现在 Pipeline快内或stage

例如

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    environment { 
        CC = 'clang'
    }
    stages {
        stage('Example') {
            environment { 
                AN_ACCESS_KEY = credentials('my-prefined-secret-text') 
            }
            steps {
                sh 'printenv'
            }
        }
    }
}

选项 options

options指令允许在pioeline内配置Pipeline专用选项。

可用选项

  • timeout:设置Pipeline运行的超时时间,超时后Jenkins会自动中止Pipeline

例如

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    options {
        timeout(time: 1, unit: 'HOURS') 
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

参数parameters

parameters指令提供用户在触发Pipeline时应提供的参数列表

是否必填
参数 如下
允许出现在 pipeline快内,只有一次

触发器 triggers

triggers指令定义了Pipeline应重新触发的自动化方式

是否必填
参数
允许 pipeline快内
  • cron
  • pollSCM

工具 tools

自动自动安装和放置工具的部分PATH。如果指定agent none,这将被忽略.

工具名称必须在Jenkins 管理Jenkins --> 全局工具管理中预配

是否必填
参数 没有
允许出现在 pipeline快或stage快内

支持的工具

  • maven
  • jdk
  • gradle
Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    tools {
        maven 'apache-maven-3.0.1' 
    }
    stages {
        stage('Example') {
            steps {
                sh 'mvn --version'
            }
        }
    }
}

输入 input

stageinput指令允许您使用input步骤提示输入,pipeline会处于暂停状态直到用户确定或者取消

配置选项

  • message:必填,在用户提交input时呈现给用户
  • id:可选的标识符,默认为stage名称
  • ok:报表单上OK按钮的可选文本
  • submitter:允许提交此input选项的用户或外部名列表,用逗号分割,默认允许任何用户
  • parameters: 用于提示提供的可选参数列表 。

例如

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Example') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "alice,bob"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
                }
            }
            steps {
                echo "Hello, ${PERSON}, nice to meet you."
            }
        }
    }
}

条件 when

允许pipeline根据指定的条件确定是否执行该stagewhen指令必须至少包含一个条件。

复杂的嵌套可以使用条件:notallOfanyOf

是否必填
参数 没有
允许出现在 在stage指令内

内置指令

  • branch

    当正在构建的分支与给出的分支模式匹配时执行stage,例如:when { branch 'master' }。请注意,这仅适用于多分支Pipeline。

  • environment

    当指定的环境变量设置为给定值时执行stage,例如: when { environment name: 'DEPLOY_TO', value: 'production' }

  • expression

    当指定的Groovy表达式求值为true时执行stage,例如: when { expression { return params.DEBUG_BUILD } }

  • not

    当嵌套条件为false时执行stage。必须包含一个条件。例如:when { not { branch 'master' } }

  • allOf

    当所有嵌套条件都为真时,执行舞台。必须至少包含一个条件。例如:when { allOf { branch 'master'; environment name: 'DEPLOY_TO', value: 'production' } }

  • anyOf

    当至少一个嵌套条件为真时执行舞台。必须至少包含一个条件。例如:when { anyOf { branch 'master'; branch 'staging' } }

例如

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'
            }
        }
        stage('Example Deploy') {
            when {
                expression { BRANCH_NAME ==~ /(production|staging)/ }
                anyOf {
                    environment name: 'DEPLOY_TO', value: 'production'
                    environment name: 'DEPLOY_TO', value: 'staging'
                }
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

并行的stage(Parallel)

例如

pipeline {
    agent any
    stages {
        stage('Non-Parallel Stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('Parallel Stage') {
            when {
                branch 'master'
            }
            failFast true
            parallel {
                stage('Branch A') {
                    agent {
                        label "for-branch-a"
                    }
                    steps {
                        echo "On Branch A"
                    }
                }
                stage('Branch B') {
                    agent {
                        label "for-branch-b"
                    }
                    steps {
                        echo "On Branch B"
                    }
                }
                stage('Branch C') {
                    agent {
                        label "for-branch-c"
                    }
                    stages {
                        stage('Nested 1') {
                            steps {
                                echo "In stage Nested 1 within Branch C"
                            }
                        }
                        stage('Nested 2') {
                            steps {
                                echo "In stage Nested 2 within Branch C"
                            }
                        }
                    }
                }
            }
        }
    }
}
posted @ 2020-07-17 14:40  ryhonour  阅读(820)  评论(0)    收藏  举报