使用rsync和sshpass自动部署Gitlab CI

时间:2018-08-11 07:14:42

标签: deployment rsync gitlab-ci sshpass

我在gitlab-ci上进行自动部署时遇到问题。我想从运行在Runner上的docker发送文件到远程主机(我的网站托管)。 我的.gitlab-ci.yml就是这样

variables:
# STAGING CONFIGURATION
  STAGING_USER: "test@test.com"
  STAGING_URL: "test.com"
# $STAGING_SSH_PASSWORD - configure in gilab->settings->CI/CD->variables

# image with php an
image: webdevops/php-apache-dev:5.6

before_script:
  - apt-get update -qq && apt-get install -y -qq sshpass
  - apt-get install zip unzip
  - apt-get install -y ssh
  - mkdir ~/.ssh
  - echo "StrictHostKeyChecking no" >> ~/.ssh/config
  - apt-get install -y rsync

# DEPLOY STAGING
DEPLOY_STAGING:
  stage: deploy
  only:
    - master
  script:
    - export SSHPASS=$STAGING_SSH_PASSWORD
    - rsync -hrvz -e sshpass -e  theme/ $STAGING_USER:~/test/

但是当我尝试这种配置时,我得到了错误

rsync: Failed to exec theme/: Permission denied (13) rsync error: error in IPC code (code 14) at pipe.c(85) [Receiver=3.1.2] rsync: connection unexpectedly closed (0 bytes received so far) [Receiver] rsync error: error in rsync protocol data stream (code 12) at io.c(235) [Receiver=3.1.2] ERROR: Job failed: exit code 1

我不知道如何正确配置它。我必须通过sshpass进行身份验证,因为我的服务器不允许rsa身份验证。

2 个答案:

答案 0 :(得分:0)

您需要将rsync ssh shell参数放在引号中

rsync -hrvz -e "sshpass -e" theme/ $STAGING_USER:~/test/

答案 1 :(得分:0)

在sshpass的手册页中,我找到了以下示例:

rsync --rsh='sshpass -p 12345 ssh -l test' host.example.com:path . 

https://linux.die.net/man/1/sshpass