Jenkins Declarative Pipeline: How to read choice from input step?

烈酒焚心 提交于 2019-12-02 16:21:22

Since you are using declarative pipelines we will need to do some tricks. Normally you save the return value from the input stage, like this

def returnValue = input message: 'Need some input', parameters: [string(defaultValue: '', description: '', name: 'Give me a value')]

However this is not allowed directly in declarative pipeline steps. Instead, what you need to do is wrap the input step in a script step and then propagate the value into approprierte place (env seems to work good, beware that the variable is exposed to the rest of the pipeline though).

pipeline {
    agent any
    stages {
        stage("foo") {
            steps {
                script {
                    env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                            parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
                }
                echo "${env.RELEASE_SCOPE}"
            }
        }
    }
}

Note that if you have multiple parameters in the input step, then input will return a map and you need to use map references to get the entry that you want. From the snippet generator in Jenkins:

If just one parameter is listed, its value will become the value of the input step. If multiple parameters are listed, the return value will be a map keyed by the parameter names. If parameters are not requested, the step returns nothing if approved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!