DEV Community

Pranav Bakare
Pranav Bakare

Posted on

CI/CD pipeline using GitHub Actions

Here’s how you can set up a simple CI/CD pipeline for a Node.js project using GitHub Actions on Windows.

Prerequisites

  1. GitHub account.
  2. Node.js and npm installed.
  3. Git installed on your system.
  4. GitHub repository for your Node.js project.

Step 1: Create a Node.js Project

  1. Create a new folder for your project:
   mkdir my-node-app
   cd my-node-app
   npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. Install any dependencies, such as Express (for a basic server):
   npm install express --save
Enter fullscreen mode Exit fullscreen mode
  1. Create a basic server.js:
   const express = require('express');
   const app = express();

   app.get('/', (req, res) => {
     res.send('Hello, CI/CD Pipeline with GitHub Actions!');
   });

   app.listen(3000, () => {
     console.log('Server is running on port 3000');
   });
Enter fullscreen mode Exit fullscreen mode
  1. Define a test script in package.json (we'll add proper tests later):
   "scripts": {
     "start": "node server.js",
     "test": "echo \"No test specified\" && exit 0"
   }
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up GitHub Repository

  1. Push your Node.js project to GitHub:
   git init
   git remote add origin https://github.com/yourusername/your-node-app.git
   git add .
   git commit -m "Initial commit"
   git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up GitHub Actions

  1. Go to your GitHub repository and click on the Actions tab.
  2. Click Set up a workflow yourself or choose the Node.js workflow template.
  3. Create a new file named .github/workflows/nodejs.yml in your repository with the following content:
   name: Node.js CI

   on:
     push:
       branches:
         - master
     pull_request:
       branches:
         - master

   jobs:
     build:
       runs-on: ubuntu-latest

       strategy:
         matrix:
           node-version: [14, 16]

       steps:
         - name: Checkout repository
           uses: actions/checkout@v3

         - name: Setup Node.js
           uses: actions/setup-node@v3
           with:
             node-version: ${{ matrix.node-version }}

         - name: Install dependencies
           run: npm install

         - name: Run tests
           run: npm test
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • on: Triggers the workflow when code is pushed or a pull request is made to the master branch.
  • jobs: Defines the build job.
    • runs-on: Uses an Ubuntu machine for the job.
    • strategy.matrix.node-version: Runs the job for multiple versions of Node.js (14 and 16 in this case).
    • steps: Defines the steps for the pipeline:
    • Checkout the repository.
    • Set up Node.js using the specified versions.
    • Install dependencies by running npm install.
    • Run tests by executing npm test.

Step 4: Commit and Push Changes

Commit the new .github/workflows/nodejs.yml file and push it to GitHub:

git add .github/workflows/nodejs.yml
git commit -m "Add GitHub Actions workflow"
git push origin master
Enter fullscreen mode Exit fullscreen mode

Step 5: View the Pipeline on GitHub

  1. Go to your GitHub repository.
  2. Click on the Actions tab, and you’ll see the pipeline running.
  3. It will test the code and install dependencies on Node.js versions 14 and 16.

Step 6: (Optional) Add Tests

To make the pipeline more useful, you can add actual tests using Mocha or Jest.

Example with Mocha:

  1. Install Mocha and Chai for testing:
   npm install mocha chai --save-dev
Enter fullscreen mode Exit fullscreen mode
  1. Create a test folder with a test.js file:
   const chai = require('chai');
   const expect = chai.expect;

   describe('Sample Test', () => {
     it('should return true', () => {
       expect(true).to.be.true;
     });
   });
Enter fullscreen mode Exit fullscreen mode
  1. Update the test script in package.json:
   "scripts": {
     "start": "node server.js",
     "test": "mocha"
   }
Enter fullscreen mode Exit fullscreen mode

Now, when the pipeline runs, it will execute the test suite using Mocha.

Step 7: (Optional) Deployment to Heroku (Simple Example)

You can automate deployment to services like Heroku in the deploy step after your tests pass.

To deploy to Heroku, follow these steps:

  1. Install the Heroku CLI and log in:
   npm install -g heroku
   heroku login
Enter fullscreen mode Exit fullscreen mode
  1. In your workflow, add a deployment step after the tests:
   - name: Deploy to Heroku
     run: |
       heroku git:remote -a your-heroku-app
       git push heroku master
     env:
       HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
Enter fullscreen mode Exit fullscreen mode

You’ll need to add your Heroku API Key to your GitHub repository’s secrets. Go to Settings > Secrets > Actions and add the HEROKU_API_KEY.

Conclusion

You’ve now set up a simple CI/CD pipeline using GitHub Actions for a Node.js app. This workflow installs dependencies, runs tests, and can be extended to deploy your app to platforms like Heroku, AWS, or Netlify.

Top comments (0)