Search code examples
node.jsgithub-actionspm2

Pm2 stops my app after github action finishes


I wrote a script deploy.sh to run as a step in the .github/deploy.yml file.

This is the part in the yaml file where I execute the shell scipt

   - name: Execute script
     uses: appleboy/ssh-action@master
     with: 
       host: ${{ secrets.HOST_DNS }}
       username: ${{ secrets.USERNAME }}
       key: ${{ secrets.EC2_SSH_KEY }}
       script: |
         cd server/
         chmod 755 deploy.sh
         ./deploy.sh

And here is the shell script

echo "Kill pm2"
sudo pm2 kill


echo "Install app dependencies"
sudo rm -rf node_modules package-lock.json
sudo npm run get

echo "Start app with pm2"
sudo pm2 start index.js -i max
sudo pm2 list

In the github action log section, I can clearly see the pm2 starting my app

out: [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
out: [PM2] PM2 Successfully daemonized
out: [PM2] Starting /home/***/server/index.js in cluster_mode (0 instance)
out: [PM2] Done.
out: ┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
out: │ id  │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
out: ├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
out: │ 0   │ index    │ default     │ 1.0.1   │ cluster │ 4807     │ 0s     │ 0    │ online    │ 0%       │ 35.1mb   │ root     │ disabled │
out: └─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
out: ┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
out: │ id  │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
out: ├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
out: │ 0   │ index    │ default     │ 1.0.1   │ cluster │ 4807     │ 0s     │ 0    │ online    │ 0%       │ 52.2mb   │ root     │ disabled │
out: └─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
==============================================
✅ Successfully executed commands to all host.
==============================================

But after that, when I SSH into the server and run pm2 list, I don't see any process running.

Any reason why this is happening? Thanks in advance.


Solution

  • So apparently, I'm using sudo to execute the command, and using another user to pm2 list, that's why I cannot see the process.