如果用户未输入Active Choice参数化管道构建的值,则退出管道

时间:2019-07-15 02:27:05

标签: if-statement jenkins null jenkins-pipeline jenkins-plugins

如果用户未为单项/多项选择/字符串管道参数的Active Choice参数选择任何值,我希望中止管道。

例如,我具有Groovy脚本,其类型为“ Multi Select”的Active Choices响应参数名为“ IPAddress”:

if (Location.equals("MyTown")) {
return["DDL1", "DDL2", "DDL3", "DDL4"]
} else if (Location.equals("Your Town")) {
return["DDP1", "DDP2"]
} else {
return ["Select an IP from the drop-down"]
}

因此,一旦运行管道,我就会看到“从下拉列表中选择IP”作为IPAddress。

现在,如果用户未从下拉菜单中选择任何内容,则管道将失败并中止。

在管道脚本中,我编写了以下条件检查,尽管用户忽略了选择任何IPAddress的操作,但无法检查条件。

def ex(param){
    currentBuild.result = 'ABORTED'
    error('BAD PARAM: ' + param)
}

pipeline {          

    agent any       

        stages {

            stage ("Pre-Check Parameters") {        

                steps {

                echo "Pre-Check called in pipeline"

                 script {
                    if ("${params.IPAddress}" == null) {ex("IPAddress")}
                    //if ("${params.myEnv}" == null) {ex("b")}
                    //if ("${params.myLoc}" == null) {ex("c")}
                    }            
               }              
             } 
          }
      }

请问这里可能是什么问题?

2 个答案:

答案 0 :(得分:0)

您对使用input步骤有什么限制?

def days=''
pipeline{
agent any;
stages {
    stage('master'){
       steps{ 
           script { 
               try {
                    timeout(time:10, unit:'SECONDS') {
                        days = input message: 'Please enter the time window in number of days', ok: 'Fetch Statistics', parameters: [string(defaultValue: '90', description: 'Number of days', name: 'days', trim: true)] 
                    }
                }
                catch (err){
                   error("No custom value has been entered for number of days.")
                }
           }
       }
    }
  }
}

答案 1 :(得分:0)

要确定您的字符串是否为空,可以使用方法.trim()。它将删除字符串中的前导和尾随空格。这两个魔术词是“ Groovy Truth”。在Groovy中,空字符串为false。这使得更容易评估条件表达式。在您的情况下,意味着如果您将.trim()与if条件一起使用,则该字符串的Groovy Truth值将用于评估。

如果将其更改为以下内容,则您的管道应该可以工作。它将检查您的变量是否为null或为空:

script {
    if (!params.IPAddress?.trim()) {
        ex("IPAddress")
    }
}