Hello I have the following code as my 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'
}
}
} }
So Here I have obtained the values from a file (FILENAME) and split them into a list (FILEDATA). It works perfectly and it prints out and echo too. Actually it returns a set of Mac addresses from a macAdds.properties such as [ff:ff:ee:ee:ee:ff, ff:ee:dd:aa:bb:ee, ff:ee:aa:cc:bb:dd].
But in my active choice reference parameter (choice1) when I returns the FILEDATA value in my switch case as single select it does not work out where it will run fall back script and gives me an error. I have tested returning some random values given manually to the switch statements where it works perfectly. I want to know why I can't return the values in my active choice reference parameter (choice1) script. Help me out!! Thank you
Well I have figured out the answer for this solution. When you are using groovy sandbox value as through you can do many thing without limitations. But you have to approve the script manually from Manage Jenkins => Script Approval. Hope this will help out someone. Thanks!