Project-1: Automating Deployment of a Simple Web Application Using Jenkins and GitHub Webhook ๐
Day 80: #90DaysOfDevOps Challange
In this blog post, we will explore how to automate the deployment of a simple web application using Jenkins and trigger it automatically using a GitHub webhook. We'll go through the step-by-step process of setting up Jenkins, integrating it with GitHub, configuring a Jenkins job, and finally, implementing a webhook to automatically trigger deployments upon code changes. Let's dive in! ๐ป๐ง
Step 1: Launch an AWS EC2 Instance (Jenkins-server) ๐
To start, we need to set up an EC2 instance on AWS. This instance will serve as our Jenkins server. Launch the instance and ensure you have the necessary access credentials to log in.
Step 2: Install Java Development Kit (JDK) and Jenkins ๐ฆโ
Log in to your AWS EC2 instance and follow these commands to install the required dependencies:
sudo apt update
sudo apt install openjdk-11-jre
java -version
Now, let's install Jenkins by running the following commands:
curl -fsSL https://pkg.jenkins.io/debian/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 binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
Once the installation is complete, Jenkins will be active and running.
Step 3: Expose Jenkins on Port 8080 ๐
By default, Jenkins runs on port 8080. To access it from your web browser, we need to expose port 8080. Follow these steps:
Navigate to the security settings of your AWS EC2 instance.
-
Click on "Security Groups" and edit the inbound rules.
-
Add a new rule with custom TCP and port range as 8080, and set the source as your IP address.
Now, you can access Jenkins by entering your IP address followed by port 8080 in your web browser.
Step 4: Setting Up Jenkins for the Web Application Deployment ๐
Access Jenkins using the IP address and port 8080.
-
Paste this above string in Administrator password
Click on "Install suggested plugins" and set up your username and password.
-
Create a new item and select "GitHub project."
-
Enter the URL of your GitHub project and configure the source code management by selecting Git and entering the Git URL.
-
Set the branch as "main"
-
Generate SSH keys by typing "ssh-keygen" in the console.
-
Copy the public key from the
.ssh
directory. -
Navigate to your GitHub accounts settings and add a new SSH key with the copied public key.
-
Add credentials for GitHub integration, selecting the domain as "Global credentials."
-
Configure the web app by adding an "Execute shell" build step.
Save the configuration and click on "Build Now."
Step 5: Dockerizing the Application ๐ณ
- Install Docker on your system:
sudo apt-get update
sudo apt install docker.io
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
Configure the Jenkins job to use Git as the source code management.
Add the following build steps in the "Execute shell":
docker build -t my-web-app .
docker run -d -p 80:80 my-web-app
-
Save the configuration and click on "Build Now" to build the Docker image and run the container.
- Application will be running on port <ip_address>:80
Step 6: Configuring GitHub
Webhook for Automatic Deployment ๐ฃ
Install the "GitHub Integration Plugin" in Jenkins from the "Manage Jenkins" section.
-
Go to your GitHub repository settings and add a new webhook.
-
Set the payload URL as
<ip-address:8080>/github-webhook/
. Make sure port 8080 is accessible. Select the content type as "application/json."
Select "Just the push event" to trigger the webhook.
Activate the webhook.
Step 7: Configure Jenkins Job for GitHub Webhook Trigger ๐ฏ
Open the respective job configuration.
Select "Build Triggers" and enable "GitHub hook trigger for GITScm polling."
Save the configuration.
Step 8: Stopping the Previous Docker Container and Building a New One ๐ณ๐
To ensure that the previous container is stopped before building and running a new one, let's update the Jenkins job configuration with the following steps:
Add an additional "Execute shell" build step after the previous steps.
Update the shell commands to include stopping the previous container, building a new image, and running the updated container:
docker-compose down
docker-compose up -d
- Save the configuration.
Now, whenever a code change is triggered by the GitHub webhook, Jenkins will stop the previous container, build a new Docker image, and run the updated container with the latest changes.
GitHub Link to the Project ๐
To explore the code and delve deeper into the project, you can access the GitHub repository below:
Feel free to explore the codebase, collaborate, and contribute to this exciting project!
Conclusion ๐
With the added steps to stop the previous container and update the Docker image, Jenkins job is now equipped to handle continuous deployments seamlessly. Each code change will trigger Jenkins to stop the previous container, build a new image, and run the updated container, ensuring your web application always reflects the latest changes. Enjoy the automated and up-to-date deployments! ๐๐