Automate Node.js Deployment on a VPS Using GitHub Actions

Automate Node.js Deployment on a VPS Using GitHub Actions

In today's fast-paced development environment, automating your Node.js deployment process is crucial. One effective way to achieve this for your Node.js application is by using GitHub Actions.

In this article, we'll walk you through setting up a GitHub Actions workflow to automate the deployment of your Node.js application to a Virtual Private Server (VPS).

Why Automate Node.js Deployment?

Automating Node.js deployment has several benefits:

  • Consistency: Ensures the same steps are followed every time you deploy, reducing human error.
  • Speed: Saves time by eliminating manual steps.
  • Reliability: Automated tests and checks can catch issues before they reach production.

Prerequisites

Before we start, make sure you have the following:

  1. A Node.js application hosted in a GitHub repository.
  2. A VPS with SSH access.
  3. A PM2 process manager installed on your VPS to manage your Node.js application. Learn more about PM2 in the PM2 Process Manager Documentation.

Setting Up GitHub Actions

GitHub Actions allows you to automate workflows directly from your GitHub repository. Here’s a step-by-step guide to creating a workflow for deploying your Node.js application.

Step 1 : Create Actions

Navigate to your Git repository, then go to the Actions tab. Select the Node.js workflow and click on "Configure." This will generate a YAML file for you with some pre-filled commands.

create-github-actions

In the generated YAML file, you'll need to make a few changes. Set the runs-on to self-hosted. Remove the on: pull_request command, as we only want this action to run on a push to the master branch. Update the Node.js version as needed, and you can skip the npm test command if it's not required. Here's our modified version:

name: Node.js CI

on:
  push:
    branches: [ "master" ]

jobs:
  build:

    runs-on: self-hosted

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

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

Ensure you use the same Node.js version that you used for developing your Node.js application. You can verify this by running the node --version command.

After making these changes, click on the "Commit changes" button to save them.

For more details on configuring workflows, visit the GitHub Actions Documentation.

Step 2 : Setup new self hosted runner

Next, navigate to Settings and select Runners from the Actions tab. Click on the "New self-hosted runner" button

Self-hosted-runner

Choose your VPS operating system and architecture, in our case, it's Linux (Ubuntu).

add-new-self-hosted-runner

Now, log in to your VPS using SSH and run all the commands from the Download tab.

Next, execute the configuration command from the Configure tab. It will ask some questions; simply press Enter to keep the default settings.

Finally, run the following command to install the svc:

sudo ./svc.sh install

Now our svc is installed, so now to make it run use below command:

sudo ./svc.sh start

It will start our server.

Step 3 : Start our project:

  Now that our server has started, we need to launch our project using PM2. Navigate to your project directory, which will be located in the _work folder, and start it with your PM2 command.  

pm2 start server.js --name="admin"

If you not installed nodejs or pm2 on your VPS you can follow this guide: Deploy Your Node.js Application on AWS EC2 Server Like a Pro

Step 4: Test Your Workflow

After setting up your workflow and secrets, push a change to the master branch to trigger the workflow. GitHub Actions will run the workflow, installing dependencies, creating the config.env file, and restarting your application using PM2.

Step 5: Monitor and Debug

You can monitor the progress and output of your workflow in the Actions tab of your GitHub repository. If the workflow fails, the logs will help you debug the issue.

Additional : Configure Secrets for Secure Node.js Deployment

In the workflow file, sensitive information such as database credentials and API keys are stored in GitHub Secrets. To set these up:

  1. Go to your GitHub repository.
  2. Click on Settings.
  3. Select Secrets in the sidebar, then click New repository secret.
  4. Add the necessary secrets such as DATABASE, DATABASE_PASSWORD, etc.

Final GitHub Actions Workflow File for Node.js Deployment

Here's the final version of your GitHub Actions workflow file:

name: Node.js CI

on:
  push:
    branches: ["master"]

jobs:
  build:
    runs-on: self-hosted

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

    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
      - run: npm ci --legacy-peer-deps
      - run: npm run build --if-present
      # Create config.env file from secrets
      - name: Create config.env
        run: |
          echo "DATABASE=${{ secrets.DATABASE }}" >> ./server/config.env
          echo "NODE_ENV=${{ secrets.NODE_ENV }}" >> ./server/config.env
      - run: pm2 restart admin

Conclusion

Automating the deployment of your Node.js application using GitHub Actions can save you time and reduce the risk of errors. By following the steps outlined in this guide, you can set up a reliable CI/CD pipeline that ensures your application is consistently and efficiently deployed to your VPS.

By leveraging GitHub Actions for Node.js deployment automation, you can streamline your deployment process, focus more on development, and less on manual deployment tasks.

Happy coding!😇

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment