0

I have step in github action file

        uses: appleboy/ssh-action@v0.1.7
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          port: ${{ secrets.SSH_PORT }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            chown -R root /opt/project
            cd /opt/project
            yarn
            npm run pm2:start

package json file

pm2:start: /usr/local/bin/pm2 restart ecosystem.config.js --time 

also tried like pm2 restart ecosystem.config.js --time

on serve pm2 path

pm2: /usr/local/bin/pm2 /root/.nvm/versions/node/v16.19.1/bin/pm2

When I see action logs it throws an error (pm2: not found), what's wrong ?

34
err: warning " > swagger-ui-express@4.6.0" has unmet peer dependency "express@>=4.0.0".
35
err: warning Workspaces can only be enabled in private projects.
36
out: [4/4] Building fresh packages...
37
out: Done in 56.73s.
38
out: > project@1.0.0 pm2:start
39
out: > /usr/local/bin/pm2 restart ecosystem.config.js --time
40
err: sh: 1: /usr/local/bin/pm2: not found
40
2023/04/16 05:36:21 Process exited with status 127
sahil
  • 5
  • 3
  • 1
    Does this answer your question? [Github Actions pm2: command not found](https://stackoverflow.com/questions/69644460/github-actions-pm2-command-not-found) – Azeem Apr 16 '23 at 06:30
  • @Azeem No it did not when I try to run the command manually from the server it's working. But through GitHub action i tried with creating shell script, through npm command both did not worked – sahil Apr 16 '23 at 06:56
  • 1
    Well, that sounds like you're using a self-hosted runner which is missing from your question. Maybe, that's why you added `digital-ocean` tag in your question but without a `runs-on` section it's unclear. Please [edit](https://stackoverflow.com/posts/76026185/edit) your question and add all the relevant parts of your workflow in your question. Thanks! – Azeem Apr 16 '23 at 07:00
  • @Azeem Its not self-hosted, I am trying to run all commands using ssh on a digital ocean droplet. what I don't get is, it's failing only when I try to run a shell script through ssh which in turn restarts the pm2 service through GitHub actions as shown in the above script. When I manually do it works – sahil Apr 16 '23 at 07:07
  • Right, got it. One possible solution could be to add the absolute path of `pm2` to `PATH` env var to make this work. – Azeem Apr 16 '23 at 07:13
  • Here's the relevant issue: https://github.com/appleboy/ssh-action/issues/154. – Azeem Apr 16 '23 at 07:24
  • 1
    I tried @Azeem adding these two lines `export NVM_DIR=~/.nvm source ~/.nvm/nvm.sh` and it worked. Thanks for your answer. But one thing I did not get what the use of symlink `pm2: /usr/local/bin/pm2 /root/.nvm/versions/node/v16.19.1/bin/pm2`if we also need to export nvm dir ? – sahil Apr 16 '23 at 07:34
  • Good to hear that. I have been seeing multiple threads having the same issue with SSH actions. You might want take a look at this thread: https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command. You may add those paths in your profile file (if not added already) and configure your SSH server settings to source those on non-interactive logins e.g. by SSH. – Azeem Apr 16 '23 at 07:41
  • Regarding those symlinks, see https://stackoverflow.com/questions/53509740/ssh-does-not-find-usr-local-bin-path. It goes back to setting the `PATH`. – Azeem Apr 16 '23 at 07:50

1 Answers1

0

You need to add the absolute path of pm2 to PATH environment variable before invoking it.

See these relevant threads:

Azeem
  • 11,148
  • 4
  • 27
  • 40