【发布时间】:2021-07-02 05:35:24
【问题描述】:
您好,我有以下代码作为我的 Jenkinsfile。
pipeline {
agent any
parameters{
string(name: 'IP_ADDRESS', defaultValue: '', description: 'Enter server IP address')
string(name: 'USERNAME', defaultValue: '', description: '')
string(name: 'PASSWORD', defaultValue: '', description: '')
}
environment{
VERSION='1.3.0'
}
stages {
stage('Build') {
steps {
echo 'Built'
sh(script:"python sshMac.py ${IP_ADDRESS} ${USERNAME} ${PASSWORD}", returnStatus: true, returnStdout: true)
}
}
stage('Test') {
steps {
script{
env.FILENAME = readFile 'macAdds.properties'
env.FILEDATA = FILENAME.tokenize( "," )
input message: 'Please choose one',
parameters: [
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select the Environemnt from the Dropdown List',
filterLength: 1,
filterable: false,
name: 'ENVIRONMENT',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["ERROR"]'
],
script: [
classpath: [],
sandbox: true,
script: """
return['1', '2', '3', '4']
""".stripIndent()
]
]
],
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select the Environemnt from the Dropdown List',
filterLength: 1,
filterable: false,
name: 'ENVIRONMENT2',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["ERROR"]'
],
script: [
classpath: [],
sandbox: true,
script: """
return['1', '2', '3', '4', '5']
""".stripIndent()
]
]
],
[$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select a choice',
filterLength: 1,
filterable: false,
name: 'choice1',
referencedParameters: 'ENVIRONMENT',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["ERROR"]'
],
script: [
classpath: [],
sandbox: true,
script: """
def tokens = ${FILENAME}.tokenize(',')
switch(ENVIRONMENT) {
case "1":
return ${env.FILEDATA}
case "2":
return ${env.FILEDATA}
case "3":
return ${env.FILEDATA}
case "4":
return ${env.FILEDATA}
}
"""
]
]
],
text(name: 'vertical', defaultValue: "${FILENAME}")
]
}
echo 'Testing'
echo "${FILENAME}"
echo "${FILEDATA}"
}
}
stage('Deploy') {
steps {
echo 'Deploying'
}
}
} }
所以这里我从文件(FILENAME) 中获取值并将它们拆分为列表(FILEDATA)。它工作得很好,它也能打印出来并回显。实际上它从 macAdds.properties 返回一组 Mac 地址,例如 [ff:ff:ee:ee:ee:ff, ff:ee:dd:aa:bb:ee, ff:ee:aa:cc :bb:dd].
但是在我的 活动选择参考参数 (choice1) 中,当我在我的 switch 案例中返回 FILEDATA 值 作为单选时,它无法确定它将在哪里运行返回脚本并给我一个错误。我已经测试了将一些手动给定的随机值返回给它可以完美运行的 switch 语句。我想知道为什么我不能返回我的 活动选择参考参数 (choice1) 脚本中的值。帮帮我!!谢谢
【问题讨论】:
标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-cli