Hi,
I’m using the Active Choice plugin with two single select “Active Choices Parameter”. The first one shows the list of images (form Harbor) and the second one shows the versions of every image.
The problem is that, when I get an image from the first list, the second one doesn’t update.
I can show only the versions of the firt image of the first list. If I select another image from the first list and click on “build”, when I’ll came back to the form I can show the list of versions.
But I need to show them before to build, in order to choose the version to deploy.
This is my pipeline script, where I create two functions listImages() and def listVersionsFromImage(imageName).
Thank you in advance for any suggestions!
properties([
parameters([
// First choice parameter (Images)
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select a docker image',
filterLength: 1,
filterable: true,
name: 'IMAGES',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["ERROR IMAGES"]'
],
script: [
classpath: [],
sandbox: true,
script: listImages()
]
]
],
// Second choice parameter (Version)
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'select version ',
name: 'VERSION',
referencedParameters: 'IMAGES',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["ERROR VERSION"]'
],
script: [
classpath: [],
sandbox: true,
script: listVersionsFromImage(IMAGES)
]
]
]
])
])
pipeline {
agent { label 'master' }
stages {
stage('Printing selected choice') {
steps {
script {
println IMAGES
println VERSION
}
}
}
}
}
def listImages() {
node("master") {
configFileProvider([configFile(fileId: "global.conf", variable: 'globalConfigFile')]) {
globalConf = readJSON returnPojo: true, file: globalConfigFile
}
def harborTokenId = globalConf.registry.token
withCredentials([string(credentialsId: harborTokenId, variable: 'harborToken')]) {
output = sh(returnStdout: true, script: "curl -X GET 'https://${globalConf.registry.url}/api/v2.0/projects/master/repositories?page=1&page_size=100' -H 'accept: application/json' -H 'authorization: Basic ${harborToken}' | jq '.[].name' | cut -d'/' -f2 | tr -d '\"' | sort ")
def ouputAsArray = output.split('\n').findAll { it.trim() }
def outputArrayWithQuotes = ouputAsArray.collect { "'${it}'" }
return "${outputArrayWithQuotes}"
}
}
}
def listVersionsFromImage(imageName) {
node ('master') {
configFileProvider([configFile(fileId: "global.conf", variable: 'globalConfigFile')]) {
globalConf = readJSON returnPojo: true, file: globalConfigFile
}
def harborTokenId = globalConf.registry.token
echo "imageName: ${imageName}"
withCredentials([string(credentialsId: harborTokenId, variable: 'harborToken')]) {
output = sh(returnStdout: true, script: "curl -X GET 'https://${globalConf.registry.url}/api/v2.0/projects/master/repositories/${imageName}/artifacts?page=1&page_size=10' -H 'accept: application/json' -H 'authorization: Basic ${harborToken}' | jq -r '.[].tags[].name' | sort -r ")
echo "Versions: ${output}"
def ouputAsArray = output.split('\n').findAll { it.trim() }
echo "Versions one: $ouputAsArray"
def outputArrayWithQuotes = ouputAsArray.collect { "'${it}'" }
echo "Versions dos: $outputArrayWithQuotes"
return "${outputArrayWithQuotes}"
}
}
}