Read interactive input in Jenkins pipeline to a variable

Yash picture Yash · Nov 2, 2017 · Viewed 67k times · Source

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?

Answer

macg33zr picture macg33zr · Nov 17, 2017

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