Answers: <p>Here is a solution which seems almost like a workaround that I have found which works for declarative pipelines.</p>
  
  <p>First, we need to add some code at the start of our file before we enter into the <code>pipeline{}</code> 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. </p>
  
  <p>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.</p>
  
  <pre><code>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}'
              }
          }
      }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-06-13T15:53:44.973Z
Answers: 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](https://stackoverflow.com/questions/46680573/how-to-make-sure-list-of-parameters-are-updated-before-running-a-jenkins-pipelin), [the upstream bug report](https://issues.jenkins-ci.org/browse/JENKINS-41929)
Answers: jayhendren
Answers: CC BY-SA 4.0
Answers: 2019-04-24T12:08:31.480Z
Answers: This indeed seems to be working best at the moment.
Answers: tftd
Answers: CC BY-SA 4.0
Answers: 2019-04-24T12:26:22.137Z
Answers: 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.
Answers: tftd
Answers: CC BY-SA 4.0
Answers: 2018-06-13T11:41:27.753Z
Answers: 2018-06-13T11:41:27.753Z
Answers: Inbar Rose
Body: <p>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. </p>
  
  <p>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)</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-06-27T07:13:51.320Z
Comments: try this.. ```def policy = $param.VAR_NAME ?: 'medium'```
Comments: Kushal Gangan
Tags: <jenkins><jenkins-pipeline><jenkinsfile>
Title: How not to break Jenkins Pipeline when adding new parameters (declarative pipeline)
UserDisplayName: Inbar Rose
ThreadText: # How not to break Jenkins Pipeline when adding new parameters (declarative pipeline)
  
  (Asked by: username_0 on 2018-06-13)
  
  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)
  
  **Tags:** jenkins, jenkins-pipeline, jenkinsfile
  
  > **Comments:**
  > **username_1**: try this.. ```def policy = $param.VAR_NAME ?: 'medium'```  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-06-13. Score: 7 | Accepted answer
  
  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}'
              }
          }
      }
  }
  
  ```
  
  > **Comments:**
  > **username_2**: 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](https://stackoverflow.com/questions/46680573/how-to-make-sure-list-of-parameters-are-updated-before-running-a-jenkins-pipelin), [the upstream bug report](https://issues.jenkins-ci.org/browse/JENKINS-41929)  
  > **username_3**: This indeed seems to be working best at the moment.  
  > **username_3**: 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.  
  
  ---
  
  