Build Jenkins Job With default Parameters using JenkinsFile
Tags: jenkinsfile, jenkins, devops
Published: 2023-01-05 05:26:34

Let's say you want to create different Jenkins job pipeline with different parameters, for example, you want to build different flavors of the mobile app or you need to build a jar file with different parameters or you need to run tests with different parameters against different environment.

How would you do that for the above, or for similar problem statement that you encounter, while working on a project?
Do you create different jenkins jobs and then configure the jobs with different parameters manually for all of them with different values?

Like as below for a sample JenkinsFile which prints git repo branch name from jenkins job with manually configured parameters as,

JenkinsFile →

```
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                echo "$SERVICE_BRANCH"
            }
        }
    }
}
```
Parameters configured manually by a user →


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21dn2bi8x7zeti61to1o.jpeg)

**Execution Output →**

```
[Pipeline] Start of Pipeline
[Pipeline] node
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
release
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
```

Did you figured out anywhere if there is some better way to do that effectively?

**JenkinsFile using Parameters with default Values and Description**

With JenkinsFile using parameters, you don't really need to configure different jobs with different type of parameters with their default values and their descriptions.

All you really need to do is to configure different type of parameters like string, password, boolean, choice, etc with JenkinsFile along with description and default values to let all your Jenkins Job being pre configured with those default values and descriptions as provided from parameters block in JenkinsFile, like as below,


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjhupxm4mb720i0cesiv.jpeg)


**JenkinsFile using Parameters with default values and description →**

```
pipeline {
    agent any
        parameters {
            string  defaultValue: 'master', description: 'GIT Branch', name: 'SERVICE_BRANCH'
        }
    stages {
        stage('Test') {
            steps {
                echo "$SERVICE_BRANCH"
            }
        }
    }
}
```

**Execution Output →**

```
[Pipeline] Start of Pipeline
[Pipeline] node
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
master
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
```

Now the problem with above JenkinsFile is that, if you will configure the Jenkins Job with different values for parameters, then, they will get overridden with default values as provided in JenkinsFile in the next execution.

How to solve this another problem statement to avoid overridden of default parameter values from JenkinsFile, if different Jenkins Job parameters is configured with different values?

**JenkinsFile using Parameters with default Values using Elvis Operator**

To solve the problem statement as discussed above, we can use Elvis Operator in default values of a parameter to not to get it overridden with default values as mentioned in JenkinsFile, if the Jenkins Job is already configured with any user provided values.

Let's say if we have configured the Jenkins Job for parameter, SERVICE_BRANCH with value release as seen below,
and the JenkinsFile is as below with Elvis Operator for the parameter,

```
pipeline {
    agent any
        parameters {
            string defaultValue: params.SERVICE_BRANCH ?: 'master', description: 'GIT Branch', name: 'SERVICE_BRANCH'
        }
    stages {
        stage('Test') {
            steps {
                echo "$SERVICE_BRANCH"
            }
        }
    }
}
```

With above changes to a Jenkins Job with multiple executions, the value of SERVICE_BRANCH won't get overridden with master and will always be release as configured by a user to the corresponding Jenkins Job.

**Execution Output →**

```
[Pipeline] Start of Pipeline
[Pipeline] node
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
release
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
```

**NOTE**: While configuring the Jenkins Job for the first time, all parameters will get initialized upon first execution of Jenkins Job only. Upon first execution of Jenkins Job, you will see all the parameters configured with default values and description.

Also, in next second execution only, you will get Build with Parameters instead of Build Now.

Voila, this is done, now you don't have to manually configure different jenkins jobs, all you have to do is to create different Jenkins Job and then override the values as referred for each job with pre-configured parameters default values and description after first execution of Jenkins Job.
Now, how can you get the syntax of different types of parameters for your JenkinsFile?

**Create different types of Parameters for JenkinsFile using Declarative Directive Generator**

All you have to do is to go to the CONFIGURE section of your Jenkins Job and the scroll until bottom to Pipeline section and then, you can click to highlighted text, PipeLine Syntax, and then select Declarative Diretive Generator and then select parameters: Parameters from Sample Directive.
From the drop down menu of Add items, select different type of parameters to add them accordingly with Name and Description like as seen here in below image,


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yq2l0nay3q1ae8sjew9.jpeg)

That's it folks, if you do really have some better ways of configuring different jenkins job, please let us know as well in comment section below.