Jenkins Declarative Pipeline - An Overview with example
🔹What is Pipeline?
In the context of Jenkins, a "pipeline" refers to a series of automated steps or jobs that are organized in a sequence to achieve a specific task or workflow. Pipelines are a fundamental concept in Jenkins, used for defining and automating the steps required to build, test, and deploy software applications.
There are two main ways to define and create pipelines in Jenkins: Declarative and Scripted.
Declarative Pipeline:
Declarative is a more recent and user-friendly way to define pipelines in Jenkins.
It is designed to provide a simpler and more structured syntax for defining pipelines.
Declarative pipelines use a set of predefined, high-level directives and keywords to define stages and steps in a pipeline.
These pipelines are easier to read, write, and maintain, making them a preferred choice for many Jenkins users, especially those who are new to pipeline scripting.
Declarative pipelines encourage best practices and provide good defaults, making them a safer and more controlled way to define pipelines.
Declarative Pipeline Example:
pipeline { agent any stages { stage('Build') { steps { sh 'echo "Building the application"' } } stage('Test') { steps { sh 'echo "Running tests"' } } stage('Deploy') { steps { sh 'echo "Deploying the application"' } } } post { // In this block you define expressions of build status or build status changes. always{ // Actions to perform irrespective of build success or fail } success { // Actions to perform on successful build echo 'Build succeeded!' } failure { // Actions to perform on build failure echo 'Build failed!' } } }
In this Declarative Pipeline example:
We specify that the pipeline can run on any available agent.
We define three stages: Build, Test, and Deploy.
Each stage contains a single step, which is a shell command (
sh
) that prints a message.Declarative pipelines use a predefined structure with
pipeline
,stages
, andsteps
directives to simplify pipeline definition.
Scripted Pipeline:
Scripted pipelines were the original way to define pipelines in Jenkins and are built using Groovy, a powerful scripting language.
Scripted pipelines offer more flexibility and control over the pipeline's logic, making them suitable for complex and custom workflows.
In scripted pipelines, you write custom Groovy scripts to define the pipeline's stages and steps.
This flexibility comes at the cost of increased complexity, as scripted pipelines can become harder to read and maintain, especially for users who are not familiar with Groovy.
Scripted pipelines are still widely used when intricate customization or advanced logic is required for a pipeline.
Scripted Pipeline Example:
node { stage('Build') { echo 'Building the application' // Additional build steps can be added here } stage('Test') { echo 'Running tests' // Additional test steps can be added here } stage('Deploy') { echo 'Deploying the application' // Additional deployment steps can be added here } }
In this Scripted Pipeline example:
We start with a
node
block, which specifies where the pipeline should run.Within each
stage
, we use theecho
command to print messages.Scripted pipelines provide more flexibility to define custom logic and execute arbitrary code within each stage.
The Importance of Having a Pipeline
By writing the pipeline's definition in a text file called a Jenkinsfile, and committing it to the project's source control repository, we're treating our CD pipeline just like any other part of the application's codebase.
This has some immediate advantages:
Consistency
Every branch and pull request follows the same pipeline defined in the Jenkinsfile, ensuring consistency across the development process.
Versioning
The pipeline becomes versioned alongside the application code, making it easier to track changes and roll back if needed.
Code Review
Just like any other code, the pipeline can go through code review and iterations, ensuring it's efficient and error-free.
📖Task-01: Task: Create a New Job using Pipeline Hello World Example
This example demonstrates the basic process of creating and executing a pipeline in Jenkins. Here's a more detailed breakdown of each step:
Step 1: Navigate to the Jenkins Home Page and Create a New Job
Open a web browser and navigate to the Jenkins home page.
Log in if required.
Click on "New Item" to create a new job.
Give a job a name and select "Pipeline" as the job type.
Click "OK" to create the job.
Step 2: Configure the Project with a Valid Description
In the job configuration page, scroll up to the top section where we can edit the job's details.
Add a valid description for the project.
Save the changes.
Step 3: Follow the Official Jenkins Hello World Example
In the configuration page of the new job, scroll down to the "Pipeline" section.
Select the "Pipeline script" option.
Choose the "Pipeline script from SCM" option and provide the necessary repository details. (In this case, you're following the "Hello World" example, so you might choose to use a simple script or select Hello World or a Jenkinsfile in your repository.)
Save the changes.
Step 4: Click on Save and Then Build Now
After saving our pipeline configuration, we can manually trigger a build by clicking "Build Now." This will initiate the pipeline execution based on the code we've written.
Step 5: Check the Full Stage View for Execution Time
Once the pipeline execution starts, we can navigate to the "Full Stage View" to see the different stages of our pipeline and the time it takes for each stage to complete.
Step 6: Verify Hello World Using Console Output
After the pipeline has been completed, we can check the "Console Output" to see the detailed logs of each step's execution.
If everything went as expected, we should see a "Success" status in the console output, indicating that the "Hello World" pipeline was executed successfully.
Subscribe to my newsletter
Read articles from Ajay Gite directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by