4

I am trying to auto-deploy the project and npm commands run perfectly but when it goes to a line where pm2 restarts the specific projects, then actions fails.

GitHub Actions Error:

GitHub Actions Error

GitHub Action .yml file content:

yml file

5 Answers5

15

NOTE: This solution is applicable only when you are using an NVM to manage node.js versions

The issue is because of the missing symbolic link for the node and the pm2, here are the commands that you can use to create a symbolic link:

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/pm2" "/usr/local/bin/pm2"
Kishor Patidar
  • 623
  • 12
  • 23
2

This is because pm2 is not linked, for linking

Access your server via ssh, after that get the path of pm2 by

whereis pm2

You will get the output of your path like pm2: /home/abhimanyu/.nvm/versions/node/v18.12.1/bin/pm2

enter image description here

Copy the path

link your path now

sudo ln -s "$NVM_DIR/versions/node/v18.12.1/bin/pm2" "/usr/local/bin/pm2"

versions/node/v18.12.1/bin/pm2 is my path your path may be diffrent

  • Thanks, I can confirm this solution works and this is needed for those who installed pm2 as non-root or non-sudo users. – calvert Feb 08 '23 at 19:41
1

I run into the same issue. So basically, pm2 is not installed. I found it a bit strange as in my VPS, the pm2 is installed and is running perfectly. The way I got around with it was to create a bash script in the root directory of my project: pm2_runner.sh, within this file I added:

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

Then in my .yml file inside .github/workflow/, instead of writing:

- run: pm2 restart my_project_name

I added:

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

It installed pm2 globally without requesting sudo passord since sudo in github actions runs using passwordless.

1

lo que pasa es que seguro instalaste todo por NVM lo cual no esta mal, pero para que puedas utilizar pm2 dentro del action de github, lo que tienes que realizar es.

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/pm2" "/usr/local/bin/pm2"

despues de la intalacion para que asi puedas utilizar con sudo el pm2 y de esa forma te funcionara sin problemas

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 26 '22 at 09:10
0

I solved this by adding pm2 to the $GITHUB_PATH

First, create a test github action to log the user configured for using the runner

- run: |
  echo $USER
  echo $PATH

Login to your server as the user configured for the runner and run this command

which pm2

Copy the output and add it to your github action

- name: Add directory to PATH
  run: echo "/path/to/.nvm/versions/node/v14.21.3/bin" >> $GITHUB_PATH

Shown here in the github docs

abo
  • 76
  • 4