👋 Welcome to Day 81 of the #90DaysOfDevOps challenge!
Today’s project focuses on automating the deployment process of a web application using Jenkins and its declarative syntax. By the end of this project, you'll have a pipeline that includes various stages such as cloning, building, pushing to Docker Hub, cleanup, and deploying. Let’s dive in and start working on our DevOps Project 2! 🚀
📄 Project Description
Automate the entire web application development process using Jenkins and GitHub. Our goal is to build a powerful Jenkins pipeline for continuous integration and continuous deployment (CI/CD). Leverage Jenkins’ automation capabilities and GitHub’s version control to achieve seamless software delivery. 🛠️💻
📋 Project Workflow
Code Changes Detection: The pipeline automatically detects code changes in the GitHub repository. 🔍
Build Stage: Jenkins initiates the build stage, compiling code and generating artifacts. 🏗️
Deployment Stage: After a successful build stage, the application goes live for users. 🌐
Notifications and Alerts: Configure notifications and alerts to stay informed about the pipeline’s status and any critical issues. 📩🚨
🔧 Hands-on Project: Jenkins Declarative Pipeline
Step 1: Set Up Jenkins
Launch an Ubuntu EC2 Instance
Launch a new Ubuntu EC2 instance on AWS. ☁️
Make sure to open ports 8080 (Jenkins) and 22 (SSH) in the security group. 🔐
Install Jenkins and Docker
Use the following user data script to install Jenkins and Docker during instance initialization: 📝
#!/bin/bash echo "Docker installation" echo "------------------------------------------------------------------" sudo apt update sudo apt install -y apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt update sudo apt install -y docker-ce docker-ce-cli containerd.io sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER echo "Jenkins installation" echo "------------------------------------------------------------------" sudo apt update sudo apt install -y openjdk-17-jre curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install -y jenkins echo "Add Jenkins to Docker Group" echo "------------------------------------------------------------------" sudo usermod -aG docker jenkins
Access Jenkins
Navigate to http://<EC2-Public-IP>:8080. 🌐
Follow the on-screen instructions to unlock Jenkins and complete the setup. 🔓
Install Suggested Plugins
Once logged in, proceed to install the suggested plugins. 🔌
Set Up Global Configurations
- Configure DockerHub credentials and email notification settings in Jenkins. ⚙️
Step 2: Create a New Jenkins Job
Create a New Job
Click on “New Item” on the Jenkins dashboard. 🆕
Enter a suitable name for the job (e.g., “DevOps Project 2”). 📝
Choose the “Pipeline” option and click “OK” to create the job. ✅
Configure the Jenkins Pipeline
In the General Section, tick the GitHub project box and provide your repository URL. 🌐
Build Triggers
Move to the “Build Triggers” section and select “Poll SCM” to trigger the Jenkins Pipeline when changes are discovered. We’ll schedule it to run a scan every minute. ⏲️
Pipeline Script
Navigate to the “Pipeline” section. 📜
Select the “Pipeline script” option and enter the declarative pipeline syntax for your project: 💻
pipeline { agent any stages { stage('Clone Code') { steps { git url: 'https://github.com/LondheShubham153/django-notes-app.git', branch: 'main' } } stage("Build") { steps { sh 'docker build . -t django-notes-app' } } stage("Push to Docker Hub") { steps { withCredentials([usernamePassword(credentialsId: "dockerhub", passwordVariable: "dockerhubPass", usernameVariable: "dockerhubUser")]) { sh "docker tag django-notes-app ${env.dockerhubUser}/django-notes-app:latest" sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}" sh "docker push ${env.dockerhubUser}/django-notes-app:latest" } } } stage("Cleanup") { steps { sh 'docker stop django-notes-app || true' sh 'docker rm django-notes-app || true' } } stage("Deploy") { steps { sh 'docker run -d -p 8000:8000 --name django-notes-app django-notes-app' } } } post { success { emailext ( to: 'nilkanthmistry0@gmail.com', subject: 'Deployment Successful', body: 'The deployment to production was successful. You can access the application at http://<ec2-instance-public-ip-address>:8000', ) } failure { emailext ( to: 'nilkanthmistry0@gmail.com', subject: 'Deployment Failed', body: 'The deployment to production failed. Please check the Jenkins console output for more details.', ) } } }
Apply and Save
Apply the changes and save the pipeline configuration. 💾
Step 3: Configure Notifications and Alerts
Install Email Extension Plugin
Go to “Manage Jenkins” > “Manage Plugins” > “Available” tab. 🔍
Search for “Email Extension” and install it. 📥
Configure Gmail SMTP Server
Go to “Manage Jenkins” > “Configure System.” ⚙️
Scroll down to the “Extended E-mail Notification” section. 📧
Enter the following details:
SMTP server: smtp.gmail.com
Default user e-mail suffix: @gmail.com
SMTP port: 465
Use SMTP Authentication: Check the box ☑️
User Name: Your Gmail email address ✉️
Password: Your Gmail account’s password or an app password 🔑
Use SSL: Check the box ☑️
Save the configuration. 💾
Enable “Less Secure Apps” in Gmail (If Required)
Go to myaccount.google.com/security. 🌐
Under “Signing in to Google,” click “App passwords.” 🔐
Generate an app password and use it in Jenkins. 🔑
Step 4: Trigger the Jenkins Job
Push Changes to GitHub
Push changes to your project’s repository on GitHub to trigger the Jenkins job. 🚀
Monitor Pipeline Execution
Monitor the progress of the pipeline execution in the Jenkins web interface. Check the console output for any errors or issues. 🖥️
Step 5: Verify the Deployment
Access the Deployed App
Navigate to http://<ec2-instance-public-ip-address>:8000 to access your newly deployed app. 🌐
Check Email Notifications
Wait for an email notification to land in your inbox. 📩
🎉 Conclusion
In today’s challenge, we tackled DevOps Project 2, automating the deployment process of a web application using Jenkins. We covered setting up Jenkins, creating a Jenkins pipeline, defining stages, triggering the pipeline, and deploying the web application to our environment. By completing this project, we have gained practical experience in continuous integration and continuous deployment (CI/CD) practices. 📈💼
🙌 Show Your Support
I hope you learned something from this blog. If you have, don’t forget to follow and click the clap 👏 button below to show your support 😄. Subscribe to my blogs so that you won’t miss any future posts. 📚✨
If you have any questions or feedback, feel free to leave a comment below. Thanks for reading and have an amazing day ahead! 🌟
🔗 Connect with me: