Yas*_*ash 3 groovy jenkins jenkins-pipeline jenkins-2
在Jenkins管道中,我想向用户提供一个选项,以便在运行时提供交互式输入。我想了解如何读取groovy脚本中的用户输入。请求帮助我们提供示例代码:
我指的是以下文档:https : //jenkins.io/doc/pipeline/steps/pipeline-input-step/
编辑1:
经过一些试验后,我已经开始工作了:
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'])
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,我能够回显(打印)用户输入参数:
echo ("IQA Sheet Path: "+userInput['Config'])
echo ("Test Info file path: "+userInput['Test'])
Run Code Online (Sandbox Code Playgroud)
但是我无法将这些参数写入文件或将它们分配给变量。我们怎样才能做到这一点?
为了保存到变量和文件中,请根据实际情况尝试以下操作:
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'
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是input()用法的最简单示例。
在单击继续或中止之前,作业将在暂停状态下等待用户输入。
pipeline {
agent any
stages {
stage('Input') {
steps {
input('Do you want to proceed?')
}
}
stage('If Proceed is clicked') {
steps {
print('hello')
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有更多高级用法来显示参数列表并允许用户选择一个参数。根据选择,您可以编写常规逻辑以继续进行并部署到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
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用StringParameterDefinition,TextParameterDefinition或BooleanParameterDefinition链接中提到的其他
解决方案:为了在 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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24646 次 |
| 最近记录: |