Creating a Jenkins Declarative CI/CD Pipeline Using GitHub, Docker, Jenkins, and EC2! ππ».
Today, let's embark on a journey to build a robust and automated CI/CD pipeline using Jenkins, GitHub, Docker, and EC2. Buckle up for a hands-on experience!
π Components of the Pipeline:
GitHub Repository:
Ensure your project is hosted on GitHub.
git clone "https://github.com/RahulSinha9/My-Note-App.git"
Docker Configuration:
Connect to Your EC2 Instance:
Once your instance is running, connect to it via SSH using a terminal or SSH client like PuTTY (for Windows).
Use the key pair associated with your instance for authentication.
Update the Package Index:
Run the following command to ensure that your package index is up to date:
sudo yum update # for Amazon Linux 2 sudo apt update # for Ubuntu
Install Docker:
Install Docker using the package manager apropriate for your Linux distribution:
sudo yum install docker # for Amazon Linux 2
sudo apt install docker.io # for Ubuntu
Start and Enable Docker:
Start the Docker service and enable it to start on boot:
sudo systemctl start docker sudo systemctl enable docker
Building the code using Docker:
Building code using Docker involves creating a Docker image that contains your application and its dependencies. Here's a general guide on how to build code using Docker:
Create a Dockerfile:
In the root of your project, create a file named
Dockerfile
(without any file extension).FROM python:3.9 WORKDIR /app/backend COPY requirements.txt /app/backend RUN pip install -r requirements.txt COPY . /app/backend EXPOSE 8000 CMD python /app/backend/manage.py runserver 0.0.0.0:8000
Build the Docker Image:
Open a terminal in the directory containing your Dockerfile and run the following command:
docker build -t note-app .
Run the Docker Image:
Once the image is built, you can run it using the following command:
docker run -p 8000:8000 -d note-app
Access Your Application:
Open a web browser and navigate to
http://localhost:8000
. (or the port you specified). You should see your application running inside the Docker container.- Jenkins Server on EC2:
Set up a Jenkins server on an EC2 instance. Install the necessary plugins like GitHub Integration, Docker, and Pipeline.
Jenkins Installation:-
sudo apt update sudo apt install openjdk-17-jre java -version Installation of Jenkins 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 jenkins
Dashboard of Jenkins:
π Jenkinsfile:
Create a Jenkinsfile
in the root of your GitHub repository. This declarative pipeline script defines the entire CI/CD process.
pipeline {
agent any
stages{
stage("Clone Code"){
steps {
echo "Cloning the code"
git url:"https://github.com/RahulSinha9/My-Note-App", branch: "master"
}
}
stage("Build"){
steps {
echo "Building the image"
sh "docker build -t my-note-app ."
}
}
stage("Push to Docker Hub"){
steps {
echo "Pushing the image to docker hub"
withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
sh "docker push ${env.dockerHubUser}/my-note-app:latest"
}
}
}
stage("Deploy"){
steps {
echo "Deploying the container"
sh "docker-compose down && docker-compose up -d"
}
}
}
}
Build the CI/CD Pipeline:
A CI/CD pipeline procedure ensures seamless software delivery. Firstly, developers commit code to a version control system triggering automated testing in a CI environment. Tests validate code quality and functionality. Upon successful tests, artifacts are built and deployed to a staging environment for further testing. After approval, the artifacts are promoted to production. CD automates deployment, reducing manual errors and speeding up delivery. Continuous monitoring ensures system health and user satisfaction. Feedback loops inform developers for iterative improvements. Documentation maintains pipeline transparency. Regular reviews and updates keep the pipeline efficient and aligned with project goals.
Push to Docker Hub:
Create a Docker H**ub Account:**
If you don't have a Docker Hub account, go to Docker Hub and sign up.
Login to Docker Hub using the Terminal:
Open a terminal window.
Run the following command and provide your Docker Hub credentials when prompted:
docker login
This command authenticates your Docker client with Docker Hub.
Jenkins Configuration:
Add your Docker Hub credentials to Jenkins and set the credentials ID in the script.
stage("Push to Docker Hub"){
steps {
echo "Pushing the image to docker hub"
withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
sh "docker push ${env.dockerHubUser}/my-note-app:latest"
}
}
}
Execution:
Trigger the Jenkins job manually or configure a webhook to trigger the job on code changes.
Jenkins will checkout your code, build the Docker image, and push it to Docker Hub.
Deploy the Application:
In the same directory as your docker-compose.yml
file, run the following command to deploy your application:
docker-compse up -d
The -d
flag runs the containers in the background.
Docker Compse File:
Docker Compose is a tool that helps you define and share multi-container applications. With Compose, you can create a YAML file to define the services and with a single command, you can spin everything up or tear it all down.
version : "3.3"
services :
web :
image : rahuldss88/note-app:latest
ports :
- "8000:8000"
View Running Containers:
Check the running containers using the following command:
docker ps
Access the Application:
Visit your application in a web browser. For the above example, you can access the web application at http://localhost
/8000
.
Stop and Remove the Application:
When you're done, stop and remove the application using the following command:
docker-compose down
This will stop and remove the containers, networks, and volumes defined in the docker-compose.yml
file.
Adjust the docker-compose.yml
file based on your application's needs, and include additional services, networks, or volumes as necessary.
This steps is Automated with the help of jenkins:
stage("Deploy"){
steps {
echo "Deploying the container"
sh "docker-compose down && docker-compose up -d"
}
}
This scripts will automate the processe.
Now finally, access the Application:
Open a web browser and navigate to http://localhost:8000
(or the port you specified). You should see your application running.
Congratulations! You've successfully orchestrated a DevOps pipeline that integrates GitHub, Docker, Jenkins, and AWS for seamless development and deployment. This project lays a solid foundation for future enhancements and innovations. ππ #DevOpsProject #GitHub #Docker #Jenkins #AWSIntegration #ContinuousIntegration #ContinuousDeployment π