Yash
Yash

Reputation: 3114

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 we can read the user input in the Groovy script.

I'm referring to this documentation.

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?

Upvotes: 22

Views: 132103

Answers (4)

Kalyan Raparthi
Kalyan Raparthi

Reputation: 435

You can use

Input step plugin

to pause the jenkins pipeline and ask user input during runtime please read this article click here I have writer clear steps to follow.

Upvotes: -1

Aviv
Aviv

Reputation: 14537

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
            }
        }

Upvotes: 12

macg33zr
macg33zr

Reputation: 1805

For saving to variables and a file, try something like this based on what you had:

pipeline {

    agent any

    stages {

        stage("Interactive_Input") {
            steps {
                script {

                    // Variables for input
                    def inputConfig
                    def inputTest

                    // Get the input
                    def userInput = input(
                            id: 'userInput', message: 'Enter path of test reports:?',
                            parameters: [

                                    string(defaultValue: 'None',
                                            description: 'Path of config file',
                                            name: 'Config'),
                                    string(defaultValue: 'None',
                                            description: 'Test Info file',
                                            name: 'Test'),
                            ])

                    // Save to variables. Default to empty string if not found.
                    inputConfig = userInput.Config?:''
                    inputTest = userInput.Test?:''

                    // Echo to console
                    echo("IQA Sheet Path: ${inputConfig}")
                    echo("Test Info file path: ${inputTest}")

                    // Write to file
                    writeFile file: "inputData.txt", text: "Config=${inputConfig}\r\nTest=${inputTest}"

                    // Archive the file (or whatever you want to do with it)
                    archiveArtifacts 'inputData.txt'
                }
            }
        }
    }
}

Upvotes: 19

dot
dot

Reputation: 627

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

Upvotes: 15

Related Questions