Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read interactive input in Jenkins pipeline to a variable

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. Request to help we with a sample code:

I'm referring to following documentation: https://jenkins.io/doc/pipeline/steps/pipeline-input-step/

EDIT-1:

After some trials i've got this working:

 pipeline {
    agent any

    stages {

        stage("Interactive_Input") {
            steps {
                script {
                def userInput = input(
                 id: 'userInput', message: 'Enter path of test reports:?', 
                 parameters: [
                 [$class: 'TextParameterDefinition', defaultValue: 'None', description: 'Path of config file', name: 'Config'],
                 [$class: 'TextParameterDefinition', defaultValue: 'None', description: 'Test Info file', name: 'Test']
                ])
                echo ("IQA Sheet Path: "+userInput['Config'])
                echo ("Test Info file path: "+userInput['Test'])

                }
            }
        }
    }
}

In this example i'm able to echo (print) the user input parameters:

echo ("IQA Sheet Path: "+userInput['Config'])
echo ("Test Info file path: "+userInput['Test'])

but I'm not able to write these parameters to a file or assign them to a variable. How can we achieve this?

like image 657
Yash Avatar asked Nov 02 '17 16:11

Yash


People also ask

How do I add active choice parameters in Jenkins?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.

What is the purpose of using the input step in Jenkins?

In Jenkins declarative pipelines it is possible to prompt a user for an interactive input by creating the input step. For example, at some stage of the Jenkins pipeline you may want to ask a user to provide the credentials. The user input can be saved as an environment variable and used in the next steps.

What type of script is accepted as input on the Jenkins script console?

Jenkins features a Groovy script console which allows one to run arbitrary Groovy scripts within the Jenkins controller runtime or in the runtime on agents.


2 Answers

Solution: In order to set ,get and access user input as a variable on jenkins pipeline, you should use ChoiceParameterDefinition , attaching a quick working snippet:

    script {
            // Define Variable
             def USER_INPUT = input(
                    message: 'User input required - Some Yes or No question?',
                    parameters: [
                            [$class: 'ChoiceParameterDefinition',
                             choices: ['no','yes'].join('\n'),
                             name: 'input',
                             description: 'Menu - select box option']
                    ])

            echo "The answer is: ${USER_INPUT}"

            if( "${USER_INPUT}" == "yes"){
                //do something
            } else {
                //do something else
            }
        }
like image 171
avivamg Avatar answered Nov 14 '22 03:11

avivamg


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

like image 34
dot Avatar answered Nov 14 '22 05:11

dot