Project-1: Automating Deployment of a Simple Web Application Using Jenkins and GitHub Webhook ๐Ÿš€

Project-1: Automating Deployment of a Simple Web Application Using Jenkins and GitHub Webhook ๐Ÿš€

Day 80: #90DaysOfDevOps Challange

ยท

5 min read

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:

  1. Navigate to the security settings of your AWS EC2 instance.

  2. Click on "Security Groups" and edit the inbound rules.

  3. 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 ๐Ÿš€

  1. Access Jenkins using the IP address and port 8080.

  2. Paste this above string in Administrator password

  3. Click on "Install suggested plugins" and set up your username and password.

  4. Create a new item and select "GitHub project."

  5. Enter the URL of your GitHub project and configure the source code management by selecting Git and entering the Git URL.

  6. Set the branch as "main"

  7. Generate SSH keys by typing "ssh-keygen" in the console.

  8. Copy the public key from the .ssh directory.

  9. Navigate to your GitHub accounts settings and add a new SSH key with the copied public key.

  10. Add credentials for GitHub integration, selecting the domain as "Global credentials."

  11. Configure the web app by adding an "Execute shell" build step.

  12. Save the configuration and click on "Build Now."

Step 5: Dockerizing the Application ๐Ÿณ

  1. Install Docker on your system:
sudo apt-get update
sudo apt install docker.io
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
  1. Configure the Jenkins job to use Git as the source code management.

  2. Add the following build steps in the "Execute shell":

docker build -t my-web-app .
docker run -d -p 80:80 my-web-app
  1. Save the configuration and click on "Build Now" to build the Docker image and run the container.

  1. Application will be running on port <ip_address>:80

Step 6: Configuring GitHub

Webhook for Automatic Deployment ๐ŸŽฃ

  1. Install the "GitHub Integration Plugin" in Jenkins from the "Manage Jenkins" section.

  2. Go to your GitHub repository settings and add a new webhook.

  3. Set the payload URL as <ip-address:8080>/github-webhook/. Make sure port 8080 is accessible.

  4. Select the content type as "application/json."

  5. Select "Just the push event" to trigger the webhook.

  6. Activate the webhook.

Step 7: Configure Jenkins Job for GitHub Webhook Trigger ๐ŸŽฏ

  1. Open the respective job configuration.

  2. Select "Build Triggers" and enable "GitHub hook trigger for GITScm polling."

  3. 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:

  1. Add an additional "Execute shell" build step after the previous steps.

  2. 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
  1. 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:

Link to my GitHub Project

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! ๐Ÿš€๐ŸŽ‰

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

ย