【问题标题】:How to SSH to a local server thru local Gitlab runner using Gitlab-ci?如何使用 Gitlab-ci 通过本地 Gitlab 运行器 SSH 到本地服务器?
【发布时间】:2020-02-23 19:26:28
【问题描述】:

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

  • Local Gitlab Runner 在 vagrant 机器中设置,
  • 本地服务器模拟这是一个流浪机器静态IP
  • Gitlab 管道存储库包含 Local Runner 和 Local Server 之间的 SSH 连接。

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

触发 Gitlab 管道存储库 -> Local 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. 我可以从 runner 的 vagrant machine 内部 ping 或在管道期间通过 runner 到本地服务器成功

这是 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

有什么想法吗?提前谢谢

【问题讨论】:

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


    【解决方案1】:

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

    相反,我在 Gitlab 管道内使用 Local Gitlab Runner Machine's 的私钥和本地服务器 ~/.ssh/authorized_keys 内的 Runner 公钥。

    看我的草图:

    更多详情见我的文章:How to configure Gitlab-CI to Auto-deploy your App via SSH

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 2016-05-02
      • 2021-11-23
      • 2016-01-01
      • 2022-07-11
      相关资源
      最近更新 更多