Gitlab CI – 通过SSH部署到远程服务器

我有一个使用Gitlab CI的Gitlab环境,用于一个新项目来certificate编译的文件,并通过rsync复制到生产服务器。

build立这些资源的机器执行是一个docker(节点6)的形象,但现在我不得不复制从该容器的结果文件的Docker命令到服务器使用Linux …我的问题是通过SSH通过连接rsync的。

目前我有以下几点:

stages: - deploy before_script: - npm i - npm run build job_deploy: stage: deploy script: - ssh-keygen -t rsa -b 4096 -C '' -f ~/.ssh/deploy_rsa - ssh-keyscan -H 8.8.8.8 >> ~/.ssh/known_hosts - ssh-copy-id -i ~/.ssh/deploy_rsa.pub [email protected] - rsync -avuz $CI_PROJECT_DIR/dist/ [email protected]:/var/wwww/example.com only: - master 

由此我得到:

  /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password). 

ssh-copy-id正在询问密码。 您可以使用sshpass -e并在Gitlab中设置SSHPASS环境variables。

您没有将ssh密钥传递给rsync。 你应该这样执行ssh命令来正确识别ssh密钥:

 rsync -avuz -e 'ssh -i ~/.ssh/deploy_rsa' $CI_PROJECT_DIR/dist/ [email protected]:/var/wwww/example.com