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 Action .yml file content:
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 Action .yml file content:
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"
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
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
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.
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
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