CI/CD Pipeline for Node JS Application using GitHub Action
Table of contents
๐ Introduction:
Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development. They help streamline the development process, reduce errors, and ensure that your application is always up-to-date. In this blog, we'll walk you through setting up a CI/CD pipeline for a Node.js application using GitHub Actions. We'll cover everything from creating a simple Node.js application to deploying it using GitHub Actions.
๐ Pre-requisites:
A GitHub account
Basic knowledge of Node.js and npm
๐ Step 1: Create a Node.js application
First, let's create a simple Node.js application. Create a new directory for your project and navigate to it:
mkdir my-node-app
cd my-node-app
Initialize a new Node.js project with the following command:
npm init -y
This command generates a package.json file with default values.
Create a new file called index.js and add the following content:
console.log('Hello, Node.js!');
๐ Step 2: Initialize a Git repository
Initialize a Git repository in your project directory:
git init
Create a .gitignore file to exclude unnecessary files from the repository:
echo "node_modules/" > .gitignore
Commit the initial project files:
git add .
git commit -m "Initial commit"
๐ Step 3: Create a GitHub repository
Create a new repository on GitHub and push your local repository to it:
git remote add origin https://github.com/your-username/my-node-app.git
git branch -M main
git push -u origin main
๐ Step 4: Set up GitHub Actions
Create a new directory called .github/workflows in your project:
mkdir -p .github/workflows
Create a new file called ci-cd.yml inside the .github/workflows directory:
touch .github/workflows/ci-cd.yml
Open the ci-cd.yml file and add the following content:
name: Node.js CI/CD
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Cache npm dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-node
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Deploy
if: github.ref == 'refs/heads/main'
run: echo "Deploy your application here"
This GitHub Actions workflow does the following:
Triggers on push and pull request events to the main branch
Checks out the repository
Sets up Node.js
Caches npm dependencies for faster builds
Installs dependencies using npm ci
Runs tests (add your test script to package.json)
Deploys the application (replace the echo command with your deployment script)
๐ Step 5: Commit and push the GitHub Actions workflow
Add the new GitHub Actions workflow to your repository:
git add .github/workflows/ci-cd.yml
git commit -m "Add GitHub Actions CI/CD workflow"
git push
Now, whenever you push changes to your main branch or create a pull request, the GitHub Actions workflow will automatically build, test, and deploy your Node.js application.
๐ Conclusion:
In this blog, we've demonstrated how to set up a CI/CD pipeline for a Node.js application using GitHub Actions. By following these steps, you can automate the build, test, and deployment processes for your Node.js projects, ensuring that your application is always up-to-date and error-free. Happy coding!
๐น Checkout GitHub Repository for projects:
๐ github.com/sumanprasad007