前言:

        pipeline语法分类一般来说,有四种。分别是环境配置、阶段步骤、行为动作、逻辑判断。

 

四、逻辑判断


(1)when

        该when指令允许管道根据给定条件确定是否应执行阶段。

        该when指令必须至少包含一个条件。如果when指令包含多个条件,则所有子条件都必须返回 true 才能执行阶段。

内置条件:

条件

描述

样例

branch

当正在构建的分支与给定的分支模式(ANT 样式路径 glob)匹配时执行阶段。

when { branch 'master' }

buildingTag

在构建构建标签时执行阶段

when { buildingTag() }

changelog

如果构建的 SCM 更改日志包含给定的正则表达式模式,则执行该阶段

when { changelog '.*^\\[DEPENDENCY\\] .+$' }

changeset

如果构建的 SCM 变更集包含一个或多个与给定模式匹配的文件,则执行该阶段

when { changeset "**/*.js" }

changeRequest

如果当前构建是针对“更改请求”(也称为 GitHub Bitbucket 上的拉取请求、GitLab 上的合并请求、Gerrit 中的更改等),则执行该阶段。当没有传递参数时,阶段会在每个更改请求上运行

when { changeRequest() }

environment

当指定环境变量设置为给定值时执行该阶段

when { environment name: 'DEPLOY_TO', value: 'production' }

equals

执行期望值等于实际值的阶段

when { equals expected: 2, actual: currentBuild.number }

expression

当指定的 Groovy 表达式计算结果为 true 时执行该阶段

when { expression { return params.DEBUG_BUILD } }

tag

TAG_NAME如果变量与给定模式匹配,则执行该阶段

when { tag "release-*" }

not

当嵌套条件为假时执行阶段。必须包含一个条件

when { not { branch 'master' } }

allOf

当所有嵌套条件都为真时执行该阶段。必须包含至少一个条件

when { allOf { branch 'master'; environment name: 'DEPLOY_TO', value: 'production' } }

anyOf

当至少一个嵌套条件为真时执行该阶段。必须包含至少一个条件

when { anyOf { branch 'master'; branch 'staging' } }

triggeredBy

当当前构建被给定的参数触发时执行阶段。

  • when { triggeredBy 'SCMTrigger' }
  • when { triggeredBy 'TimerTrigger' }
  • when { triggeredBy 'BuildUpstreamCause' }
  • when { triggeredBy cause: "UserIdCause", detail: "vlinde" }

 

示例:

1)单一条件

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'}
        }
        stage('Example Deploy') {
            when {
                branch 'production'}
            steps {
                echo 'Deploying'}
        }
    }
}

2)多条件

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'}
        }
        stage('Example Deploy') {
            when {
                branch 'production'environment name: 'DEPLOY_TO', value: 'production'}
            steps {
                echo 'Deploying'}
        }
    }
}

3)嵌套条件(与前面示例相同的行为)

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'}
        }
        stage('Example Deploy') {
            when {
                allOf {
                    branch 'production'environment name: 'DEPLOY_TO', value: 'production'}
            }
            steps {
                echo 'Deploying'}
        }
    }
}

4)多重条件和嵌套条件

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'}
        }
        stage('Example Deploy') {
            when {
                branch 'production'anyOf {
                    environment name: 'DEPLOY_TO', value: 'production'environment name: 'DEPLOY_TO', value: 'staging'}
            }
            steps {
                echo 'Deploying'}
        }
    }
}

 

(2)Matrix

        pipeline中的阶段可能有一个matrix部分定义要并行运行的名称-值组合的多维矩阵。我们将这些组合称为矩阵中的“单元”。矩阵中的每个单元可以包括一个或多个阶段,以使用该单元的配置顺序运行。

        matrix部分必须包括axes部分和stage部分。“axes”部分定义矩阵中每个axes的值。

1)axe

        该axes部分指定了一个或多个axis指令。每个都axis包含一个name和一个列表values。每个轴的所有值都与其他值组合以生成单元格。

示例:

(具有 3 个单元的单轴)

matrix {
    axes {
        axis {
            name 'PLATFORM'values 'linux', 'mac', 'windows'}
    }
    // ...}

具有 12 个单元的两轴(三乘四)

matrix {
    axes {
        axis {
            name 'PLATFORM'values 'linux', 'mac', 'windows'}
        axis {
            name 'BROWSER'values 'chrome', 'edge', 'firefox', 'safari'}
    }
    // ...}

具有 3 个单元的单轴,每个单元运行三个阶段 - “构建测试部署

matrix {
    axes {
        axis {
            name 'PLATFORM'values 'linux', 'mac', 'windows'}
    }
    stages {
        stage('build') {
            // ...}
        stage('test') {
            // ...}
        stage('deploy') {
            // ...}
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐