0

I am running a github actions workflow. Here is the code:

name: Deploy to EC2

on:
  workflow_dispatch:  # Manual trigger

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Transfer code to EC2
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.EC2_HOST }}  
          username: ${{ secrets.EC2_USERNAME }}  
          key: ${{ secrets.EC2_SSH_KEY }}  
          source: .  # Copy all files from the repository root
          target: /home/ubuntu/app/next/  
      
      - name: SSH into EC2 and Run Script
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.EC2_HOST }}  
          username: ${{ secrets.EC2_USERNAME }}  
          key: ${{ secrets.EC2_SSH_KEY }}  
          script: |
            cd /home/ubuntu/app
            cd next
            npm ci 
            npm run build
            pm2 restart 0

        

And when I run the workflow its throwing this error:

======CMD======
cd /home/***/app
cd next
npm ci 
npm run build
pm2 restart 0

======END======
err: bash: line 3: npm: command not found
err: bash: line 4: npm: command not found
2023/06/13 03:31:13 Process exited with status 127
err: bash: line 5: pm2: command not found

I have globally configured npm and pm2 on my ec2 instance. But its throwing this error every time. I have tried different methos but not working

  • 1
    Does this answer your question? [Github Actions and npm - npm: command not found](https://stackoverflow.com/questions/70523431/github-actions-and-npm-npm-command-not-found) – Azeem Jun 13 '23 at 17:03
  • It does not work, I have tried commenting the following line in .bashrc: # If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac It works. Its because it prevent non-interactive shell sessions, like scripts or subshells, from executing commands that are meant for interactive use only – Akrur Neupane Jun 14 '23 at 03:37

1 Answers1

0

I have the same issue. Finally, I got the solution. The way I got around with it was to create a bash script in the root directory of my project: pm2_runner.sh, I added:

#!/bin/bash
if ! type pm2 > /dev/null
then
  sudo npm install -g pm2 && pm2 start ./index.js 
else
  pm2 restart ./index.js
fi

in pm2_runner.sh file.

Then in my .yml file inside .github/workflow/, added 2 lines which are

- run: chmod +x ./pm2_runner.sh
- run: bash ./pm2_runner.sh

Here .yml file

runs-on: ubuntu-latest

strategy:
  matrix:
    node-version: [18.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: npm run build --if-present
- run: chmod +x ./pm2_runner.sh
- run: bash ./pm2_runner.sh

Here I have added a screenshot for project structure reference.

For more info: Github Actions pm2: command not found

Dimple
  • 41
  • 4