Build with Parameters in Jenkins

Build with Parameters is a function of Jenkins that allows us to pass some values to the Jenkins job before we build them. For example, you have a Jenkins job to build release for an application. Each build to release, you need to enter the release version of the release file, you can use the Build with Parameters function of Jenkins to do this. How is it in details? Let’s find out in this tutorial.

As an example of this tutorial, I will be using the Maven project spark-hello-world in the tutorial Hello World with Spark framework. This project was pushed to the huongdanjava.com repository using my GitHub account at https://github.com/huongdanjavacom/huongdanjava.com.

The current version of this project is 0.0.1-SNAPSHOT. To be able to change the release version of this project to Jenkins and not affect the development process, I will change a bit. I will add the <build> tag along with the <finalName> tag to the project’s Maven configuration file to define the file name after the build:

with value of version variable is assigned to the Jenkins job.

Then in the Maven statement to build this project of Jenkins, I would declare the following:

Declaration -Dversion=${Version} is the way we use the environment variable in Jenkins job. Notice here that the ${Version} variable is a variable that is created and assigned a value using Jenkins’ Build With Parameters function.

Another note is that because the code for this project is currently using the Java 8 Lambda Expression syntax, then we need to add the maven-compiler-plugin plugin:

for Maven to be able to build because the default Maven plugin in Jenkins only builds with Java 1.5 syntax.

OK, now, we will setup Jenkins job to build this project.



The steps are the same as I stated in this tutorial but to use the Jenkins Build with Parameters function then go to the General section of the Jenkins job, and you will see a checkbox “This project is parameterized” :

Build with Parameters in Jenkins

Click to select it, you will see Jenkins display a section that allows us to Add Parameter as follows:

Build with Parameters in Jenkins

Here, as you can see, we have many types of Parameters that we can use:

  • Boolean Parameter
  • Choice Parameter.
  • Credentials Parameter.
  • File Parameter
  • Multi-line String Parameter.
  • Password Parameter
  • Run Parameter
  • String Parameter

Depending on your needs, you can learn more about them to use. In this tutorial, my need is to fill in the version number every time I build, so I’ll select the String Parameter.

Build with Parameters in Jenkins

Here, as you can see, we have three fields:

  • Name: used to name the variable
  • Default Value: Used to declare the default value of this variable.
  • Description: is the description of the variable that we want to create.

You can enter any variable name, according to your needs. In this example, I would fill in the following:

Build with Parameters in Jenkins

You can add more Parameter if you want!

Next, we need to modify the configuration for the Maven command to build the project. As I said above, this statement would be:

Build with Parameters in Jenkins

OK, so the configuration for Jenkins job is done. Now let’s save it and run it!

After you click Save, you will be taken to Jenkins job page. At this point, you will see a link “Build with Parameters” as follows:

Build with Parameters in Jenkins

Click on it, it will allow us to enter the value that we have configured, as follows:

Build with Parameters in Jenkins

Here, I filled out the release number 1.0. Click the Build button to build this project.

Result:

Build with Parameters in Jenkins

Add Comment