Project Title: Jenkins Declarative Pipeline with Email Notification Part-2🚀
Table of contents
- 🌟Introduction
- 🌟Project Overview
- 🔱Pre-requisites
- 🔱Let's get started--->
- ✔Step 1: Get the source code of the web App
- ✔Step 2: Make the server ready for deployment purposes
- ✔Step 4: Create a Jenkins Pipeline project
- ✔Step 5: Docker Hub credentials
- ✔Step 6: Create a Jenkinsfile for our pipeline to run
- ✔Step 7: Run the Pipeline manually to check for any errors
- ✔Step 8: Check whether the docker image successfully upload to the docker hub or not
- ✔Step 9: Setup AWS SES Service
- ✔Step 10: Setup SMTP in Jenkins
- ✔Step 11: Setup Cloud Watch metrics of CPU Utilization in Percentage(%)📺📈
- 🔱Clean Up
- 🌟Conclusion:
🌟Introduction
Continuous Integration and Continuous Delivery (CICD) are crucial aspects of software development that help us build, test, and deploy software quickly and efficiently.
🌟Project Overview
Our project is a weather application application. To automate the deployment process of this application, we have set up a CI/CD pipeline using Jenkins. The pipeline consists of several stages, including code cloning, building Docker images, deploying services to servers using Docker Compose, and updating the Docker registry, on the server. Get email notifications on your Gmail account.
🔱Pre-requisites
A GitHub account to store the source code.
One server/machine :
a) Jenkins declarative
Jenkins, Docker and Docker Compose are installed.
Docker Hub is used to store Docker-versioned images.
Knowledge of Groovy syntax to create the Jenkins pipelines.
Set up AWS Simple Email Service (cost-effective email service).
🔱Let's get started--->
✔Step 1: Get the source code of the web App
Fork this repo:
https://github.com/dushyantkumark/Weather-App.git
✔Step 2: Make the server ready for deployment purposes
Create an ec2 instance, one as the master node for deployment purposes.
Instances can be t2-medium, 2 CPU, 4 GiB Memory, and ports 8080 , 80, 22, 3000 are open to allow incoming traffic
HOW TO SETUP AN INSTANCE FOLLOW THE PART1 LINK
Check versions.
Access your Jenkins on Localhost[EC2](localhost%5Bec2/)**\ [instance public ip]:8080**
✔Step 4: Create a Jenkins Pipeline project
✔Step 5: Docker Hub credentials
✔Step 6: Create a Jenkinsfile for our pipeline to run
pipeline{
agent any
stages{
stage('Copy Code'){
steps{
echo "Cloning the code"
git url:"https://github.com/dushyantkumark/Weather-App.git", branch:"main"
}
}
stage('Build'){
steps{
echo "Building the image"
sh "docker build -t weather_app ."
}
}
stage('Logging & Pushing to DockerHub'){
steps{
echo "Pushing the image to dockerhub"
withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
sh "docker tag weather_app ${env.dockerHubUser}/weatherapp:latest"
sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
sh "docker push ${env.dockerHubUser}/weatherapp:latest"
}
}
}
stage('Deploy'){
steps{
echo "Deploying the container"
sh "docker-compose down && docker-compose up -d"
}
}
}
The pipeline has several stages that are executed sequentially:
"code" stage: This stage checks out the code from the GitHub repository using Git.
"Build docker images" stage: This stage builds Docker images for the different components of the multi-container application, such as worker, result, and vote.
"Deploy services" stage: This stage deploys the Docker containers for the application using docker-compose.
"Update docker images" stage: This stage updates the Docker images on DockerHub by pushing the newly built images
The pipeline is executed and sets an environment variable DOCKER_IMAGE_TAG using the build number. The pipeline uses several shell scripts to perform Docker-related tasks . The pipeline also uses credentials to log in to DockerHub and push images.
✔Step 7: Run the Pipeline manually to check for any errors
✔Step 8: Check whether the docker image successfully upload to the docker hub or not
The build Succeeded and we can access the Application on the development server, for this Please allow 3000 port in the security group to check the weather app.
✔Step 9: Setup AWS SES Service
Go to the AWS console and search SES (Simple Email Service)
Once logged in, navigate to the Amazon SES dashboard by searching for "Simple Email Service" in the AWS Management Console's search bar.
Before you can send emails using Amazon SES, you need to verify the email addresses or domains you'll be sending from. This helps ensure your emails are trusted and less likely to be marked as spam.
Verify Email Addresses: You can verify individual email addresses that you want to send from. Amazon SES will send a verification email to each address, and you need to follow the verification steps in that email.
Verify Domains: If you want to send emails from a specific domain (e.g., example.com), you can verify the domain instead of individual email addresses. This typically involves adding DNS records to your domain's DNS settings.
Verify this URL
If you want to use Amazon SES to send emails from your applications, you can create SMTP credentials. These credentials can be used with any email client or application that supports SMTP authentication.
You get your SMTP credentials such as IAM user name, SMTP user name and SMTP password.
✔Step 10: Setup SMTP in Jenkins
Now Jenkins CI/CD pipeline code be
pipeline{
agent any
stages{
stage('Copy Code'){
steps{
echo "Cloning the code"
git url:"https://github.com/dushyantkumark/Weather-App.git", branch:"main"
}
}
stage('Build'){
steps{
echo "Building the image"
sh "docker build -t weather_app ."
}
}
stage('Logging & Pushing to DockerHub'){
steps{
echo "Pushing the image to dockerhub"
withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
sh "docker tag weather_app ${env.dockerHubUser}/weatherapp:latest"
sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
sh "docker push ${env.dockerHubUser}/weatherapp:latest"
}
}
}
stage('Deploy'){
steps{
echo "Deploying the container"
sh "docker-compose down && docker-compose up -d"
}
}
}
post{
success{
emailext attachLog: true, body: 'Email send out from Jenkins', subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: 'kumardushyant545@gmail.com'
}
failure{
emailext attachLog: true, body: 'Email send out from Jenkins', subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: 'kumardushyant545@gmail.com'
}
}
}
Run this pipeline code
After a successful pipeline run, you get an email from Jenkins with the pipeline name, build number, build status and log file attached.
WOW, your application is running successfully.🔥
<ip_address>:3000
✔Step 11: Setup Cloud Watch metrics of CPU Utilization in Percentage(%)📺📈
🔱Clean Up
To clean up and delete all of the infrastructure resources we created, run the following command:
🔥Terraform Destroy Command is used when you complete your project, this is only a setup process. The next stage is Jenkins CI/CD pipeline with email message (your build is successful with the project name, build number and build log).
terraform destroy
IF YOU WANT TO KNOW HOW TO SET UP SERVER REFER TO PART1 BLOG
suggestions are always welcome🔥
🌟Conclusion:
In this blog, we explored the significance of implementing Continuous Integration/Continuous Delivery (CI/CD) pipelines in software development. Demonstrated how to create a Declarative pipeline in Jenkins that can automatically build Docker images for updated app code and deploy them to the targeted deployment environment, notify management teams about new releases/updates, and perform clean-up for the host server.
Using CICD pipelines teams can streamline their software development processes, improve software quality, and achieve faster release cycles. By automating the repetitive and error-prone tasks of building, testing, and deployment, teams can focus on writing high-quality code and delivering value to their users.
In the end, we know how to use aws ses service and integrate it with Jenkins.
\...................................................................................................................................................
The above information is up to my understanding. Suggestions are always welcome. Thanks for reading this article.
#terraform #aws #hcl #Devops #jenkins #CICD #TrainWithShubham #90daysofdevopsc #happylearning
Follow for many such contents:
LinkedIn: linkedin.com/in/dushyant-kumar-dk
Blog: dushyantkumark.hashnode.dev
TERRAFORM_SERVER_SETUP: github.com/dushyantkumark/Terraform_Jenkins..
PROJECT_GITHUB: https://github.com/dushyantkumark/Weather-App