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.


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

  1. AWS Account
  2. EC2 Instance
  3. Node.js and NPM on EC2
  4. GitHub Account
  5. GitHub Webhook
  6. IAM Role
  7. Jenkins Server & Jenkins Plugins
  8. Docker

For setup instructions on EC2 and Jenkins installation and configuration, please refer to my previous article. click here

Configure EC2 Instance

  • Go to the AWS Management Console.
  • Launch an EC2 instance with an Amazon Linux 2 or Ubuntu AMI.
  • Configure security groups to allow HTTP (port 80) and SSH (port 22) access.
  • please refer to my previous article. click here

Install Necessary Software

  • Install node js and npm on EC2
  • login inside EC2 using session manager or putty and run below command.

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

  • Create a new directory for your Node.js project.

mkdir node-docker-example
cd node-docker-example        

  • Initialize a new Node.js project.

npm init -y        

  • Create a simple index.js file for your Node.js application.

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

  • Create a file named Dockerfile in your node-docker-example directory.

# 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

  • Generate a GitHub Personal Access Token:

  1. Go to GitHub > Settings > Developer settings > Personal access tokens.
  2. Generate a new token with repo and admin:repo_hook permissions.

  • Add GitHub Credentials to Jenkins:

  1. Go to Jenkins > Manage Jenkins > Manage Credentials.
  2. Add a new credential with your GitHub personal access token.

  • Set Up GitHub Webhook:

  1. Go to your GitHub repository > Settings > Webhooks.
  2. Add a new webhook with the following details:

  • Payload URL: http://<your-ec2-instance-public-dns>:8080/github-webhook/
  • Content type: application/json
  • Select Just the push event.

Article content
Add Webhooks

Configure Jenkins Server

Jenkins Server: Ensure Jenkins is installed and running. You can access Jenkins via your browser at http://your-server-ip:8080.

Article content
Jenkins Server pager

Create a New Jenkins Job

Create a new Jenkins job, set it to pull the repository containing the Node.js application from GitHub.

  • Create Freestyle Project:

  1. From the Jenkins dashboard, click on "New Item".
  2. Enter a name for your project (e.g., "MyNodeApp").
  3. Select "Freestyle project" and click "OK".

Article content
Select new item
Article content
Add new item

  • Configure the Jenkins Job:

  1. Scroll down to the "Source Code Management" section.
  2. Select "Git".
  3. Enter the repository URL (e.g., https://github.com/your-username/your-repo.git).
  4. Choose the credentials you added for GitHub (if not, add them here).

Article content
add description and github project url
Article content
Source Code Management
Article content
Add correct branch

Set Up Build Triggers

  1. Tick "GitHub hook trigger for GITScm polling". This ensures Jenkins listens to GitHub webhook events and triggers builds accordingly.

Article content
build triggers

Add Build Steps

  1. Click "Add build step" > "Execute shell" (for example).
  2. Enter the shell commands to build and deploy your application.

docker build . -t node-js
docker run -d --name nodejs-on-ec2 -p 8000:8000 node-js        
Article content
Add build steps

Configure Post-Build Actions

  • After configuring your build steps, scroll down to the "Post-build Actions" section
  • To add a post-build action, click on the "Add post-build action" button.
  • Select the desired action from the dropdown list.
  • Configure the action parameters as needed. Each action may have specific settings or configurations depending on its functionality.

Article content
Add post-build actions

Save Your Configuration and Build

  • Click "Save".
  • Click "Build Now" to manually trigger the build for the first time.

Article content
save configuration
Article content
Build Now
Article content
See build success/failed history

Access Console Output

  • Inside the job page, you will see a menu on the left-hand side. Click on "Console Output" or "Console Output" link under the job's title.
  • The Console Output page will display real-time logs and details of the job's execution.
  • You can scroll through the console output to view build steps, commands executed, errors, warnings, and any other relevant information generated during the build process.

Article content
console output

Verify Integration

  • Make a change to your GitHub repository (e.g., commit and push).
  • Check Jenkins to ensure that it automatically triggers a build based on the GitHub webhook.

Test the Webhook

  • Make a small change to your project code and push it to the GitHub repository. Check the Jenkins dashboard to see if the job is triggered automatically. Verify that the build and deployment process completes successfully.

Article content
auto deploy log

Final output

To view the final result with a Docker container image and the actual output.

Article content
Docker image
Article content
Final 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







To view or add a comment, sign in

More articles by vishal patil

Others also viewed

Explore content categories