无法在jenkins中的jenkins活动选择引用参数脚本中以列表形式返回文件值



你好,我有以下代码作为我的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:ee:ee:ee,ff:ee:dd:aa:bb:ee,ff:ee:aa:cc:bb:dd]

但是在我的活动选择引用参数(choice1(中,当我在开关情况下以单选返回FILEDATA值时,它不知道它将在哪里运行回退脚本,并给我一个错误。我已经测试过将一些手动给定的随机值返回到switch语句,在那里它可以完美地工作。我想知道为什么我不能返回活动选择引用参数(choice1(脚本中的值。帮帮我!!谢谢

我已经找到了这个解决方案的答案。当您使用groovy沙箱值as through时,您可以无限制地做许多事情。但您必须从Manage Jenkins=>手动批准脚本;脚本审批。希望这能帮助到别人。谢谢

最新更新