NodeJS application deployment on Digital Ocean using Github Actions

20 views Asked by At

I have a NodeJS application(NestJS Framework) which I am deploying on the Digital Ocean droplet using GitHub Actions.

On the droplet, I have already cloned the app, deployed it and started it with pm2 to make sure it works correctly without Github actions and it does, I can call the APIs using the droplet IP with port right now(Nginx is not set up yet)

I am a bit new to GitHub actions and now the part remains as to how I can every time I push to main, I want to update my application code with the main branch and refresh the pm2 app.

deploy.yml file

name: Varisht Prod Deployment

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: SSH into Droplet and Deploy
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.DROPLET_IP }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd varisht-backend
            git pull origin main
            npm install
            npm run build
            pm2 restart varisht-prod

So to explain a bit here as per my understanding:

  1. As all the steps need to happen in the droplet and not any local machine, I first do SSH to the droplet using appleboy/ssh-action@master
  2. I have correctly set the secrets where my DROPLET_IP is the IP address of the droplet, SSH_USERNAME is the root and SSH_PRIVATE_KEY is the private key.
  3. After that, in the script I do all the necessary step required to deploy and update the app.
  4. Also, I am not setting the Nodejs setup, as it's already there in the droplet(below SS) along with the path.

enter image description here

  1. I have workflow_dispatch so I can run the script from Git.

Now when I am running the deployment, it fails, and I get the below error. What am i missing here, npm should be there as NodeJS is already installed on the droplet. How I can debug this? Thanks!

enter image description here

0

There are 0 answers