5

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)

  • try this.. def policy = $param.VAR_NAME ?: 'medium' – Kushal Gangan Jun 27 at 7:13
2

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}'
            }
        }
    }
}
  • 1
    This 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 – jayhendren Jun 13 '18 at 15:53
  • This indeed seems to be working best at the moment. – tftd Apr 24 at 12:08
  • 2
    Just a note - you can simplify the definition of the variables like this def MY_VARIABLE = params.getOrDefault("my_parameter", "my default value") and def 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. – tftd Apr 24 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.