How not to break Jenkins Pipeline when adding new parameters (declarative pipeline)
Here is a solution which seems almost like a workaround that I have found which works for declarative pipelines.
First, we need to add some code at the start of our file before we enter into the pipeline{}
section. The code we add will run before the pipeline section, and will check if the env/param exists, and if it does will take the value, otherwise we will give it a default and assign that to a variable that we can access during the pipeline. Then we need to change our pipeline code references from the parameter to the variable.
Below is a sample that defines an arbitrary parameter, and also an agent label to be used. As you can see, the variables can be used during stages and also during procedures such as determining the agent.
def MY_VARIABLE = null
if (env.my_parameter) {
MY_VARIABLE = env.my_parameter
} else {
MY_VARIABLE = "my default value"
}
echo "MY VARIABLE IS SET: ${MY_VARIABLE}"
def MY_AGENT = null
if (env.my_agent) {
MY_AGENT = env.my_agent
} else {
MY_AGENT = "agent_label"
}
echo "MY AGENT IS SET: ${MY_AGENT}"
pipeline {
parameters {
string(defaultValue: "my default value", description: 'This is my parameter', name: 'my_parameter') ;
string(defaultValue: "agent_label", description: 'This is my agent label', name: 'my_agent') ;
}
agent {
label "${MY_AGENT}"
}
stages {
stage('my stage') {
steps {
sh 'echo ${MY_VARIABLE}'
}
}
}
}
Related videos on Youtube
Inbar Rose
Updated on September 18, 2022Comments
-
Inbar Rose over 1 year
An Issue I have been struggling with is whenever I add a new parameter to a Jenkins Pipeline, the next job will not correctly get this parameter. This is especially an issue if those parameters are required for certain pipeline procedures like determining agent. This issue is further frustrating because on a job that already exists, when the default parameter is changed the new job will still use the old one; but when creating a new job it will take none.
In order to solve the parameter issue from the first run of a job, and also to solve the parameter for changes to the parameters for jobs that already ran we need a solution. (At the moment, I have a workaround, which I am posting as an answer, but I would like to know if there is a better solution)
-
Kushal Gangan almost 5 yearstry this..
def policy = $param.VAR_NAME ?: 'medium'
-
-
jayhendren almost 6 yearsThis workaround is actually the best solution that exists at the moment. This issue is fairly well-known in the Jenkins community. See: this question on StackOverflow, the upstream bug report
-
tftd about 5 yearsThis indeed seems to be working best at the moment.
-
tftd about 5 yearsJust a note - you can simplify the definition of the variables like this
def MY_VARIABLE = params.getOrDefault("my_parameter", "my default value")
anddef MY_AGENT = params.getOrDefault("my_agent", "agent_label")
. It will essentially do the same thing, but will make the pipe easier to read. So far it seems to be working as expected for me.