Skip to main content

Command Palette

Search for a command to run...

Declarative Pipeline in Jenkins

[Day25]

Updated
5 min read
Declarative Pipeline in Jenkins
Y

Hi there! My name is Yashswini and I'm a Linux Engineer with 3 years of experience in automation, continuous integration, and deployment. My expertise lies in DevOps and Cloud Computing with a focus on AWS. I have hands-on experience in various tools and technologies related to infrastructure automation, managed services, containerization, and monitoring and logging. I have a passion for troubleshooting and resolving issues and I'm always eager to learn and explore new technologies

1. What is Jenkins Pipeline?

Jenkins Pipeline is a combination of plugins that supports the integration and implementation of continuous delivery pipelines. It has an extensible automation server to create simple and complex delivery pipelines as code via pipeline

A Pipeline is a group of events interlinked with each other in a sequence.

Jenkins Pipeline offers powerful automation for software delivery pipelines, providing benefits such as:

  • Automation: Effortlessly automate the entire software delivery process, from build to deployment, reducing errors and ensuring consistent results.

  • Flexibility: Customize pipelines to fit specific project requirements, workflows, and technology stacks.

  • Reusability: Define pipelines as code for easy sharing and reuse across projects and teams, promoting collaboration and consistency.

  • Visibility: Gain clear visibility into each stage and step of the delivery process, enhancing accountability and troubleshooting capabilities.

  • CI/CD Support: Facilitate continuous integration and delivery practices with frequent code integration, automated testing, and seamless deployment.

2. What are Continuous Delivery Pipelines? How does it work?

Continuous Delivery with Jenkins Pipeline

The picture above represents a continuous delivery pipeline in Jenkins. It contains a group of states called to build, deploy, test, and release. These events are interlinked with each other. Every state has its events, which work in a sequence called a continuous delivery pipeline.

every change made in your software goes through a number of complex processes on its way to being released. It also involves developing the software in a reliable and repeatable manner, and progression of the built software through multiple stages of testing and deployment.

3. What is a JenkinsFile?

Jenkins Pipeline is defined using a text file called the Jenkinsfile. It is implemented as code using the Groovy Domain-specific language, which can be edited either through an editor or the configuration page on the Jenkins instance. The Jenkinsfile provides developers with easy access and flexibility to modify and review the code as needed**.**

4. Different Types of Jenkins CI/CD Pipelines

The Jenkins pipeline syntaxes are as follows–

👉Declarative

👉Scripted

🚀Scripted:

The scripted Jenkins pipeline runs on the Jenkins master with the help of a lightweight executor. It uses very few resources to translate the pipeline into atomic commands. Both declarative and scripted syntax are different from each other and are defined totally differently.

🚀Declarative:

In the context of a pipeline, "decelerative" might refer to an iterative approach, where you move forward in smaller, manageable steps rather than taking huge leaps all at once. This is particularly helpful when tackling complex tasks, as it allows for continuous improvement and adaptation.

Jenkinsfile (Declarative Pipeline)

Example - Software Development:

Let's take software development as an example. In a traditional "waterfall" approach, a team might try to complete the entire software before testing it thoroughly. But in a decelerative (iterative) pipeline, they would break the development into smaller cycles, each producing a functional version of the software.

  • Step 1: Define Requirements - Gather all the necessary information about what the software should do.

  • Step 2: Design - Create a blueprint or plan for the software's structure and functionality.

  • Step 3: Development - Build a small, functional part of the software based on the design.

  • Step 4: Testing - Check the small part to ensure it works correctly and fix any issues.

  • Step 5: Repeat - Continue with new cycles, adding more features with each iteration.

🚀Let's do some hands-on declarative pipeline

check out the first below diagram

👉 First create one EC2 machine Ubuntu os based and use security groups 8000 and 8080 because our Jenkins runs on port no. 8000 and the application runs on 8080

👉 Users can get code from GitHub by simply just copy the repository code URL and cloning it into the system

👉 Now install the docker in your system and give permissions to $USER (ubuntu and Jenkins)

👉and build the image and complete the build stage

👉 As we see the image built in the system, but as we do all jobs through Jenkins so let's install Jenkins in our system, for Jenkins installation do check out my previous blog where I mentioned detailed steps of Jenkins installation.

👉now select the build trigger option

👉this is the complete pipeline

👉now save it and build now option

👉here is the output and check out the console output

👉now let's automate the overall system

  1. By adding a Git URL to get code from GitHub and clone it to the system

  2. In the stage of the building add the docker build command to build an image

  3. In the stage of pushing the image to docker hub by login to docker hub using the environment credential

  4. And the last stage deploying stage for deploying a container

Note: when we create a container and again we create then an error message comes during the build "port already allocate " For that we have docker-compose and need to install it first in our machine and try to create it again

👉 There is another way to build this app by Jenkins and Git integration using Webhook

  1. Select the option

    1. and now create a webhook

the concept of creating a webhook is that whatever change we commit in our code it automatically replicates or updates on the Jenkins side and continues to build this define the complete concept CI/CD infrastructure

  1. At the end run the application on the browser

✔️Here we end with this blog hope it will clear all concepts ...stay tunned.