Jenkins Declarative Pipeline: A Step-by-Step Guide for Day 26
Imagine a world where every time you make a change to your code, everythingโbuilding, testing, and deployingโhappens automatically, with no manual effort. Sounds like magic? It's Jenkins Pipelines! ๐ช
Jenkins acts like your automation sidekick, and Jenkins Pipelines allow you to define your entire workflow in a simple file called a Jenkinsfile. You commit this file alongside your code, and boomโyour entire build process is automated, versioned, and ready for any changes. ๐
There are two types of pipelines: Declarative (modern and easy to use) and Scripted (the older, more complex version). Weโll focus on the Declarative pipeline because itโs super simple and efficient! ๐ก
Why do you need a pipeline? Hereโs why:
Automation: Automatically build and test your project on every branch or pull request.
Versioning: Treat your pipeline like code, versioned and reviewed.
Efficiency: Save time and reduce errors by automating repetitive tasks. โฑ๏ธ
We'll walk you through setting up a Jenkins job that runs your pipeline, and we'll use a super simple "Hello World" example to get you started. You'll learn how to create a Jenkinsfile (the blueprint for your pipeline), commit it to your project, and watch Jenkins take care of the rest. ๐
By the end of this tutorial, you'll be all set to say goodbye to manual steps and hello to continuous integration and delivery! So, letโs get started!
Prerequisites:
Before we jump in, make sure you have the following:
Jenkins Installed: You need a running Jenkins instance. If you don't have it, you can install it on your system (Jenkins supports Windows, macOS, and Linux).
A GitHub or Git Repository: Youโll need a project repository with code to work with (any small project will do).
Basic Understanding of Jenkins: A simple understanding of Jenkins jobs and pipelines would be helpful, though not mandatory.
Jenkins Pipeline Plugin Installed: Make sure the Jenkins Pipeline plugin is installed in your Jenkins instance to create pipeline jobs.
Basic Knowledge of Groovy: Since Jenkins pipelines often use Groovy scripts, a little knowledge about it will come in handy. But donโt worry if you're not a Groovy expert! We'll keep things simple.
Once you're ready with these, we can kickstart the process and create a powerful pipeline to automate your tasks! Let's go! ๐
What is a Jenkins Pipeline?๐ค
Think of a pipeline like an assembly line in a factory. It's a sequence of steps (or jobs) that get executed one after the other. Each step builds on the previous one. For Jenkins, a pipeline automates the entire process of building, testing, and deploying your code.
There are two types of pipelines:
Declarative Pipeline: This is the new, easier way to define your pipeline using a clear structure and syntax.
Scripted Pipeline: The traditional way, using Groovy code, which is more flexible but can be a bit complicated.
Why Should You Use a Pipeline?๐ค
By defining your Jenkins pipeline in a file (called a Jenkinsfile), you get several advantages:
Version Control: You can track your pipeline like any other code, making it easier to collaborate and review.
Automation: Pipelines can automatically run for every change (like creating new branches or pull requests), reducing manual work and ensuring consistency.
Now, let's dive into the task of creating your first Declarative Pipeline in Jenkins!
Pipeline Syntax Explained
Here's the basic syntax for a Jenkins Declarative Pipeline:
pipeline {
agent any // Can run on any available agent
stages { // Collection of stages (jobs)
stage('Build') {
steps {
// Define steps for build (e.g., compile, package, etc.)
}
}
stage('Test') {
steps {
// Define steps for tests (e.g., unit tests, integration tests)
}
}
stage('Deploy') {
steps {
// Define deployment steps (e.g., push to server, cloud)
}
}
}
}
Explanation of Syntax:
pipeline
: Top-level block defining the whole pipeline.agent any
: Specifies that the pipeline can run on any available Jenkins agent.stages
: A collection ofstage
blocks, each representing a job in the pipeline.stage('Build')
: The first stage where code is compiled or built.stage('Test')
: The second stage where tests are executed.stage('Deploy')
: The last stage where the code is deployed.
Task-01: Creating Your First Jenkins Pipeline
Letโs create a Jenkins Pipeline Job using the Declarative pipeline! ๐
Steps to Follow:
Create a New Jenkins Job:
Go to Jenkins Dashboard.
Click on New Item.
Choose Pipeline (not Freestyle project).
Enter a name for your job (e.g.,
MyFirstPipeline
).Click OK.
Configure the Pipeline:
Scroll down to the Pipeline section.
In the Definition dropdown, select Pipeline script.
Copy-paste the following Declarative pipeline code:
Jenkinsfile (Declarative Pipeline)
/* Requires the Docker Pipeline plugin */
pipeline {
agent { docker { image 'node:22.12.0-alpine3.20' } }
stages {
stage('build') {
steps {
sh 'node --version'
}
}
}
}
Run the Pipeline:
Save your configuration and click on Build Now.
Watch your first Pipeline run
๐ Congratulations! You've just created your first Jenkins pipeline using Declarative syntax! Now you can automate the entire process of building, testing, and deploying your code.
And thatโs a wrap! ๐ Youโve learned the building blocks of Jenkins pipelinesโfrom understanding the difference between Declarative and Scripted to how pipelines function as code. Youโre now equipped to start automating the delivery of your applications with the ease and flexibility of Jenkins.
By following this example, youโve created a simple pipeline, and with the knowledge gained, you can go on to create more complex and robust pipelines for your projects.
Remember, the more you play around with Jenkins, the more youโll master the art of continuous integration and continuous delivery. Keep experimenting, keep learning, and soon youโll be running pipelines like a pro!
So, grab that Jenkinsfile and start automating your workflowsโbecause the future of DevOps is all about pipelines! ๐ฌ
๐ Follow my journey here:
๐ LinkedIn
๐ Hashnode
๐ GitHub
Until next time, keep automating and innovating! ๐ก๐
Happy Learning! โจ