How to use jenkins to restart PM2?
Asked Answered
L

3

7

i'm running my node.js app on the linux server using PM2, with a config file, like this:

PM2 start mywebsite.config.js

all is good. but now i want to add jenkins to the picture. i'm running a pipeline project in jenkins, using Jenkinsfile. All working fine except for the last command, that should restart the app, to make the new version live:

stage('Restart PM2') {  
  steps {
    sh 'pm2 restart all'  }
  }
}

and this command fails. here is the log output:

+ pm2 restart all 
Use --update-env to update environment variables 
[PM2][WARN] No process found 
< empty pm2 log table here> 
Use `pm2 show <id|name>` to get more details about an app

I understand that PM2 is working per user. means, that the user who ran the first command (start) is the one that should run the restart as well. but how to do this?

Littell answered 16/6, 2019 at 4:35 Comment(3)
wrapp your app with docker container . very simple with pm2 . it will solve your permission issuesRaffin
Here is a workaround to run pm2 in Jenkins Fix Jenkins pm2Sankhya
Here is a workaround to run pm2 in Jenkins Fix pm2 JenskinsSankhya
O
4

Instead of restarting PM2 through you jenkins code, let PM2 do it by itself, using the watch flag. in your config file, set watch to be true. You may want to add a relatively new flag called watch-ignore. that's an array, with files to be ignored by the watch. add your log file and error file to this list. otherwise, any logged information will cause your node app to restart endlessly. after doing these changes to the config file, run pm2 again with the config. remove the restarting code from Jenkinsfile, you don't need that anymore, pm2 will detect the new version and will reload the app!

Owens answered 19/6, 2019 at 15:20 Comment(1)
Is there another way that Jenkins can restart it itself so it can be tracked within the pipeline ?Ultraviolet
L
6

To run pm2 restart all from Jenkins you need to:

  1. Configure your system to run sudo from jenkins (https://sgoyal.net/2016/11/18/run-a-shell-from-jenkins-using-sudo-ubuntu/)

  2. Make a symbolic link to the .pm2/ folder, in my case(Ubuntu) it was at /root/.pm2 so i run

sudo ln -s /root/.pm2/ /var/lib/jenkins/

NOTE: /var/lib/jenkins if the default jenkins root directory, you can check yours on Jenkins configuration

  1. after that you can go to jenkins and setup a shell command, in my case i did:

#!/bin/sh echo "RESTARTING ALL" sudo pm2 restart all echo "ALL RESTARTED"

NOTE: if you have a .pm2 folder already in your jenkins root directory rename it so you can do the symbolic link

Hope this helps

Lahomalahore answered 4/3, 2020 at 0:38 Comment(0)
O
4

Instead of restarting PM2 through you jenkins code, let PM2 do it by itself, using the watch flag. in your config file, set watch to be true. You may want to add a relatively new flag called watch-ignore. that's an array, with files to be ignored by the watch. add your log file and error file to this list. otherwise, any logged information will cause your node app to restart endlessly. after doing these changes to the config file, run pm2 again with the config. remove the restarting code from Jenkinsfile, you don't need that anymore, pm2 will detect the new version and will reload the app!

Owens answered 19/6, 2019 at 15:20 Comment(1)
Is there another way that Jenkins can restart it itself so it can be tracked within the pipeline ?Ultraviolet
P
2
BUILD_ID=dontKillMe PM2 start mywebsite.config.js

Jenkins kills the pm2 daemon to be created by the build. You should put the keyword to prevent killing daemon by Jenkins.

Pellmell answered 31/8, 2021 at 6:3 Comment(1)
This is super important. wiki.jenkins.io/display/JENKINS/ProcessTreeKiller. Thank you for pointing this out! I was running pm2 start from Jenkins and the process was receiving a SIGTERM when it started each time, so it always started with an immediate restart. This fixed it!Pachton

© 2022 - 2024 — McMap. All rights reserved.