2016-09-07 78 views
0

我私人项目的大多数存储库托管在gitlab.com(托管解决方案,而不是私人托管的gitlab服务器)上的私有存储库中。这些网站托管在数字海洋VPS上。Gitlab.com CI无法登录到SSH服务器

我想使用gitlab CI在develop分支上自动部署在测试服务器上。由于我已经在这个测试服务器上拥有一个仓库的克隆,所以自动部署的最简单的方法似乎是让gitlab-ci连接到ssh服务器,并触发git pull。

gitlab-ci.yml我现在(ssh before_script抄自http://docs.gitlab.com/ce/ci/ssh_keys/README.html)。

deploy to test: 
    environment: test 
    only: 
    - develop 
    before_script: 
    # Install ssh-agent if not already installed, it is required by Docker. 
    # (change apt-get to yum if you use a CentOS-based image) 
    - 'which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)' 
    # Run ssh-agent (inside the build environment) 
    - eval $(ssh-agent -s) 

    # add ssh key stored in SSH_PRIVATE_KEY variable to the agent store 
    - ssh-add <(echo "$SSH_PRIVATE_KEY") 

    # disable host key checking (NOTE: makes you susceptible to man-in-the-middle attacks) 
    # WARNING: use only in docker container, if you use it with shell you will overwrite your user's ssh config 
    - mkdir -p ~/.ssh 
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config 

    script: 
    # Try and connect to the test server 
    - ssh [myname]@[mydomain.com] "cd /var/www/test.[projectname].com/ && git pull" 

在gitlab管道提交上develop结果:

$ ssh [myname]@[mydomain.com] "cd /var/www/test.[projectname].com/ && git pull" 
Warning: Permanently added '[mydomain.com],[255.255.255.255]' (ECDSA) to the list of known hosts. 

Permission denied, please try again. 

Permission denied, please try again. 

Permission denied (publickey,password). 

ERROR: Build failed: exit code 1 

我有我的本地用户对我的笔记本电脑加在gitlab的SSH_PRIVATE_KEY变量的私钥。私钥应该可以工作,因为我可以从我的笔记本电脑连接到服务器而无需提供密码。

有没有人有这个工作,gitlab.com工作人员如何连接到SSH服务器?

+0

嗨@vityrus,你管理使用ssh连接并执行命令在远程主机上? – BigDong

回答

0

AFAIK,你不能做到这一点:

# add ssh key stored in SSH_PRIVATE_KEY variable to the agent store 
- ssh-add <(echo "$SSH_PRIVATE_KEY") 

ssh-agent没有拿到钥匙情况下,也没有FD。你应该存储密钥在一些临时文件,然后将其添加到代理(和潜在删除该文件,如果不再需要它):

# add ssh key stored in SSH_PRIVATE_KEY variable to the agent store 
- echo "$SSH_PRIVATE_KEY" > key 
- chmod 600 key 
- ssh-add key 
- rm key 
+0

谢谢你的建议Jakuje! 我第一次尝试你的实际代码: ' @警告:未被保护的私人密钥文件! @ 'key'的权限0644太开放了。 建议您的私钥文件不能被他人访问。 该私钥将被忽略。 ' – vitrus

+0

然后您可能需要确保它具有适当的权限(写完后'chmod 600 key')。 – Jakuje

+0

我也做到了这一点(抱歉,评论有问题,发布代码没有多行代码)。 它给出了和我原来的尝试完全一样的输出:'允许被拒绝,请再试一次。'x 2 – vitrus