我正在将一个 Next.JS 应用程序从 GitLab 直接部署到我的远程服务器。我已经配置了一个包含我的私有(private) SSH key 的变量,以便连接到远程服务器,并且我正在使用 rsync 从 docker runner 复制到我的远程服务器。当我通过 SSH 使用 PM2 重新启动服务时,一切正常,直到最后一行。
image: node:latest
stages:
- build
- test
- deploy
cache:
paths:
- node_modules/
- .next/
install_dependencies:
stage: build
script:
- npm install
- npm run build
artifacts:
paths:
- node_modules/
- .next/
test-build:
stage: test
script:
- npm run test
deploy_production:
stage: deploy
only:
- master
before_script:
- "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
- mkdir -p ~/.ssh
- eval $(ssh-agent -s)
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- apt-get update -y
- apt-get -y install rsync
script:
- ssh -p22 dev@example.com "mkdir -p /var/www/example.com/index_tmp"
- rsync -rav -e ssh --exclude='.git/' --exclude='.gitlab-ci.yml' --delete-excluded ./ dev@example.com:/var/www/example.com/index_tmp
- ssh -p22 dev@example.com "mv /var/www/example.com/index /var/www/example.com/index_old && mv /var/www/example.com/index_tmp /var/www/example.com/index"
- ssh -p22 dev@example.com "rm -rf /var/www/example.com/index_old"
- ssh -p22 dev@example.com "pm2 restart landing-page"
以下是任务运行程序日志的最后 8 行左右:
[... rsync output ...]
sent 193,022,347 bytes received 550,996 bytes 9,003,411.30 bytes/sec
total size is 191,108,661 speedup is 0.99
$ ssh -p22 dev@example.com "mv /var/www/example.com/index /var/www/example.com/index_old && mv /var/www/example.com/index_tmp /var/www/example.com/index"
$ ssh -p22 dev@example.com "rm -rf /var/www/example.com/index_old"
$ ssh -p22 dev@example.com "pm2 restart landing-page"
bash: pm2: command not found
ERROR: Job failed: exit code 1
奇怪的是,如果我直接连接到远程服务器并运行
pm2 restart landing-page
这将是结果:dev@example.com:~$ pm2 restart landing-page
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [landing-page](ids: 0)
[PM2] [landing-page](0) ✓
┌──────────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬──────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────┼──────────┤
│ landing-page │ 0 │ 0.33.11 │ fork │ 18174 │ online │ 2 │ 0s │ 0% │ 7.6 MB │ dev │ disabled │
└──────────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴──────────┴──────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
所以最后,我完全不知道为什么如果
rsync
中的所有内容都不起作用,为什么最后一个命令不起作用工作得很好,因为我确实检查过,并且远程服务器中的文件已更改。你们对如何解决这个问题有任何想法吗?我将不胜感激!
感谢阅读,感谢您的宝贵时间。
最佳答案
那是因为您的 SSH 调用是直接内联的。没有 .bash_aliases 或 .bash_rc 文件来源。
要使您的 pm2 工作,您应该使用完整路径调用它。为此,请直接在远程服务器上使用此命令检查您的 pm2 位置:
whereis pm2
# Output something like /usr/bin/pm2
然后使用之前给出的完整路径进行 SSH 调用:
script:
- ...
- ssh -p22 dev@example.com "/usr/bin/pm2 restart landing-page"
关于continuous-integration - Gitlab CI 部署失败 : "bash: pm2: command not found" during gitlab-ci. yml 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52890750/