This repository demonstrates a Jenkins pipeline implementation for achieving continuous integration and continuous deployment (CI/CD) using Docker and GitHub.
The CI/CD pipeline provided here allows you to automate the process of building, testing, and deploying your application whenever changes are pushed to the GitHub repository. Jenkins, an open-source automation server, is used as the orchestrator for the pipeline. Docker, a popular containerization platform, is utilized to manage the deployment process.
Before setting up the pipeline, ensure that you have the following prerequisites:
- An Amazon EC2 Linux machine with Jenkins, Git, and Docker installed.
- A GitHub repository containing your application code and Dockerfile.
The CI/CD pipeline workflow is represented as follows:
To install Jenkins, follow these steps:
-
Ensure that your software packages are up to date on your instance by using the following command to perform a quick software update:
sudo yum update –y
-
Add the Jenkins repository using the following command:
sudo wget -O /etc/yum.repos.d/jenkins.repo \ https://pkg.jenkins.io/redhat-stable/jenkins.repo
-
Import a key file from Jenkins-CI to enable installation from the package:
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
-
Upgrade the system packages:
sudo yum upgrade
-
Install Java (Amazon Linux 2):
sudo amazon-linux-extras install java-openjdk11 -y
Install Java (Amazon Linux 2023):
sudo dnf install java-11-amazon-corretto -y
-
Install Jenkins:
sudo yum install jenkins -y
-
Enable the Jenkins service to start at boot:
sudo systemctl enable jenkins
-
Start Jenkins as a service:
sudo systemctl start jenkins
-
You can check the status of the Jenkins service using the command:
sudo systemctl status jenkins
To install Docker, follow these steps:
-
Install Docker:
sudo yum install docker -y
-
Enable Docker to start on system boot:
sudo systemctl enable docker
-
Start the Docker service:
sudo systemctl start docker
To install Git, follow these steps:
- Install Git:
sudo yum install git -y
To allow Jenkins to interact with Docker, execute the following command:
sudo usermod -aG docker jenkins
After executing the above command, restart Jenkins:
sudo systemctl restart jenkins
The Jenkins pipeline is triggered by a webhook configured on the GitHub repository. Whenever a developer pushes changes to the repository, the pipeline is initiated, and the following steps are executed:
- The pipeline checks if there is a running Docker container for the application.
- If a container is running, the pipeline copies the updated files from the Jenkins workspace to the running container, ensuring the changes are immediately reflected.
- If no running container is found, the pipeline builds a new Docker image using the code from the GitHub repository and deploys it as a new container on the Amazon EC2 Linux machine.
- The deployed application can then be accessed on the target machine through the specified port.
To get started with this CI/CD pipeline, follow the steps below:
- Set up Jenkins, Git, and Docker on your machine.
- Provide Jenkins with a GitHub credential (token):
- Generate a GitHub personal access token with the appropriate scopes (repo, webhook, etc.).
- In Jenkins, go to "Manage Jenkins" > "Manage Credentials"> "Global credentials (unrestricted)".
- Add a new credential of type "Secret text" or "Secret file" and enter your GitHub token.
- Save the credential.
- Configure Jenkins by accessing its web interface.
- Create a new Jenkins job and configure it as follows:
- Set the job type to "Freestyle Project".
- Connect it to your GitHub repository (https://github.com/harshartz/Jenkins-project.git) and configure the webhook.
- Select "GitHub hook trigger for GITScm polling" as the build trigger.
- Add an "Execute Shell" build step to the pipeline and use the following code:
#!/bin/bash container_id=$(docker ps --filter "status=running" --format "{{.ID}}") if [ -n "$container_id" ]; then docker cp /var/lib/jenkins/workspace/devops-project/. "$container_id":/usr/share/nginx/html else docker build -t server /var/lib/jenkins/workspace/devops-project docker run -d -p 9090:80 server fi
Run the Jenkins job and verify the successful execution of the pipeline.
Application is running, and whenever a developer commits changes to the GitHub repository, it will automatically get deployed to the application.