Gitlab CI部署失败:gitlab-ci.yml运行期间“ bash:pm2:命令未找到”

连续集成 React.js

西里神奇

2020-04-07

我正在从GitLab部署Next.JS应用程序直接到我的远程服务器。我已经配置了一个变量,该变量包含我的私有SSH密钥,以便连接到远程服务器,并且我正在使用rsync从docker运行程序复制到我的远程服务器。当我在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,因为我确实检查了一下,并在远程服务器中更改了文件。

你们对如何解决这个问题有想法吗?我将不胜感激!

感谢您的阅读,也感谢您的宝贵时间。

第4116篇《Gitlab CI部署失败:gitlab-ci.yml运行期间“ bash:pm2:命令未找到”》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
GO 2020.04.07

那是因为您的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"

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android