Jehad Nasser
Jehad Nasser

Reputation: 3565

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

I have a problem to SSH from local Gitlab runner to a local server, these are the characters of my little story:

the end result should be to deploy a demo file to the Local Server thru Local Gitlab Runner and using SSH.

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

This is my .gitlab-ci.yml file:

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 [email protected] "ls ~"


    # - ping -c 2 ec2XXX.amazonaws.com
    # - ssh -o StrictHostKeyChecking=no [email protected] "ls ~"

  tags:
    - docker
  only: 
    - master

Two Important notes:

  1. I have tried to replace the Local server with EC2(see the commented lines in the .gitlab-ci.yml file) and works fine.
  2. I can ping from inside the vagrant machine of the runner or thru the runner during the pipeline to the Local server successfully .

here is part of the SSH's logs:

 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.
 [email protected]: Permission denied (publickey).
 ERROR: Job failed: exit code 1

Any idea guys? thanks in advance

Upvotes: 3

Views: 15422

Answers (1)

Jehad Nasser
Jehad Nasser

Reputation: 3565

My mistake was that I am using the private key of Local Server inside the Gitlab Pipeline;

Instead I am using the Private key of the Local Gitlab Runner Machine's Private key inside the Gitlab Pipeline and the Runner's public key inside the ~/.ssh/authorized_keys of the Local server.

see my sketch: enter image description here

For more details see my article: How to configure Gitlab-CI to Auto-deploy your App via SSH

Upvotes: 2

Related Questions