3

I have simple ec2 server in which I have added GitHub actions for CI/CD all the things are working fine but issue is when I change something in existing end points or do anything in existing code on server my code is not reflecting I can see there is green tick on my commit and code is pushed to my server but its not reflecting. And If I do pm2 restart index.js manually on server then every things is working fine. Also If I create new route or something then it's reflecting I think there is some issue in my workflow file ? But I am not getting it why it's happening.

name:  Ci/Cd Pipeline 

on:
  push:
    branches: [ "staging" ]
  pull_request:
    branches: [ "staging" ] 

jobs:
  build:

    runs-on: self-hosted
    
    strategy:
      matrix: 
        node-version: [18.x]
        
    steps:
    - name: cleanup #https://github.com/actions/checkout/issues/21
      run: |
        sudo chown -R $USER:$USER $GITHUB_WORKSPACE
    - uses: actions/checkout@v3
    - run: sudo npm i 
    - run: sudo pm2 kill -a
    - run: sudo pm2 start index.js
rameez khan
  • 73
  • 1
  • 23
  • 68

1 Answers1

0

If I do pm2 restart index.js

That is not the same as sudo pm2 ...: it will use your ~/.bashrc and ~/.profile environment settings, as opposed to /root.

This answer, for instance, suggests a - run: pm2 restart my_project_name in the GitHub Action (or a pm2_runner.sh to install pm2 if not installed first).
But the best practice is to stop/start without using sudo, in order to not have to deal with files created as root.
The PM2 Quick Start page does not show any usage of sudo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250