问题
Can the parameters in a Jenkins declarative pipeline be dynamic?
I want a the choice option values be populated at runtime by a function. The following code does generate a list of options, but they seem to be stale - probably generated on the first time I ran this code. If the list of AMIs changes, the choices remain the same. I want this to run every time I select build with parameters
.
def findAMIs() {
// Find relevant AMIs based on their name
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \
' --owners OWNER --filter Name=name,Values=PATTERN \
' --query Images[*].{AMI:Name} --output text'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(10000)
return sout.tokenize()
}
def AMIs = findAMIs().join('\n')
pipeline {
// a declarative pipeline
agent any
parameters {
choice(name: 'Release',
choices: AMIs)
}
...
}
EDIT
I ended up using jenkins-job-builder
, with extended choice parameters. It does not support the groovyScript
parameter at the moment, so I modified it https://review.openstack.org/#q,I0c6ac0b49c24b8d3afbc06b003847de2e043c2b8,n,z
EDIT The above link went dead, so here is another link to openstack: https://review.opendev.org/#/c/477003/ But the gist of the matter is I have added a new parameter to jenkins-job-builder called 'groovyScriptFile', which was merged.
回答1:
what about user input:
def findAMIs() {
return UUID.randomUUID().toString().split('-').join('\n')
}
node{
def userInput = input(
id: 'userInput', message: 'input parameters', parameters: [
[
$class: 'ChoiceParameterDefinition',
name: 'ami',
choices: findAMIs(),
description: 'AMI',
],
]
)
echo ("Selected AMI :: "+userInput)
}
回答2:
There is another solution: you can use the "properties" step before "pipeline" - there you can use the active choice plugin too:
properties([
parameters([
[
$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterable: false,
name: 'Release',
randomName: 'choice-parameter-21337077649621572',
script: [
$class: 'GroovyScript',
fallbackScript: '',
script: '''// Find relevant AMIs based on their name
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \
' --owners OWNER --filter Name=name,Values=PATTERN \
' --query Images[*].{AMI:Name} --output text'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(10000)
return sout.tokenize()'''
]
]
])
])
pipeline {
...
}
The only thing is that first time you start your build, it would fail. Second time you start it it should be a "build with parameter".
Hope it helps.
回答3:
For anyone needing a declarative pipeline syntax option, I found a good solution in another question, which helped me.
This is my suggestion based on it. You should be able to generate a more dynamic list with the code that creates the ${WORKSPACE}/list
file
pipeline {
agent any
stages {
stage("Release scope") {
steps {
script {
// Prepare a list and write to file
sh "echo \"patch\nminor\nmajor\" > ${WORKSPACE}/list"
// Load the list into a variable
env.LIST = readFile (file: "${WORKSPACE}/list")
// Show the select input
env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
parameters: [choice(name: 'RELEASE_SCOPE', choices: env.LIST, description: 'What is the release scope?')]
}
echo "Release scope selected: ${env.RELEASE_SCOPE}"
}
}
}
}
I hope this helps
来源:https://stackoverflow.com/questions/44570163/jenkins-dynamic-declarative-pipeline-parameters