jenkins-pm2重新启动jenkins中的所有命令,不更新public的node-app

时间:2019-02-28 09:24:10

标签: jenkins

我正在尝试在jenkins的帮助下在服务器上部署node.js项目,我添加了GitHub web-hook,一切正常。 pm2重新启动index.js ,当我从用户hamza运行此命令时,它使用新的pull代码更新了内容,但jenkins do命令成功运行但没有更新,即使我在shell中尝试了su

    + ./script/deploy
su: must be run from a terminal
From https://github.com/hamza-younas94/node-app
 * branch            master     -> FETCH_HEAD
   a7e9a1a..188a395  master     -> origin/master
Updating a7e9a1a..188a395
Fast-forward
 index.js      | 2 +-
 script/deploy | 2 +-
 test/test.js  | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
audited 190 packages in 1.706s
found 55 vulnerabilities (16 low, 19 moderate, 19 high, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [index.js](ids: 0)
[PM2] [index](0) ✓
┌──────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬─────────┬──────────┐
│ App name │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user    │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼─────────┼──────────┤
│ index    │ 0  │ 0.0.2   │ fork │ 10159 │ online │ 138     │ 0s     │ 0%  │ 22.0 MB   │ jenkins │ disabled │
└──────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴─────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
Finished: SUCCESS

我的pm2命令的ubtunu终端输出正常运行

┌──────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬─────────┬──────────┐
│ App name │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user    │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼─────────┼──────────┤
│ index    │ 0  │ 0.0.2   │ fork │ 10159 │ online │ 25     │ 0s     │ 0%  │ 22.0 MB   │ hamza │ disabled │
└──────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴─────────┴──────────┘

我的部署shell代码:

#!/bin/sh 
su - hamza
cd /home/hamza/node-app
git pull origin master
npm install --production
pm2 restart index.js
exit

1 个答案:

答案 0 :(得分:1)

好吧,我是通过shell中的shell做到的,我通过ssh连接其他用户。

  • 登录jenkins用户生成的ssh密钥
  • 将密钥添加到authorized_keys中
  • 写一个连接otheruser @ my_ip_add的外壳并编写我需要的命令

为什么我必须这样做? 因为pm2 restart一切都在工作,但是以jenkins用户的身份运行,您可以在我的问题中看到它,当我与其他启动它的其他用户重新启动它时,一切正常。

PS:可能是pm2需要相同的用户/会话来进行活动