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

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

问题描述

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

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'){
  agent any
  when{
    branch 'master'
  }
  steps {
    echo 'run this stage - ony if the branch = master branch'
  }
}

stage('feature-branch-stuff') {
    agent label:'test-node'
    when { branch "feature/*" }
    steps {
        echo 'run this stage - only if the branch name started with feature/'
    }
}

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

stage('env-specific-stuff') {
    agent label:'test-node'
    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

更新
新的WHEN条款
REF: https://jenkins.io/blog/2018/04/09/whats-in-declarative

UPDATE
New WHEN Clause
REF: https://jenkins.io/blog/2018/04/09/whats-in-declarative

等于-比较两个值-字符串,变量,数字,布尔值- 如果相等,则返回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-以最简单的形式,如果满足以下条件,则将返回true 管道正在构建变更请求,例如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.

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

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