如何使用Gitlab-ci通过本地Gitlab运行程序通过SSH到本地服务器?

时间:2020-02-21 10:00:45

标签: ssh gitlab gitlab-ci gitlab-ci-runner

我在从本地Gitlab运行程序到本地服务器之间进行SSH时遇到问题,这些是我的小故事的特征:

    在流浪者机器中
  • 本地Gitlab Runner 设置
  • 本地服务器模拟,它是具有静态IP的无业游民的机器
  • Gitlab管道存储库包含本地运行器和本地服务器之间的SSH连接。

最终结果应该是通过 Local Gitlab Runner 将演示文件部署到 Local Server ,并使用 SSH

触发Gitlab管道存储库-> 本地Gitlab Runner -> SSH到本地服务器->将演示文件部署到本地服务器。

这是我的 .gitlab-ci.yml 文件:

image: ubuntu:latest

stages:
  - deploy

deploy:
  stage: deploy
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y && apt-get install -y iputils-ping )'
    - eval $(ssh-agent -s)
    - echo "$PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    # - echo "$AWS_EC2_PRIKEY" | tr -d '\r' | ssh-add - > /dev/null

    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
    - ping -c 2 192.168.213.160
    - ssh -vvvv -o StrictHostKeyChecking=no vagrant@192.168.213.160 "ls ~"


    # - ping -c 2 ec2XXX.amazonaws.com
    # - ssh -o StrictHostKeyChecking=no ec2-user@ec2XXX.amazonaws.com "ls ~"

  tags:
    - docker
  only: 
    - master

两个重要说明:

  1. 我试图用EC2替换本地服务器(请参阅注释 .gitlab-ci.yml文件中的第几行),并且工作正常
  2. 我可以从运行程序的游民机器内部ping或在运行到本地服务器的管道中通过运行程序成功

这是SSH日志的一部分:

 debug1: Server host key: ecdsa-sha2-nistp256 SHA256:6OLHurOSA2T9E/Q00bMRa129Ma21bYG2U+9wCqNr0A0
 Warning: Permanently added '192.168.213.160' (ECDSA) to the list of known hosts.
 debug3: send packet: type 21
 debug2: set_newkeys: mode 1
 debug1: rekey after 134217728 blocks
 debug1: SSH2_MSG_NEWKEYS sent
 debug1: expecting SSH2_MSG_NEWKEYS
 debug3: receive packet: type 21
 debug1: SSH2_MSG_NEWKEYS received
 debug2: set_newkeys: mode 0
 debug1: rekey after 134217728 blocks
 debug2: key: (stdin) (0x555e8014a4a0), agent
 debug2: key: /root/.ssh/id_rsa ((nil))
 debug2: key: /root/.ssh/id_dsa ((nil))
 debug2: key: /root/.ssh/id_ecdsa ((nil))
 debug2: key: /root/.ssh/id_ed25519 ((nil))
 debug3: send packet: type 5
 debug3: receive packet: type 7
 debug1: SSH2_MSG_EXT_INFO received
 debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
 debug3: receive packet: type 6
 debug2: service_accept: ssh-userauth
 debug1: SSH2_MSG_SERVICE_ACCEPT received
 debug3: send packet: type 50
 debug3: receive packet: type 51
 debug1: Authentications that can continue: publickey
 debug3: start over, passed a different list publickey
 debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
 debug3: authmethod_lookup publickey
 debug3: remaining preferred: keyboard-interactive,password
 debug3: authmethod_is_enabled publickey
 debug1: Next authentication method: publickey
 debug1: Offering public key: RSA SHA256:l2h6Lwchp4znO049FtrtUCQFboW2OGLT6vKj27jc9ss (stdin)
 debug3: send_pubkey_test
 debug3: send packet: type 50
 debug2: we sent a publickey packet, wait for reply
 debug3: receive packet: type 51
 debug1: Authentications that can continue: publickey
 debug1: Trying private key: /root/.ssh/id_rsa
 debug3: no such identity: /root/.ssh/id_rsa: No such file or directory
 debug1: Trying private key: /root/.ssh/id_dsa
 debug3: no such identity: /root/.ssh/id_dsa: No such file or directory
 debug1: Trying private key: /root/.ssh/id_ecdsa
 debug3: no such identity: /root/.ssh/id_ecdsa: No such file or directory
 debug1: Trying private key: /root/.ssh/id_ed25519
 debug3: no such identity: /root/.ssh/id_ed25519: No such file or directory
 debug2: we did not send a packet, disable method
 debug1: No more authentication methods to try.
 vagrant@192.168.213.160: Permission denied (publickey).
 ERROR: Job failed: exit code 1

有创意的人吗?预先感谢

1 个答案:

答案 0 :(得分:0)

我的错误是我在Gitlab管道中使用了 Local Server 的私钥;

相反,我使用的是Gitlab管道内的 Local Gitlab Runner计算机的私钥和本地服务器的~/.ssh/authorized_keys内的Runner的公钥。

请参阅我的草图: enter image description here

有关更多详细信息,请参见我的文章:How to configure Gitlab-CI to Auto-deploy your App via SSH