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


12

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

执行这些资源的机器是docker(节点6)的映像,但是现在我必须使用Linux将那个容器Docker命令的结果文件复制到服务器上……我的问题是通过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 $SERVER_USER@8.8.8.8
    - rsync -avuz $CI_PROJECT_DIR/dist/ $SERVER_USER@8.8.8.8:/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).

3
这是我不明白的事情:每个构建都是动态生成一个新的ssh密钥吗?需要pwd部署pub密钥时,使用ssh leys有何意义?
lrkwz

像@lrkwz一样,当您仍然被要求输入密码时,我也错过了在每个版本上发送新密钥的意义。另外,我也想在远程服务器上看到authorized_keys文件...
法比奥·杜克·席尔瓦

Answers:



1

您没有将ssh密钥传递给rsync。您应该执行以下操作,执行ssh命令以正确识别ssh密钥:

rsync -avuz -e 'ssh -i ~/.ssh/deploy_rsa' $CI_PROJECT_DIR/dist/ $SERVER_USER@8.8.8.8:/var/wwww/example.com
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.