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

How to SSH to a local server thru local Gitlab runner using Gitlab-ci?

我在从本地 Gitlab 运行器 SSH 到本地服务器时遇到问题,这些是我的小故事中的人物:

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

触发Gitlab pipeline repository -> Local Gitlab Runner -> SSH to Local Server -> Deploy a demo file to the Local server .

这是我的.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. 我可以从 runner 的 vagrant 机器内部或在管道期间通过 runner 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

伙计们有什么想法吗?提前致谢

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

相反,我使用 本地 Gitlab Runner 机器的私钥 Gitlab 管道内的私钥和 ~/.ssh/authorized_keys 内的 Runner 的 public 密钥本地服务器。

看我的草图:

详情请看我的文章:How to configure Gitlab-CI to Auto-deploy your App via SSH