Comprehensive Guide to Jenkins Declarative Pipeline [With Examples]

Jenkins Pipeline is an automation solution that lets you create simple or complex (template) pipelines via the DSL used in each pipeline. Jenkins provides two ways of developing a pipeline — scripted and declarative. Traditionally, Jenkins jobs were created using Jenkins UI called freestyle jobs. In Jenkins 2.0, Jenkins introduced a new way to create jobs using the technique called pipeline as code. In the pipeline as code technique, jobs are created using a script file that contains the steps to be executed by the job. In Jenkins, that scripted file is called a Jenkinsfile. In this Jenkins tutorial, we will take a deep dive into Jenkins Declarative Pipeline with the help of Jenkins Declarative Pipeline examples.

Let’s get started with the basics.

Ace Your DevOps Game With This Ultimate List of Plugins in Jenkins

In recent years, DevOps has established itself as a formidable force in the software world. By bridging the gap between development and IT operations teams, DevOps has brought speed, efficiency, and quality to software development. It enabled IT businesses to adopt agile software delivery methodologies like Continuous Integration and Continuous Delivery (CI/CD).

However, despite gaining universal popularity, the road to successful DevOps adoption remained bumpy. Many organizations struggle to implement optimal automation across the software development lifecycle (SDLC), from build, integration, and testing to delivery and deployment. Consequently, harnessing the full potential of the CI/CD pipeline remained elusive. At this juncture, Jenkins has emerged as an indispensable tool to help DevOps teams achieve automation goals.

Hyper-Automation — New Age Automation With AI

Article Image

Active minds look to obviate monotony. This is the seed of the greatest ideas that have ever transpired. Ideas that lead to progress, growth, and freedom from doing things manually. Over the past several decades, the advancement in technology has given us the greatest gift of all, time. Time to focus on our creative endeavors and leave it to the machines to carry out the tasks that our brains now consider mundane. As a species, it is our constant endeavor to make our lives easier and more convenient.

While the core meaning of “automation” remains the same, the usage of the word has truly changed over time. We have come a long way from manually switching on the ceiling fan to automatic temperature control in air conditioners everywhere. Things that we perceived as “automated” years ago, kept getting more and more automated and became more convenient to use. This is largely due to advancements in technology over the years. 

Running Selenium Tests in Jenkins

DevOps, an organizational approach that has been used immensely for quicker development and deployment of applications, has been the latest technical buzz. Today organizations are adopting DevOps, as it promises to provide best practices starting right from planning and testing to deployment and monitoring. CI/CD tools like Jenkins play an integral role in allowing organizations to implement DevOps, and Jenkins integration with Selenium makes the entire process of Selenium test automation so much easier.

In this article, I will take you through a quick introduction to Jenkins & Selenium, followed by the method to integrate Jenkins with Selenium WebDriver. If you are already familiar with the concepts, you can jump to the section explaining Jenkins integration with Selenium directly.

Jenkins Pipeline Groovy script – Part 2 Add a User to a Gitlab Group

My earlier post on Jenkins Pipeline Groovy script - Part 1 , discusses about how to create Gitlab group using Jenkins Pipeline Groovy script. In this post, I am discussing about how to add list of Users to newly created Gitlab group or existing group with specific permission granted to them. In companies while handling big projects and maintaining enormous code repositories, it is very much important to know what permissions supposed to be given to each member in the team. 

Because giving wrong permission to team member, opens big path for problems if the team member really unaware about how efficiently the permission can be used for good purpose. Hence giving right permissions to the team member is very serious business for quick and reliable delivery. And yes, this also avoids team member intervening unnecessarily to the projects where they do not actually belong.

Jenkins Pipeline Groovy Script – Part 1: Creating Gitlab Group

In this article, I am discussing a simple groovy functionality along with Jenkinsfile pipeline script on how to automatically create a new GitLab group or subgroup under the parent group.  In DevOps Continuous Integration(CI) and Continuous Delivery(CD), managing projects in git level is very much important and useful for version control.  These projects are organized under a specific groups to have a disciplined structure in order to handle the application deployment well via DevOps.

What Is Jenkins Groovy?

Groovy is  an object-oriented programming language like Java and has Domain Specific Language(DSL) features. Jenkins provides pipeline capabilities along with groovy DSL features for Continuous Integration(CI). So how to use Jenkins pipeline groovy script, use load keyword in Jenkinsfile to create constructor for groovy class. Using constructor or class object of that Groovy class, functions can be inherited and values are returned. In order to use Jenkins Pipeling Groovy script, in Jenkins CI server Pipeline Groovy Plugin should be installed as prerequisite.

Jenkins Configure Master and Slave Nodes

Take a look at master and slave nodes in Jenkins.

Jenkins Master and Slave Concept

A Jenkins master comes with the basic installation of Jenkins, and in this configuration, the master handles all the tasks for your build system.

You may also enjoy:  Getting Started With Jenkins: The Ultimate Guide

If you are working on multiple projects you may run multiple jobs on each and every project. Some projects need to run on some particular nodes, and in this process, we need to configure slaves. Jenkins slaves connect to the Jenkins master using the Java Network Launch Protocol.