(Day 27 )Task: Jenkins Declarative Pipeline with Docker
Introduction:
Welcome back to my 90DaysOfDevOps journey! On day 27, I delved into the world of Continuous Integration and Continuous Deployment (CI/CD) by creating a robust pipeline for my Node.js application. This blog post will walk you through the process of setting up a Docker-integrated Jenkins declarative pipeline and seamlessly integrating it with GitHub through a webhook.
Task 1: Creating a Docker-Integrated Jenkins Declarative Pipeline
Step 1: Login to Jenkins
The first step in our CI/CD journey was logging into Jenkins. If you haven't installed Jenkins yet, make sure to do so and access it through your browser.
Step 2: Creating a New Item
Navigate to the Jenkins dashboard and click on "New Item." Choose the "Pipeline" option and give your pipeline a descriptive name.
Dashboard:
Select: Pipeline
Step 3: Configuring Required Fields
In the configuration section, specify the GitHub repository URL of your Node.js application. Additionally, set up the necessary build triggers to initiate the pipeline on code changes.
Description for Node-todo-app:
GitHub Hook Trigger:
Step 4: General Configuration
Under the "General" tab, configure your pipeline settings. Define parameters like the build discard settings and other general options according to your project requirements.
Step 5: Advanced Project Options
Explore the "Advanced Project Options" to fine-tune your pipeline's behavior. This is where you can set up post-build actions, custom workspace locations, and other advanced settings.
Step 6: Declarative Pipeline in Groovy
The heart of our CI/CD pipeline is the Groovy code that defines the declarative pipeline. Below is a snippet showcasing the essential structure:
Pipeline Script:
pipeline{
agent{
label "label-1"
}
stages{
stage('Code'){
steps{
git url: "https://github.com/ansarshaik965/node-todo-cicd.git", branch: "master"
sh "echo Codeclone-->Done!"
}
}
stage('Build'){
steps{
sh "docker build -t node-todo-app:latest . "
sh "echo imageBuild-->Done!"
}
}
stage('Push to Dockerhub'){
steps{
withCredentials([usernamePassword(credentialsId:"Dockerhub",passwordVariable:"DockerhubPass",usernameVariable:"DockerhubUser")]){
sh "docker login -u ${env.DockerhubUser} -p ${env.DockerhubPass}"
sh "docker tag node-todo-app:latest ${env.DockerhubUser}/node-todo-app:latest"
sh "docker push ${env.DockerhubUser}/node-todo-app:latest"
sh "echo imagePush-->Done!"
}
}
}
stage('Deploy'){
steps{
sh "docker-compose down && docker-compose up -d"
sh "echo Deploy--Done!"
}
}
}
}
Customize the stages according to your application's build, test, and deployment requirements.
Build:
Stage Logs(Code):
Stage Logs(Build):
Stage Logs(Push to Dockerhub):
Stage Logs(Deploy):
Result:
Conclusion:
Day 27 of the 90DaysOfDevOps challenge was dedicated to establishing a solid foundation for CI/CD. By creating a Docker-integrated Jenkins declarative pipeline and seamlessly integrating it with GitHub through a webhook, I've streamlined the development and deployment process for my Node.js application. Stay tuned for more updates as I continue my journey into the world of DevOps!
Subscribe to my newsletter
Read articles from ANSAR SHAIK directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
ANSAR SHAIK
ANSAR SHAIK
AWS DevOps Engineer