Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: set a parameter defaultValue dynamically

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"?

like image 499
Cal Avatar asked Feb 17 '20 20:02

Cal


People also ask

What is default value in Jenkins?

Default value: an optional value that will be used when a user does not specify one A single Jenkins job or pipeline can have multiple parameters. The only restriction is the parameter name must be unique.

How can I have dynamic parameters in Jenkins?

You can have dynamic parameters based on user parameter selection. To use the active choice parameter, you need to have an Active Choices plugin installed in Jenkins. Here is a small use case for an active choice parameter.

What is active choice parameter in Jenkins?

Unlike default parameter types, the Active choice parameter type gives you more control over the parameters using a groovy script. You can have dynamic parameters based on user parameter selection. To use the active choice parameter, you need to have an Active Choices plugin installed in Jenkins.

How to parameterize Jenkins jobs and pipelines?

Any Jenkins job or pipeline can be parameterized. All we have to do is check the box on the General settings tab that says This project is parameterized: Then we click the Add Parameter button. From here, we must specify several pieces of information:


1 Answers

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')
like image 52
Cal Avatar answered Oct 28 '22 13:10

Cal