Configuring a Jenkins-GitHub CI/CD Pipeline for Node.js application Deployment on AWS EC2.
Configuring a Jenkins-GitHub CI/CD Pipeline for Node.js application deployment on AWS EC2 ensures a streamlined, automated process for continuous integration and delivery. This setup provides several key benefits:
1. Automated Builds: Jenkins automatically builds the Node.js application whenever changes are pushed to the GitHub repository, ensuring that the latest code is always tested and ready for deployment.
2. Continuous Integration: Integrating code frequently helps detect issues early, reducing integration problems and improving the quality of the software.
3. Automated Testing: Running automated tests on each build helps catch bugs early in the development cycle, improving the overall stability and reliability of the application.
4. Seamless Deployment: Automating the deployment process to AWS EC2 ensures that new features and fixes are deployed quickly and consistently, reducing downtime and manual errors.
5. Scalability: Using AWS EC2 allows for scalable and flexible infrastructure management, making it easy to handle varying loads and ensuring high availability of the application.
6. Efficient Resource Management: Automating the CI/CD pipeline helps optimize resource usage by ensuring that builds and deployments are only triggered when necessary, reducing the need for manual interventions.
Overall, this setup enhances development efficiency, accelerates delivery cycles, and ensures high-quality releases, making it essential for modern application development and deployment practices.
Prerequisites
For setup instructions on EC2 and Jenkins installation and configuration, please refer to my previous article. click here
Configure EC2 Instance
Install Necessary Software
sudo apt-get install -y nodejs npm # For Ubuntu
sudo yum install -y nodejs npm # For Amazon Linux
Install docker
To install Docker on your system, follow these steps depending on your operating system:
sudo apt-get update
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
sudo systemctl start docker
sudo systemctl status docker
You should see the status of Docker indicating that it's active and running.
Create Node js application
mkdir node-docker-example
cd node-docker-example
npm init -y
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello from Dockerized Node.js App!\n');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
Create a Dockerfile
# Use the official Node.js image.
FROM node:14
# Create and change to the app directory.
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
COPY package*.json ./
# Install production dependencies.
RUN npm install --production
# Copy local code to the container image.
COPY . .
# Run the web service on container startup.
CMD [ "node", "index.js" ]
# Expose the port the app runs on
EXPOSE 3000
Push to GitHub
Go to GitHub and create a new repository named node-docker-example. Do not initialize with a README, .gitignore, or license, as you have already done this locally. use below commands for push code in github.
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/your-username/node-docker-example.git
git push -u origin master
Enter your GitHub credentials if prompted.
Verify on GitHub
Go to your GitHub repository (https://github.com/your-username/node-docker-example) and verify that your code has been pushed successfully.
Configure GitHub Webhook and Jenkins
Configure GitHub Webhook
Configure Jenkins Server
Jenkins Server: Ensure Jenkins is installed and running. You can access Jenkins via your browser at http://your-server-ip:8080.
Recommended by LinkedIn
Create a New Jenkins Job
Create a new Jenkins job, set it to pull the repository containing the Node.js application from GitHub.
Set Up Build Triggers
Add Build Steps
docker build . -t node-js
docker run -d --name nodejs-on-ec2 -p 8000:8000 node-js
Configure Post-Build Actions
Save Your Configuration and Build
Access Console Output
Verify Integration
Test the Webhook
Final output
To view the final result with a Docker container image and the actual output.
Integrating Jenkins with GitHub allows for automated builds and deployments whenever code changes are pushed to the repository. This setup streamlines the CI/CD process, ensuring that your application is continuously tested and deployed with each update. Adjust configurations and plugins as per your project's specific requirements and workflows.
By following above steps, you can establish a streamlined Jenkins-GitHub CI/CD pipeline to deploy your Node.js applications on AWS EC2, enhancing both development efficiency and deployment reliability. Embrace automation and continuous integration to propel your projects forward seamlessly. Happy coding! 🚀
#DevOps #CI/CD #NodeJS #AWS#ContinuousIntegration, #ContinuousDeployment, #Jenkins, #GitHub, #Docker, #EC2, #Automation, #SoftwareDevelopment, #CloudComputing, #Containerization, #DeploymentPipeline
Insightful