将 Jenkins 管道中的交互式输入读取到变量 [英] Read interactive input in Jenkins pipeline to a variable

查看:23
本文介绍了将 Jenkins 管道中的交互式输入读取到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Jenkins 管道中,我想为用户提供一个选项,以便在运行时提供交互式输入.我想了解我们如何读取 groovy 脚本中的用户输入.请求帮助我们提供示例代码:

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:

我指的是以下文档:https://jenkins.io/doc/pipeline/steps/pipeline-input-步骤/

编辑-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?

推荐答案

要保存到变量和文件,请根据您拥有的内容尝试以下操作:

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}
Test=${inputTest}"

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

这篇关于将 Jenkins 管道中的交互式输入读取到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆