【问题标题】:Jenkins Conditional Stages詹金斯条件阶段
【发布时间】:2019-09-19 16:51:32
【问题描述】:

我正在使用声明式 Jenkins 管道。我有一个阶段可以从用户那里获得输入,例如

//Input 
    stage ('Manual Input'){
        agent none
        steps {
            input message: "Please Approve", ok: 'Approve'
        }
    }

我不希望在 Jenkins 等待手动步骤完成时阻止任何代理,因此我使用了agent none。

我想知道是否有办法让这个阶段有条件地执行。

详细说明:

pipeline {
agent none

parameters {
    choice(choices: "No\nYes",
        description: 'Choose Yes to wait for Manual Input',
        name: 'Input')
}
stages {

    stage ('Stage_1'){
        agent any
        steps {
           //Some Steps here
        }
    }

    //Input stage which should only get executed if ${Input} is Yes
    // Or else Directly go to Stage 3

    stage ('Manual Input'){
        agent none
        steps {
            input message: "Please Approve", ok: 'Approve'
        }
    }

stage ('Stage_3'){
        agent any
        steps {
           //Some Steps here
        }
    }
}
}

我希望 Jenkins 执行“Stage_1”,然后仅当参数“Input”为“Yes”时才执行 Stage“Manual Input”,否则跳过“Manual Input”阶段并转到“Stage_3”。

由于agent none,我无法在“手动输入”阶段的script{} 块中执行if/else。它会引发错误。

非常感谢您的帮助! TIA

【问题讨论】:

    标签: jenkins continuous-integration devops


    【解决方案1】:

    基本上,您所要做的就是在第二阶段添加一个when 语句,您希望它看起来像这样:

    stage ('Manual Input'){
        agent none
        when{
            expression { params.Input == 'Yes' }
        }
        steps {
            input message: "Please Approve", ok: 'Approve'
        }
    }
    

    您可以在此here 上找到文档。 (您会看到,对于这种特定情况,equals 更合适)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多