Deployment of NodeJs using CICD pipeline with github action and NGINX in EC2 in AWS

Deployment of NodeJs using CICD pipeline with github action and NGINX in EC2 in AWS

Let's break down the process of deploying a Node.js application using a CI/CD pipeline with GitHub Actions on an EC2 instance in AWS:

  1. CI/CD Pipeline: CI stands for Continuous Integration, and CD stands for Continuous Deployment (or Continuous Delivery). A CI/CD pipeline automates the steps involved in building, testing, and deploying your application. It ensures that changes made to your codebase are automatically validated and deployed in a consistent and reliable manner.

  2. GitHub Actions: GitHub Actions is a feature provided by GitHub that allows you to automate workflows directly within your GitHub repository. You can define workflows using YAML files to specify the steps to run when certain events occur, such as pushing code changes, creating pull requests, or tagging releases.

  3. Node.js Application: This refers to your Node.js application that you want to deploy. It could be any type of application, such as a web server, API, or any other type of Node.js application.

  4. EC2 Instance: Amazon EC2 (Elastic Compute Cloud) is a web service provided by AWS that allows you to launch virtual servers (instances) in the cloud. You can choose the operating system, configure the server, and deploy your applications on these instances. In this case, you'll launch an EC2 instance to host your Node.js application.

Now, let's go through the steps involved in deploying a Node.js application using a CI/CD pipeline with GitHub Actions on an EC2 instance:

  1. Set up EC2 Instance: Launch an EC2 instance on AWS. You'll need to choose the appropriate instance type, configure security groups, and optionally set up a key pair for SSH access.

  2. Prepare Node.js Application: Make sure your Node.js application is set up and ready to be deployed. This includes writing code, setting up dependencies, and configuring any necessary environment variables.

  3. Create GitHub Actions Workflow: In your GitHub repository, create a .github/workflows directory if it doesn't already exist. Inside this directory, create a YAML file (e.g., deploy.yml) to define your GitHub Actions workflow. This workflow will include steps to build your application, run tests, and deploy it to the EC2 instance.

  4. Define Workflow Steps: Within your GitHub Actions workflow file, define the steps to execute during the workflow. These steps might include installing dependencies, running tests, building the application, and deploying it to the EC2 instance. You'll likely need to use SSH or a deployment tool (such as AWS CodeDeploy) to transfer files to the EC2 instance and start the application.

  5. Configure AWS Credentials: To deploy your application to the EC2 instance from GitHub Actions, you'll need to configure AWS credentials with the necessary permissions. You can use GitHub Secrets to securely store these credentials and access them within your workflow.

  6. Trigger Workflow: Push your code changes to GitHub to trigger the GitHub Actions workflow. The workflow will automatically run based on the triggers defined in the workflow file.

  7. Monitor Deployment: Monitor the progress of the deployment in the GitHub Actions workflow logs. If there are any errors or failures, you'll need to troubleshoot and fix them accordingly.

  8. Verify Deployment: Once the deployment is complete, verify that your Node.js application is successfully running on the EC2 instance. You can access the instance using its public IP address or domain name(if you have).

Steps

step1: Create the new node js application

npm init
npm install express

step2: Create the index.js

// Import the express module
const express = require('express');

const app = express();

app.get('/', (req, res) => {
    res.send('Hello, World! Welcome........'); // Send a response to the client
});

const port = 4000;

// Start the server and listen on the specified port
app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});

step 3: Then commit to the github

step 4: Now setup the github runner

step 4 : Launch EC2 instance

step 5. Configure security group like this port 4000 for backend running.

# run the following 
sudo apt update
sudo su -
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt install -y nodejs
node -v 
npm -v
npm install -g npm@10.4.0
npm install pm2@latest -g
sudo apt install nginx
# For more https://hashnode.com/post/clsvmlxrp000009jn6jdj2suy

step 6. Copy the command from github action shown in the step 4 above as follows

# important to consider
sudo ./svc.sh install
sudo ./svc.sh start

step 7. Check the github runner, you will see the idle state

step 8. Now configure the GitHub Actions for Node js

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
  push:
    branches: [ "main" ]

jobs:
  build:

    runs-on: self-hosted

    strategy:
      matrix:
        node-version: [21.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci

step 8. Then check the following actions in code

step 9. Now you will see the _work folder and run the following commands

step 10. Configure the NGINX server

# check the NGINX installed or not
cd etc/nginx/sites-enabled
sudo rm default
sudo nano myapp

make sure that your backend is running on port 4000 then

sudo service nginx restart

step 11. Now open the server of the nginx you will get the sucess result

step 12. Now make some changes in the code. Add /products api and commit


# make sure that 
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
  push:
    branches: [ "main" ]

jobs:
  build:

    runs-on: self-hosted

    strategy:
      matrix:
        node-version: [21.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: pm2 restart index
   #Note update the last line in the node.js.yaml

step 13. Now make some changes in code

# Before making changes run 
git pull
# and then commit

Finally, the new changes reflect automatically in the result

Github project used in this article :- https://github.com/SahadevDahit/cicd-githubAction

Thanks for reading.........................................