đ DevOps project: Automating Web App Deployment with Jenkins & GitHub Pipeline!đ§
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 will have a pipeline that includes various stages such as cloning, building, pushing to Docker Hub, clean up, 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:
To stay informed about the pipelineâs status and any critical issues, weâll configure notifications and alerts. Jenkins will send notifications to relevant team members through various communication channels, such as email, Slack, or other messaging platforms. This proactive approach ensures swift resolution of any potential problems.
Step 1: Set Up Jenkins
- Ensure you have Jenkins installed and running. If not, follow the installation instructions for your specific platform. Iâll use an Ubuntu EC2 instance and the below User Data file.
#!/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
2. Access the Jenkins web interface by navigating to http://localhost:8080
or the URL where Jenkins is hosted.
3. Once logged in, proceed to install the suggested plugins.
4. Set up the necessary global configurations, such as DockerHub credentials and email notification settings, in Jenkins.
Step 2: Create a New Jenkins Job
Click on âNew Itemâ on the Jenkins dashboard to create a new Jenkins job.
Enter a suitable name for the job (e.g., âDevOps Project 2â).
Choose the âPipelineâ option and click âOKâ to create the job.
Step 3: Configure the Jenkins Pipeline
- In the General Section, tick the
GitHub project
box and provide your repository URL.
2. 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.
3. Navigate to the âPipelineâ section.
4. Select the âPipeline scriptâ option and enter the declarative pipeline syntax for your project.
pipeline {
agent any
stages {
stage('Clone Code') {
steps {
// Build steps go here
git url: 'https://github.com/estebanmorenoit/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 {
// Stop and remove the container if it exists
sh 'docker stop django-notes-app || true'
sh 'docker rm django-notes-app || true'
}
}
stage("Deploy") {
steps {
// Run the container and bind port 8000
sh 'docker run -d -p 8000:8000 --name django-notes-app django-notes-app'
}
}
}
post {
success {
emailext (
to: 'morenoramirezesteban@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: 'morenoramirezesteban@gmail.com',
subject: 'Deployment Failed',
body: 'The deployment to production failed. Please check the Jenkins console output for more details.',
)
}
}
}
5. Apply the changes and save the Pipeline.
Step 4: Configure Notifications and Alerts:
To set up email notifications using Gmail as the SMTP server in Jenkins, follow these steps:
- Install the Email Extension Plugin:
Go to âManage Jenkinsâ > âManage Pluginsâ > âAvailableâ tab.
Search for âEmail Extensionâ in the filter box.
Check the checkbox next to âEmail Extensionâ and click âInstall without restart.â
2. Configure Gmail SMTP Server in Jenkins:
Go to âManage Jenkinsâ > âConfigure System.â
Scroll down to the âExtended E-mail Notificationâ section.
In the âSMTP serverâ field, enter Gmailâs SMTP server address:
smtp.gmail.com
Set the âDefault user e-mail suffixâ to
@gmail.com
Click âAdvancedâ to expand additional settings.
Set âSMTP portâ to
465
.Check the âUse SMTP Authenticationâ box.
Enter your Gmail email address in the âUser Nameâ field.
Click âAddâ to add a new âPasswordâ and enter your Gmail accountâs password.
Check the âUse SSLâ box.
Save the configuration.
3. Enable âLess Secure Appsâ in Gmail (Only if Required):
If you encounter authentication issues, you may need to enable âLess Secure Appsâ in your Gmail account settings.
Under âSigning in to Google,â click âApp passwords.â
Sign in to your Google account if prompted.
Scroll down to âAllow less secure appsâ and turn it ON.
Generate an app password and use it in the Jenkins email configuration instead of your regular Gmail password.
Step 5: Trigger the Jenkins Job
Push changes to your projectâs repository on GitHub to trigger the Jenkins job.
Jenkins will automatically detect the changes and start running the pipeline.
Step 6: Monitor and Verify the Pipeline
- Monitor the progress of the pipeline execution in the Jenkins web interface. Check the console output for any errors or issues.
2. Navigate to http://<ec2-instance-public-ip-address>:8000
and access your newly deployed app.
3. Wait for an email notification to land in your inbox
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. Stay tuned for a new project on Day 82 of the #90daysofdevops challenge!
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 on LinkedIn: www.linkedin.com/in/trushid-hatmode
Connect with me on GitHub: https://github.com/Trushid