Jenkins Declarative Pipeline with Docker
In today's world of continuous integration and continuous delivery (CI/CD), automation is the backbone of efficient software development. This is where Jenkins Pipelines come in handy. They allow teams to automate their processes, from building and testing to deploying their code, all in a streamlined way. So, what exactly is a pipeline, and how does it work in Jenkins? Let's dive in!
π οΈ What is a Pipeline?
A pipeline in Jenkins is essentially a suite of automated tasks that are executed in a defined sequence. Think of it as a workflow or a set of instructions that tells Jenkins how to build, test, and deploy your application.
Pipelines are typically written in Groovy-based domain-specific language (DSL) and can range from simple scripts with just a few steps to complex workflows that integrate various stages like compilation, testing, deployment, and more.
Key Benefits of Pipelines:
Automation: Reduces manual work and human errors.
Transparency: Provides visibility into the status of each stage in your process.
Scalability: Can handle both simple and complex workflows.
Version Control: Pipeline scripts can be versioned just like code, ensuring traceability and easier collaboration.
π Types of Pipeline: Declarative vs. Scripted Pipeline
In Jenkins, there are two types of pipelines: Declarative and Scripted. Letβs compare the two:
Feature | Declarative Pipeline | Scripted Pipeline |
Syntax | Simple and more structured | Flexible and powerful, but complex |
Error Handling | Built-in error handling, easier for beginners | Requires custom error handling, more control |
Use Case | Best for most use cases, encourages best practices | Best for complex use cases requiring customization |
Complexity | Easier to write and understand | More complex, especially for large pipelines |
Pipeline Block | Requires pipeline block with predefined stages | No strict structure; more freedom |
When to Use | Ideal for standard CI/CD workflows | When you need advanced customizations |
π What is a Declarative Pipeline?
The Declarative Pipeline syntax provides a structured and opinionated way to define your pipeline. It follows a well-defined structure with mandatory sections like pipeline
, stages
, and steps
. This makes it easier to understand and use for most CI/CD workflows.
Here's an example of a simple Declarative Pipeline:
pipeline {
agent any
stages {
stage('Build'){
steps {
echo 'Building...'
}
}
stage('Test'){
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
Task: Create a Declarative Pipeline
Create a Pipeline to automate the task of creating containers and running them through docker and also Pushing image to Docker Hub, all covered under 1 Script.
1) Setup Jenkins Environment: Follow my Blog of Day x for the same.
2) Create a Pipeline: Navigate as follows: Dashboard -> New Item -> Select Pipeline -> Give name to Pipeline -> Create -> Give Description -> GitHub Project URL for code cloning.
3) Pipeline Script: Now write the Pipeline Script as below (Select Pipeline Script for now as option):
agent: Means that we have specified here that it will run on agent which has label dev. Note: Agent with this label needs to be created.
stage: Defines the different stages present in the Pipeline.
steps: Defines the steps in order that need to take place in that order.
Save the Pipeline.
Make sure Agent is ready to run the Job and docker is already installed on it.
Play / Run the playbook while monitoring the logs to see if Pipeline ran successfully.
Once done, output will look like as shown below and application will be running on Port 8000 as we mapped that in our script.
In case we use docker-compose instead of docker, the only change in script is as shown below (Note: docker-compose should be installed on agent):
π Conclusion
The Jenkins Pipeline is a powerful tool that automates and streamlines your CI/CD processes. By understanding the difference between Declarative and Scripted Pipelines, you can choose the right approach based on your needs.
Declarative Pipelines: Provide a structured, easier-to-read format and are ideal for most users and workflows.
Scripted Pipelines: Offer flexibility and customization for complex use cases, but come with a steeper learning curve.
For most teams, the Declarative Pipeline is the best starting point, offering simplicity and structure while still being powerful enough to handle a wide range of use cases.
Congrats to you if you try and achieve this successfully, you are one step close to become a successful DevOps Engineer!!!
Happy automating! π
Subscribe to my newsletter
Read articles from Deepesh Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Deepesh Gupta
Deepesh Gupta
DevOps & Cloud Enthusiast | Open Source Contributor | Driving Technological Excellence in DevOps and Cloud Solutions