代码之家  ›  专栏  ›  技术社区  ›  fillipvt

Gitlab-CI部署失败:在Gitlab-CI.yml运行期间“bash:pm2:command not found”

  •  1
  • fillipvt  · 技术社区  · 8 年前

    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"
    

    下面是TaskRunner日志的最后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 工作得很好,因为我检查过了,而且文件在远程服务器上被更改了。

    你们知道怎么解决这个问题吗?我将不胜感激!

    1 回复  |  直到 8 年前
        1
  •  10
  •   André DS Nina Scholz    8 年前

    这是因为SSH调用是直接内联的。没有.bash\u别名或.bash\u rc文件。

    要使你的pm2工作,你应该称之为完整的路径。为此,请直接在远程服务器上使用以下命令检查pm2位置:

    whereis pm2
    # Output something like /usr/bin/pm2
    

    然后使用前面给出的完整路径进行SSH调用:

     script:
        - ...
        - ssh -p22 dev@example.com "/usr/bin/pm2 restart landing-page"