在Jenkins流水线中将交互式输入读取到一个变量中

20

在 Jenkins 流水线中,我想为用户提供一个运行时交互输入的选项。我想了解如何在 Groovy 脚本中读取用户输入。

我参考了这份文档

经过一些尝试,我成功地做到了这一点:

 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'])
                              
                }
            }
        }
    }
}
在这个例子中,我能够回显(打印)用户输入的参数:

在这个例子中,我可以输出(打印)用户输入的参数:

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

但我无法将这些参数写入文件或将它们赋值给变量。我们该怎么实现呢?


请展示您当前的代码。拥有至少一个基本版本并不难,对吧? - StephenKing
4个回答

19

如果要保存到变量和文件中,可以尝试基于您的代码进行以下操作:

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

14

这是使用input()的最简单示例。

  • 在阶段视图中,当你悬停在第一个阶段上时,你会注意到问题“是否要继续?”。
  • 当作业运行时,你会在控制台输出中注意到类似的提示。

除非你点击继续或中止,否则工作将在暂停状态下等待用户输入。

pipeline {
    agent any

    stages {
        stage('Input') {
            steps {
                input('Do you want to proceed?')
            }
        }

        stage('If Proceed is clicked') {
            steps {
                print('hello')
            }
        }
    }
}

有更高级的用法来显示参数列表并允许用户选择一个参数。根据选择,您可以编写Groovy逻辑来继续并部署到QA或生产环境。

以下脚本呈现一个下拉列表,用户可以从中选择。

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

}

您也可以使用 StringParameterDefinitionTextParameterDefinitionBooleanParameterDefinition,以及链接中提到的许多其他参数定义。


感谢 @dot 的详细解释。请参考我在原问题中的 EDIT-1。我想将用户输入分配给一个变量或写入文件。我该如何实现? - Yash
当您将用户输入分配给变量时会发生什么? def variable = userInput['config'] - dot
在StringParameterDefinition的情况下,我们如何使用“choices:”字段? - MeowRude

12
解决方案: 为了在jenkins流水线中将用户输入设置、获取和访问为变量,您应该使用ChoiceParameterDefinition,以下是一个快速有效的代码片段:
    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
            }
        }

-1

您可以使用

输入步骤插件

来暂停Jenkins流水线并在运行时请求用户输入,请阅读本文点击此处,我已经写清楚了要遵循的步骤。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接