I have the following (simplified) Jenkins pipeline code.
jobParams.groovy
List get(Object paramVars = {}) {
def params = []
params += [
choice(
choices: ['branch', 'tag'],
name: 'RELEASE_TYPE'
),
string(
defaultValue: '',
name: 'VERSION'
),
]
return params
}
pipeline.groovy
def call() {
properties([
parameters(
jobParams.get()
)
])
pipeline {
agent { label 'deploy-slave' }
stages {
stage('Prepare') {
steps {
script {
// Do some logic here and set a job parameter?
}
}
}
}
}
}
This works fine. When the pipeline starts the job parameters are set and available for the next time the job runs.
However, is it also possible to set job parameters dynamically after some logic in a pipeline step?
It turned out to be pretty easy!
I created a jobProperties.groovy file in my shared pipeline library, which composes the parameter list and calls the properties() function.
def call() {
params = [
string(
defaultValue: '',
description: 'Version to deploy',
name: 'VERSION'
),
]
if (env.HANDLER == 'ansible') {
params += [
string(
defaultValue: '',
description: 'DEPLOY_ARGS | Ad hoc "ansible-playbook" args. Example to limit hosts to' +
' deploy to "-l somehost"',
name: 'DEPLOY_ARGS'
),
]
} else if (env.HANDLER == 'capistrano') {
params += [
string(
defaultValue: '',
description: 'DEPLOY_ARGS | Ad hoc "cap" args. Example to limit hosts to' +
' deploy to "-z somehost"',
name: 'DEPLOY_ARGS'
),
]
}
properties([
parameters(
params
)
])
}
pipeline.groovy
def call() {
pipeline {
agent { label 'deploy-slave' }
stages {
stage('Prepare') {
steps {
script {
jobProperties()
}
}
}
}
}
}
I think that if you don't have a shared pipeline library, the code of jobParams.groovy can be also put directly in the script {} wrapper of the pipeline.