詹金斯管道中的条件步骤/阶段 [英] Conditional step/stage in Jenkins pipeline

查看:13
本文介绍了詹金斯管道中的条件步骤/阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在构建特定分支时如何运行构建步骤/阶段?

How do you run a build step/stage only if building a specific branch?

例如,仅当分支称为 deployment 时才运行部署步骤,其他一切都保持不变.

For example, run a deployment step only if the branch is called deployment, leaving everything else the same.

推荐答案

在声明式管道语法中做同样的事情,下面是几个例子:

Doing the same in declarative pipeline syntax, below are few examples:

stage('master-branch-stuff') {
    when {
        branch 'master'
    }
    steps {
        echo 'run this stage - ony if the branch = master branch'
    }
}


stage('feature-branch-stuff') {
    when {
        branch 'feature/*'
    }
    steps {
        echo 'run this stage - only if the branch name started with feature/'
    }
}


stage('expression-branch') {
    when {
        expression {
            return env.BRANCH_NAME != 'master';
        }
    }
    steps {
        echo 'run this stage - when branch is not equal to master'
    }
}


stage('env-specific-stuff') {
    when { 
        environment name: 'NAME', value: 'this' 
    }
    steps {
        echo 'run this stage - only if the env name and value matches'
    }
}

更有效的方法即将出现 -https://issues.jenkins-ci.org/browse/JENKINS-41187
还请看 -https://jenkins.io/doc/book/pipeline/syntax/#when

More effective ways coming up - https://issues.jenkins-ci.org/browse/JENKINS-41187
Also look at - https://jenkins.io/doc/book/pipeline/syntax/#when

指令 beforeAgent true 可以设置以避免启动代理来运行条件,如果条件不需要 git state 来决定是否运行:

The directive beforeAgent true can be set to avoid spinning up an agent to run the conditional, if the conditional doesn't require git state to decide whether to run:

when { beforeAgent true; expression { return isStageConfigured(config) } }

发布帖文档

更新
新的 WHEN 子句
参考:https://jenkins.io/blog/2018/04/09/whats-in-declarative

equals - 比较两个值 - 字符串、变量、数字、布尔值 -如果它们相等,则返回 true.老实说,我不确定我们是怎么错过的早点添加这个!你可以做不等于"使用 not 进行比较{ 等于 ... } 组合.

equals - Compares two values - strings, variables, numbers, booleans - and returns true if they’re equal. I’m honestly not sure how we missed adding this earlier! You can do "not equals" comparisons using the not { equals ... } combination too.

changeRequest - 以最简单的形式,如果这将返回 truePipeline 正在构建更改请求,例如 GitHub 拉取请求.您还可以对变更请求进行更详细的检查,允许你问这是针对主人的变更请求吗?分行?"还有更多.

changeRequest - In its simplest form, this will return true if this Pipeline is building a change request, such as a GitHub pull request. You can also do more detailed checks against the change request, allowing you to ask "is this a change request against the master branch?" and much more.

buildingTag - 一个简单的条件,只检查管道是否是针对 SCM 中的标签运行,而不是针对分支或特定提交参考.

buildingTag - A simple condition that just checks if the Pipeline is running against a tag in SCM, rather than a branch or a specific commit reference.

tag - 更详细的 buildingTag 等价物,允许您检查针对标签名称本身.

tag - A more detailed equivalent of buildingTag, allowing you to check against the tag name itself.

这篇关于詹金斯管道中的条件步骤/阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆