首页 > 解决方案 > how to call a groovy function in an active choice parameter in Jenkins pipeline

问题描述

I have a requirement where user has to select multiple resource names from the input block. I tried active choice parameter inside the user input step, and its working when I hardcode the values but the output is empty when I call a groovy function to dynamically generate the value. This function will return a list of resources names based on the environment passed earlier at the start of the job. So hardcoding the values won't be ideal for my situation. Any ideas on how to call a groovy function into the active choice parameter block?

pipeline {

  agent any

  stages{

    stage('test user input') {

        steps {

          timestamps {

            script {

 def userInput = input(

                parameters: [[$class: 'ChoiceParameter', choiceType: 'PT_CHECKBOX', description: 'Please select the values', filterLength: 1, filterable: false, name: 'testvalues', randomName: 'choice-parameter-37737065277176157', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: '''return[

\'error\'

]'''], script: [classpath: [], sandbox: false, script: '''def getvalues(){

return[

\'values1\',

\'values2\',

\'values3\',

\'values4\'

]

}

def value=getvalues()

return value''']]]]

                )

                println("input: " + userInput)

            }

          }

        }

    }

  }

}

标签: groovyjenkins-pipeline

解决方案


推荐阅读