jenkins高级篇 pipeline系列之-—04 语法

官网说明
Pipeline Steps Reference

1. 简介

Pipeline 支持两种语法:Declarative Pipeline(在 Pipeline 2.5 中引入,结构化方式)和 Scripted Pipeline。Pipeline 最基本的部分是 “step”。step 告诉 Jenkins 要做什么,并且作为 Pipeline 的基本构建块。
为与 BlueOcean 脚本编辑器兼容,通常建议使用 Declarative Pipeline 的方式进行编写。

2. Declarative Pipeline

Declarative Pipeline中有效的基本语句和表达式遵循与Groovy语法相同的规则 ,但有以下例外:

  1. Pipeline的顶层必须是块,具体来说是:pipeline
  2. 没有分号作为语句分隔符。每个声明必须在自己的一行
  3. 块只能包含章节Sections,指令Directives, 步骤Steps或赋值语句。
  4. 属性引用语句被视为无参数方法调用。所以例如,input被视为input()

Declarative Pipeline语法- Sections(章节)

Sections 通常包含一个或多个 Directives 或 Steps

关键字 意义 常见选项
agent 作用:告知Jenkins选择那台节点机器去执行Pipeline代码。这个指令在 pipeline 块顶层必须定义。 any---任何
none---管道不指定,模块,步骤中指定
label---指定标签
node---agent { node { label 'labelName' } },等同于 agent { label 'labelName' },但 node 允许其他选项(如 customWorkspace)
docker--动态供应一个 docker 节点,docker 还可以接受args,直接传递给 docker run 调用
dockerfile---使用从 Dockerfile 源存储库中包含的容器来构建执行。为使用此选项,Jenkinsfile 必须从 Multibranch Pipeline 或 “Pipeline from SCM"加载。
kubernetes
post 作用:post部分定义了一个或多个附加步骤,一般用来发送消息或者邮件通知。在 Pipeline 或 stage 运行结束时操作。 在post代码块区域,支持多种条件指令,这些指令有always,changed,failure,success,unstable,和aborted。
stages 允许出现至少一次stages。一个stages下可以包含多个stage,一个stage 下至少有一个steps。
steps 只支持steps,不支持在steps {…} 里面嵌套写step{…}。
Example 1
pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                println "Build" 
            }
        }
        stage('Test') { 
            steps {
                println "Test" 
            }
        }
        stage('Deploy') { 
            steps {
                println "Deploy" 
            }
        }
    }
}

Declarative Pipeline语法- Directives(指令)

关键字 意义 常见选项
environment 设置环境变量,指定一系列键值对 credentials()
options 允许在Pipeline中配置特定于Pipeline的内置选项。 buildDiscarder, checkoutToSubdirectory, disableConcurrentBuilds, newContainerPerStage, overrideIndexTriggers, preserveStashes, quietPeriod, retry, skipDefaultCheckout,skipStagesAfterUnstable, timeout,timestamps
parameters 提供了用户在触发Pipeline时应提供的参数列表,可通过页面配置 string, text, booleanParam, choice, file, password
triggers 定义了触发管道的自动方式。 对于与GitHub或BitBucket等源集成的管道,可能不需要触发器,因为基于webhooks的集成可能已经存在。 目前可用的触发器是cron,pollSCM和upstream,可通过页面配置 cron, pollSCM, upstream
stage 位于阶段部分。Pipeline所做的所有实际工作都将包含在一个或多个阶段指令中。
tools 定义自动安装和放置工具的部分PATH。如果agent none指定,这将被忽略。引用的工具名必须在Manage Jenkins → Global Tool Configuration中预定义 maven,jdk,gradle
input 等待用户输入,根据输入值继续后续的流程 message, id, ok, submitter, submitterParameter, parametersv
when 符合条件,则执行。when指令必须至少包含一个条件。 beforeAgent, branch, buildingTag, changelog, changeset, changeRequest, environment, equal, expression, tag, not, allof, anyof
Example 2 input
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."
            }
        }
    }
}
Example 3 when
pipeline {
    agent any
    environment {
        quick_test = false
    }
    stages {
        stage('Example Build') {
            steps {
                script {
                    echo 'Hello World'
                }
            }
        }
        stage('Example Deploy') {
            when {
                expression { 
                   return  (quick_test == “true” )
                    
                }
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

Declarative Pipeline语法- 多个stage的关系:顺序和并行

顺序stage

Example 4
pipeline {
    agent none
    stages {
        stage('Non-Sequential Stage') {
            agent {
                label 'for-non-sequential'
            }
            steps {
                echo "On Non-Sequential Stage"
            }
        }
        stage('Sequential') {
            agent {
                label 'for-sequential'
            }
            environment {
                FOR_SEQUENTIAL = "some-value"
            }
            stages {
                stage('In Sequential 1') {
                    steps {
                        echo "In Sequential 1"
                    }
                }
                stage('In Sequential 2') {
                    steps {
                        echo "In Sequential 2"
                    }
                }
                stage('Parallel In Sequential') {
                    parallel {
                        stage('In Parallel 1') {
                            steps {
                                echo "In Parallel 1"
                            }
                        }
                        stage('In Parallel 2') {
                            steps {
                                echo "In Parallel 2"
                            }
                        }
                    }
                }
            }
        }
    }
}

并行stage
failFast true,只要有一个不通过,就中止运行pipeline下面的代码

Example 4
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"
                            }
                        }
                    }
                }
            }
        }
    }
}

3. Scripted Pipeline

Scripted Pipeline 是基于 groovy 的一种 DSL 语言,与 Declarative pipeline 相比提供了更巨大的灵活性和可扩展性。

流程控制 Flow Control

依赖于Groovy表达式,例如if/else条件,try/catch/finally块

与普通Groovy的区别 Differences from plain Groovy

由于 pipeline 的个性化需求,比如在重新启动 jenkins 后要求 pipeline 脚本仍然可以运行,那么 pipeline 脚本必须将相关数据做序列化。这个设计要求,一些Groovy习惯用法集合并不完全支持,例如 collection.each { item → /* perform operation */ }

posted @ 2020-05-03 19:47  雨 燕  阅读(1266)  评论(0编辑  收藏  举报