gitlab-ci利用rsync和sshpass部署项目时报错“Host key verification failed”


由于是内部私有项目,为了方便连接ssh时,使用了密码

.gitlab-ci.yml

stages:
  - deploy

# 部署
deploy:
  stage: deploy
  image: alpine:latest
  variables:
    TARGET_DIR: /opt/1panel/apps/openresty/openresty/www/sites/scripts/index/
  before_script:
    - apk update
    - apk add openssh
    - apk add rsync
    - apk add sshpass
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "StrictHostKeyChecking no" >> ~/.ssh/known_hosts
    - export SSHPASS=$SERVER_PASSWORD
  script:
    - sshpass -e rsync -azv --delete --exclude='.*' . $SERVER_USER@$SERVER_HOST:$TARGET_DIR
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

报错信息:

Host key verification failed.
rsync error: unexplained error (code 255) at rsync.c(713) [sender=3.2.5]
ERROR: Job failed: exit code 6

解决

添加-e "ssh -o StrictHostKeyChecking=no"选项
修改同步命令为

sshpass -e rsync -azv -e "ssh -o StrictHostKeyChecking=no" --delete --exclude='.*' . $SERVER_USER@$SERVER_HOST:$TARGET_DIR

声明:初心|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - gitlab-ci利用rsync和sshpass部署项目时报错“Host key verification failed”


愿你勿忘初心,并从一而终