Day 80 of 90 Days of DevOps Challenge: Automating Web Application Deployment with Jenkins and GitHub
In today’s fast-paced development environment, the need for automation in the software delivery process has become more critical than ever. In this blog, we will explore how to automate the building, testing, and deployment process of a web application using Jenkins and GitHub. By leveraging Jenkins' powerful CI/CD capabilities alongside GitHub's version control features, we can create a seamless workflow that enhances productivity and reduces the time to market.
Project Overview
The primary goal of this project is to set up an automated CI/CD pipeline that integrates with GitHub. This pipeline will be triggered automatically whenever changes are made to the code repository, allowing for a smooth and efficient development process. The stages of our pipeline will include building the application, running tests, and deploying the application to a production environment. Additionally, we will implement notifications and alerts to keep the development team informed of any issues during the build and deployment processes.
Key Components
1. Jenkins
Jenkins is an open-source automation server that enables developers to build, test, and deploy their applications easily. It supports numerous plugins and integrations, making it a powerful tool for continuous integration and continuous delivery (CI/CD).
2. GitHub
GitHub is a web-based platform for version control using Git. It provides collaborative features that allow teams to work together on projects. By integrating GitHub with Jenkins, we can automate the triggering of builds and deployments based on changes made to the codebase.
3. Webhook Integration
Webhooks are HTTP callbacks that are triggered by events in GitHub, such as pushes to a repository. When changes are made to the codebase, a webhook will notify Jenkins to start the pipeline, ensuring that our application is always up to date.
Implementation Steps
Task-01: Set Up GitHub Integration and Webhooks
1. Fork the Repository
Begin by forking this repository for the Node.js application.
This will create a copy of the project in your GitHub account that you can modify and integrate with Jenkins.
2. Create a Jenkins Job
Create a New Job:
Open Jenkins and create a new job by selecting "New Item."
Choose "Freestyle project" and give it a name relevant to your Node.js application.
Configure Source Code Management:
In the job configuration, go to the "Source Code Management" section.
Select "Git" and enter the URL of your forked repository.
Provide credentials if required to access the repository.
3. Integrate GitHub Webhooks
Configure GitHub Webhooks:
Go to your GitHub repository settings.
Navigate to "Webhooks" and click "Add webhook."
Set the Payload URL to your Jenkins server URL followed by
/github-webhook/
(e.g.,http://your-jenkins-url/github-webhook/
).Choose the content type as
application/json
.Select the events that will trigger the webhook. Typically, you’ll select "Just the push event."
Save the webhook configuration.
Configure GitHub Plugin in Jenkins:
In Jenkins, go to the job configuration and scroll to the "Build Triggers" section.
Check the "GitHub hook trigger for GITScm polling" option.
This allows Jenkins to be notified of changes in the GitHub repository and automatically trigger builds.
Task-02: Set Up Docker Compose and Run the Project
1. Create a Docker Compose File
Define the Docker Compose File:
Create a
docker-compose.yml
file in your project’s root directory. This file defines the services required for your Node.js application.Here’s a basic example of a Docker Compose file for a Node.js app:
version: '3'
services:
web:
image: "trainwithshubham/node-app-batch-6:latest"
working_dir: /app
volumes:
- .:/app
ports:
- "5000:5000"
command: npm start
2. Update Jenkins Job to Use Docker Compose
Configure Build Steps:
In your Jenkins job configuration, go to the "Build" section.
Add a build step to execute the shell command:
docker-compose up -d
Clean Up After Build:
- Optionally, add a post-build step to clean up resources:
docker-compose down
3. Run the Project and Celebrate
Trigger a Build:
Manually trigger a build in Jenkins or push a change to your GitHub repository to automatically trigger the pipeline.
Jenkins will use Docker Compose to build and run your Node.js application.
4. Verify the build
Trigger a Build:
Go to appropriate port of your locanhost and verify that the application is running.
Conclusion
By automating the building, testing, and deployment processes of your web application using Jenkins and GitHub, you can significantly streamline your development workflow. The integration of GitHub webhooks with Jenkins allows for real-time monitoring of changes, ensuring that your application remains robust and up to date. This not only improves team productivity but also enhances the overall quality of your software delivery process.