Read interactive input in Jenkins pipeline to a variable

前端 未结 3 1503
我在风中等你
我在风中等你 2021-02-05 22:02

In a Jenkins pipeline, i want to provide an option to the user to give an interactive input at run time. I want to understand how can we read the user input in the groovy script

3条回答
  •  [愿得一人]
    2021-02-05 22:26

    This is the simplest example for input() usage.

    • In the stage view, when you hover on the First stage, you notice the question 'Do you want to proceed?'.
    • You will notice similar note in Console Output when job is run.

    Until you click either proceed or abort, the job waits for user input in paused state.

    pipeline {
        agent any
    
        stages {
            stage('Input') {
                steps {
                    input('Do you want to proceed?')
                }
            }
    
            stage('If Proceed is clicked') {
                steps {
                    print('hello')
                }
            }
        }
    }
    

    There are more advanced usages to display list of parameters and allow user to select one parameter. Based on the selection, you can write groovy logic to proceed and deploy to QA or production.

    The following script renders a drop down list from which a user can choose

    stage('Wait for user to input text?') {
        steps {
            script {
                 def userInput = input(id: 'userInput', message: 'Merge to?',
                 parameters: [[$class: 'ChoiceParameterDefinition', defaultValue: 'strDef', 
                    description:'describing choices', name:'nameChoice', choices: "QA\nUAT\nProduction\nDevelop\nMaster"]
                 ])
    
                println(userInput); //Use this value to branch to different logic if needed
            }
        }
    
    }
    

    You can also use StringParameterDefinition,TextParameterDefinition or BooleanParameterDefinition and many others as mentioned in your link

提交回复
热议问题