Jenkins: set a parameter defaultValue dynamically

Cal picture Cal · Feb 17, 2020 · Viewed 7.5k times · Source

I'm trying to set up a multibranch pipeline configuration where the "Deploy" boolean checkbox is defaulted to true on non-production branches, and false on the production build.

pipeline {
  parameters{
    booleanParam(defaultValue: true, description: 'Do deploy after build', name: 'DEPLOY')

Is there some method to conditionally set defaultValue=false when $BRANCH_NAME == "production"?

Answer

Cal picture Cal · Feb 18, 2020

I think I might have answered my own question through a bunch of experimentation. This seems crazy simple, but my test between two branches shows the Deploy parameter is properly defaulted on/off depending on the $BRANCH_NAME

def defaultDeploy = true
if ( BRANCH_NAME == "production" )
{
  defaultDeploy = false
}
pipeline {
  parameters{
    booleanParam(defaultValue: defaultDeploy, 
      description: 'Do deploy after build', name: 'DEPLOY')