【云原生】devops之jenkins中pipeline语法(4)

本文详细介绍了JenkinsPipeline中的`when`指令用于实现条件判断,允许根据特定条件决定阶段是否执行,包括内置的多种条件如分支匹配、标签检查等。此外,还讨论了如何使用矩阵(matrix)来定义并行运行的多维度配置组合,加速自动化构建和测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:

        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') {
            // ...}
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sun cat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值