In my previous blog, we delved into setting up a basic continuous integration and continuous deployment (CI/CD) pipeline for Node.js applications using Jenkins. In this blog, we'll take a deeper dive into Jenkins Declarative Pipeline, an essential feature for modern DevOps practices.
You can check my previous blog here: "Complete Jenkins CI/CD Project on Node.js"
What is a Pipeline: In CI/CD, a pipeline automates code compilation, testing, and deployment. It's a sequence of steps ensuring automatic building, testing, and deployment, enhancing development efficiency.
Declarative vs. Scripted Pipeline: Jenkins supports two types of pipelines, Declarative and Scripted. Declarative Pipeline, which we'll focus on here, provides a simplified and more friendly syntax for defining your pipeline. It's a great choice for beginners and projects with straightforward requirements. It's easy to read and understand, making it a popular choice among developers.
Why Should We Have a Pipeline?
A Jenkins Pipeline is defined in a text file called a Jenkinsfile, which can be added to a project's source control repository(SCM). This file contains instructions for the Jenkins Pipeline and is usually stored alongside the project code it builds or deploys.
This forms the basis of "Pipeline-as-code," where the continuous delivery pipeline is treated as an integral part of the application. It can be versioned and reviewed just like any other code component.
A pipeline is crucial for several reasons:
Automation: Pipelines automate the entire process, reducing manual intervention and potential errors.
Consistency: They ensure consistency across different environments, ensuring that code behaves the same way in all stages of development.
Visibility: Pipelines provide transparency, allowing team members to track the progress of changes from code commit to deployment.
Efficiency: By automating repetitive tasks, pipelines free up developers' time, allowing them to focus on writing code and fixing issues.
Pipeline Syntax
pipeline {
agent any
stages {
stage('Build') {
steps {
//
}
}
stage('Test') {
steps {
//
}
}
stage('Deploy') {
steps {
//
}
}
}
}
Task 01
Start by creating a New Job and choose Pipeline instead of a Freestyle Project.
Refer to the Official Jenkins Hello World example for guidance.
Complete the example by implementing the Declarative pipeline approach.
Start by setting up an EC2 instance and installing Jenkins on it. Access Jenkins through the instance's public IP and port 8080.
After successfully installing and accessing Jenkins via your browser, navigate to the Jenkins Dashboard and click on "New Item."
Refer to my previous blog on "Getting Started with Jenkins"
In this step, provide the Project Name and choose the project type as "pipeline." This selection is necessary as we'll be creating the JenkinsFile in the subsequent steps.
Select "OK" to proceed. You will then be directed to the "Project Configuration" section.
Navigate to the "Pipeline" section, where you should choose "Pipeline script" as the definition.
Next, we will create a simple Pipeline Script that displays "Hello World".
Press the Save button to store the Pipeline configuration.
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
Code explanation
Pipeline:
- The Declarative pipeline must begin with the pipeline block, which is essential and mandatory.
Agent:
The agent specifies the location where the Jenkins build job should be executed. Here, we've chosen "any" as the agent, indicating it can run on any available environment.
Jenkins provides support for a diverse range of agents. For a comprehensive list of supported agents in Jenkins.
Stages/stage:
Stages Description: Within the stages block, there are various executable stage blocks.
Mandatory Stage: At least one stage block is required within the stages block for the pipeline to function.
Example Stage Name: In this case, we've named the stage as "Hello" for illustrative purposes.
Next, we can initiate the build process by clicking on the "Build Now" tab manually.
After the build is finished, you can review the build output by clicking on the "Console Output" tab available.
Here, we observe the successful execution of the pipeline, displaying the message "Hello World."
Now, we can view the multi-stage layout by clicking on "Full Stage View" on the project's main page.
Conclusion
Incorporating Jenkins Declarative Pipeline into your CI/CD workflow can significantly enhance your development process. It simplifies the management of complex build, test, and deployment tasks, making your software delivery pipeline more efficient and reliable.๐
I hope this blog post has provided valuable insights into the world of Jenkins Declarative Pipeline. For more updates on DevOps practices, CI/CD, and related technologies, feel free to connect with me on LinkedIn. ๐
Let's continue the conversation and explore the exciting realms of software development together.๐ค