参数能否用作方法体,使其在Jenkins管道中可重用?

参数能否用作方法体,使其在Jenkins管道中可重用?,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我有一个文件,它在触发构建之前询问参数。但我希望用户检查并确认传递的参数。若参数传递正确,则进一步移动,否则要求再次选择参数 //method getParameter() will be used to prompt parameter from the user def getParameter () { parameters { string(defaultValue: "API", description: 'What application?', name:

我有一个文件,它在触发构建之前询问参数。但我希望用户检查并确认传递的参数。若参数传递正确,则进一步移动,否则要求再次选择参数

//method getParameter() will be used to prompt parameter from the user

def getParameter ()  {
    parameters {
        string(defaultValue: "API", description: 'What application?', name: 'Application')
        choice(choices: ['DEV', 'UAT'], description: 'choose the environment', name: 'Environment')
    }
}



pipeline {
    agent any
    getParameter () // This line will call the parameter method first time 
    stages { 
        stage("primary"){
            steps {
                script {
                    sh '''#!/bin/bash +x
                    # here user will check and confirm the passed parameter are passed correctly
                    echo Application: $Application 
                    echo Environment: $Environment
                    '''
                    try {
                        input message: 'Please check the parameters along with their values', ok: 'Confirm'
                        } 
                    catch(err) {
                        getParameter () // This line will call if user do not passed the parameter correctly
                    }
                }
            }    
        }
仅允许在管道块内使用一次

src:

所以你不能在一个阶段定义它。我认为您正在尝试复制输入指令行为

我想你会喜欢这样的东西:

def getParameter()  {
    timeout(time: 120, unit: 'SECONDS') {
        script {
            // Show the select input modal
            def INPUT_PARAMS = input message: 'Please provide parameters', ok: 'Next',
                parameters: [choice(
                    name: 'ENVIRONMENT', choices: ['dev','qa'].join('\n'), 
                    description: 'Please select the Environment'
                )]
            env.ENVIRONMENT = INPUT_PARAMS.ENVIRONMENT
        }
    }
}