我有一个使用 Gitlab CI 的 Gitlab 环境,用于新项目验证编译的文件并通过 rsync 复制到生产服务器。
执行这些资源构建的机器是 docker 的镜像(节点 6),但现在我必须使用 linux 将该容器 Docker 命令生成的文件复制到服务器...我的问题是通过 rsync 通过 ssh 连接。
目前我有以下内容:
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).
答案1
ssh-copy-id 要求输入密码。您可以sshpass -e
在 Gitlab 中使用和设置 SSHPASS 环境变量。
答案2
您没有将 ssh 密钥传递给 rsync。您应该执行类似这样的操作,执行 ssh 命令以正确识别 ssh 密钥:
rsync -avuz -e 'ssh -i ~/.ssh/deploy_rsa' $CI_PROJECT_DIR/dist/ [email protected]:/var/wwww/example.com