简体   繁体   English

如何使用jenkins重启PM2?

[英]How to use jenkins to restart PM2?

i'm running my node.js app on the linux server using PM2, with a config file, like this:我正在使用 PM2 在 linux 服务器上运行我的 node.js 应用程序,并带有一个配置文件,如下所示:

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.我正在 jenkins 中运行一个管道项目,使用 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.我知道 PM2 正在为每个用户工作。 means, that the user who ran the first command (start) is the one that should run the restart as well.意味着,运行第一个命令 (start) 的用户也应该运行重启。 but how to do this?但如何做到这一点?

Instead of restarting PM2 through you jenkins code, let PM2 do it by itself, using the watch flag.与其通过 jenkins 代码重新启动 PM2,不如让 PM2 使用 watch 标志自行完成。 in your config file, set watch to be true.在您的配置文件中,将 watch 设置为 true。 You may want to add a relatively new flag called watch-ignore.您可能想要添加一个名为 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.对配置文件进行这些更改后,再次使用配置运行 pm2。 remove the restarting code from Jenkinsfile, you don't need that anymore, pm2 will detect the new version and will reload the app!从 Jenkinsfile 中删除重新启动代码,您不再需要它了,pm2 将检测到新版本并重新加载应用程序!

To run pm2 restart all from Jenkins you need to:要从 Jenkins 运行pm2 restart all ,您需要:

  1. Configure your system to run sudo from jenkins ( https://sgoyal.net/2016/11/18/run-a-shell-from-jenkins-using-sudo-ubuntu/ )将您的系统配置为从 jenkins 运行 sudo ( 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建立一个指向 .pm2/ 文件夹的符号链接,在我的情况下(Ubuntu)它位于 /root/.pm2 所以我运行

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注意:/var/lib/jenkins 如果是默认的 jenkins 根目录,您可以在 Jenkins 配置中检查您的

  1. after that you can go to jenkins and setup a shell command, in my case i did:之后你可以去 jenkins 并设置一个 shell 命令,在我的例子中我做了:

#!/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注意:如果您的 jenkins 根目录中已经有一个 .pm2 文件夹,请重命名它,以便您可以执行符号链接

Hope this helps希望这可以帮助

BUILD_ID=dontKillMe PM2 start mywebsite.config.js

Jenkins kills the pm2 daemon to be created by the build. Jenkins 杀死了要由构建创建的 pm2 守护进程。 You should put the keyword to prevent killing daemon by Jenkins.您应该输入关键字以防止 Jenkins 杀死守护进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM