Jenkins Pipeline Example: Create Jenkins Pipeline Tutorial

Jenkins Pipeline Example: Create Jenkins Pipeline Tutorial. Continuous integration solution of Jenkins lets you automate your software development process and is one of the most popular pieces of software in the DevOps toolchain.

Well, Jenkins Pipeline allows you to create your development workflow in code. It helps developers concentrate on building code rather than handling Jenkins configurations.

For example, developers employ pipelines to make their builds predictable and automated instead of going through all of their jobs to confirm they are still operating as intended.

This article shows you how to build a Jenkins Pipeline to automate your workflow through code.

What is Jenkins Pipeline?

All in all, Jenkins is a continuous integration solution that aids in automating the software development process. Continuous integration is a software development practice where developers integrate code often and regularly to shorten the total software delivery time.

Simply put, Jenkins Pipelines, in particular, are a method to customize your Jenkins continuous delivery pipelines. Instead of using a text based setup, you specify your pipeline in code. It enables simple sharing, modification, and reuse of common stages across pipelines.

Moreover, it also allows you to develop automated tests and track their outcomes. The Jenkins pipeline is compatible with any technological stack, including Java, Python, and Ruby.

Types of Jenkins Pipeline Code

There are two types of Jenkins Pipeline syntax or code:

  • The declarative syntax makes it simple to build pipelines. It has a preset framework for building Jenkins pipelines and uses stages and steps to handle all aspects of pipeline execution simply and straightforwardly. It involves specifying what software must be produced, tested, or deployed and how it should be done.
  • The scripted syntax is intended for smaller build definitions and does not support stages or any particular structure. A scripted Jenkins pipeline runs on the Jenkins master with a lightweight executor that converts the pipeline into atomic instructions.

So, this tutorial uses only declarative syntax or code.

Benefits of the Jenkins Pipeline

  • Jenkins pipeline is in code, so different people edit and run it.
  • Pipelines persist. If your server restarts unexpectedly, the pipeline automatically resumes.
  • What is more, pipelines incorporate user input.
  • Then, Jenkins Pipelines truly shine and assist with large projects. It’s possible to utilize pipelines in a loop and perform numerous tasks.
  • Support additional plugins and add-ons.

Jenkins Pipeline Example: Create Jenkins Pipeline Tutorial 

Install the Build Pipeline Plugin in Jenkins

First of all, we need the Build Pipeline plugin for basic pipeline job creation and management.

To install this plugin, login to Jenkins, go to the Dashboard, and select the Manage Jenkins option from the left side panel.

Select the Plugins option under the System Configuration section.

If you have the build Pipeline plugin already installed on your Jenkins, you see it under the Installed plugins. Otherwise, navigate to Available Plugins and search for it through the search bar at the top.

Once you find it in the results, check the box next to it. Next, install it directly through the Install without restart or install it in steps with the Download Now and install after restart option. These options are displayed at the bottom; click on any option and follow the process to install the plugin.

Create Jenkins Pipeline

Once you have installed the Build Pipeline plugin, you are now ready to create Pipelines.

Next, Go to the Dashboard and click on the New Item in the left pane.

In the next window, enter the pipeline name and select the Pipeline option from the multiple options given in the window.

After that, Jenkins prompts for the pipeline configuration. Here, go to the Pipeline section in the Configure window.  Following, select Pipeline script or Pipeline script from the SCM option in the Definition.

All in all, both options use declarative syntax; the only difference is that in the Pipeline script option, you have to manually type your script in the Script section. Whereas in the Pipeline script from SCM option, you provide the link and details of an already written script on any supply chain management solution. It automatically fetches your script into the pipelines. 

Although the scripted pipeline syntax is not used in this tutorial, use and explore the scripted syntax by selecting the Pipeline Syntax option under the Pipeline section.

Click on Apply and then the Save option.

Build Jenkins Pipeline Jobs

Now that you have declared the script, you need to build the pipeline jobs to see if the code is correct and is working properly.

Either build a pipeline job directly from the Jenkins dashboard by clicking on the play icon at the end of the pipeline job.

Or select the job and then click on the Build Now option in the left pane.

Whether the build is successful or unsuccessful, the Full Stage View or Console Output provides unparalleled insight into the build.

The Full Stage View provides a view of the overall build and its stages. Go ahead and open it by selecting the Status or Full Stage View options from the left pane under the pipeline.

Here, the Console Output is another story altogether. Basically, it displays details about the start of the job, execution details about each line of the pipeline code, and errors, if any. In other words, it is also a tool for troubleshooting your code.

Besides, you need to open the pipeline from the dashboard to go to the console output. See all the builds listed in the left pane. After, select any particular build and click on the Console Output option.

Now, see the console appear in the right pane, and  also see it in text format using the View as plain text option.

If you want to explore an individual stage, select the Stages option in the left pane under the pipeline job. Select the particular stage you want to see. See the action performed during this stage in the right pane.

Configure Upstream and Downstream Jobs through Triggers

In Jenkins, putting all your code into one pipeline job is impractical, especially for big projects. But it is necessary to maintain the execution pattern automatically. For example, some parts need to be executed first.

This issue is solved by setting any job downstream or upstream through the triggers section in the pipeline configuration.

To set this up, navigate to the Pipeline job you need to set as downstream and click on Configure in the left pane.

Next, go to the Build Trigger section in the right pane. In here, check the Build after other projects are built option. And under the Project to Watch section, write the upstream job or jobs separated by a comma. Now, click on Apply and Save.

When you run the upstream job, you see that the upstream job completes itself and then automatically invokes the downstream job. To determine whether this job has started on its own or if another job is starting it, view the console output of the jobs.

That’s all for Jenkins Pipeline Example: Create Jenkins Pipeline Tutorial. Let’s conclude.

Jenkins Pipeline Example: Create Jenkins Pipeline Tutorial Conclusion 

Summing up, Jenkins Pipeline is a platform for specifying your build process in code. Importantly, it simplifies the creation and management of continuous delivery pipelines, which are vital components of DevOps tools.

This article goes over the process of building and configuring the Jenkins pipeline and how to automate the execution of multiple Jenkins jobs through it.

Explore our Jenkins section to learn more.

Avatar for Sobia Arshad
Sobia Arshad

Information Security professional with 4+ years of experience. I am interested in learning about new technologies and loves working with all kinds of infrastructures.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x