Build Pipeline in Jenkins

I talked briefly about Jenkins Pipeline in the tutorial Install Pipeline plugin in Jenkins, in this tutorial, I will guide you through the basic steps to use Pipeline, to build any project in the Jenkins.

For example, I will use the example project in the tutorial InternalResourceViewResolver in Spring Boot. This project has been pushed to huongdanjava.com repository using my GitHub account at https://github.com/huongdanjavacom/huongdanjava.com.

To build this project using Pipeline, after installing Pipeline plugin, you please select the New Item menu in the Jenkins homepage to create a new Jenkins job:

Build Pipeline in Jenkins

After entering the Jenkins job name, please select Pipeline:

Build Pipeline in Jenkins

then press the OK button.

This Jenkins configuration page will display. Scroll down to the Pipeline section:

Build Pipeline in Jenkins

to start installing the Pipeline for this Jenkins job.

In this section, as you can see, by default we have Definition selection with the option of the Pipeline script and below is the Script box. This means to use the Pipeline in Jenkins, we need to write scripts. Those are the codes that define the operations we need to do to build, run the test and deploy the application to your production.

We can use the methods and functions available of the Jenkins plugins that support Pipeline with Groovy programming language to implement the Pipeline (this is called Scripted Pipeline, this is the old way when Jenkins introduced the Pipeline concept) or write explicit statements to do this (this is also called Declarative, was introduced after Scripted Pipeline that help us build a Pipeline simpler.)

No matter the way you are using, you must follow the Pipeline syntax with code blocks to implement your Pipeline!

The block in the Pipeline can be listed as follows:

– The “pipeline” code block defines the entire build process, used in Declarative Pipeline

– The “agent” code block used in Declarative Pipeline to allocate an executor in the node running Jenkins to run Pipeline.

– The “node” code block also defines the entire build process but used with Scripted Pipeline.

– The “stages” code block contains many” stage” code block helps us to sort tasks into sections, each stage separately, for example, we can have stages for build, test, deploy.

– The ” steps” code block is used to define tasks in each stage.

A “pipeline” code block or “node” code block will include one or more “stage” code block marking each stage of the Jenkins build process. And in a “stage” code block, we will have many “steps” code blocks that define the tasks we need to do in any stage.

Below is an example of a Pipeline script with a Declarative Pipeline:

and Pipeline script with Scripted Pipeline:

If using Declarative Pipeline for my example to clone the code and build, my Pipeline script can be defined with the following content:

As you can see, we just need to use the sh statement to execute the commands of Git and Maven tools to fulfill our needs. Simple, intuitive, isn’t it? … This is just an example, the actual problem will be different 🙂

The result when I ran the Jenkins job was as follows:

Build Pipeline in Jenkins

As you can see, each stage will be executed in the order defined in the Pipeline script. Don’t worry about the build failing, because of the fail Unit Test!

I can rewrite the Pipeline script using Scripted Pipeline as follows:

With the above Pipeline script, you will need to install the Git plugin to run! This Pipeline script is using the existing function of the Git plugin.

Pipeline script can be defined in a configuration file named Jenkinsfile instead of defining in the configuration of Jenkins job.

Back to the configuration Jenkins job page in my example, Pipeline section, if you choose the “Pipeline script from SCM” in Definition selection, you will see the following result:

Build Pipeline in Jenkins

Here, it allows us to select the SCM (Source Control Management likes Git, SVN) where we manage the application’s code, for example, using Git, the configuration for this SCM will be as follows:

Build Pipeline in Jenkins

To use this option, as I said, your source code must have a Jenkinsfile file!

Add Comment