0

I installed docker and gitlab + a runner using this tutorial: https://frenchco.de/article/Add-un-Runner-Gitlab-CE-Docker

The problem is that when I try to modify the .gitlab-ci.yml to make a deployment on my host machine I can not do it.

My .yml :

stages:
  - deploy

deploy_develop:
   stage: deploy
   before_script:
     - apk update && apk add bash && apk add openssh && apk add rsync
     - apk add --no-cache bash
   script:
     - mkdir -p ~/.ssh
     - ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
     - cat ~/.ssh/id_rsa.pub
     - rsync -hrvz ~/ root@172.16.1.97:~/web_dev/www/test/
   environment:
     name: develop

And the problem is that in ssh or rsync I always have the same error message in my job:

$ rsync -hrvz ~/ root@172.16.1.97:~/web_dev/www/test/
Host key verification failed.
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.3]

I tried to copy the ssh id_rsa and id_rsa.pub in the host, it's the same.

Surely a problem because my runner is in a docker can be? It is strange because I manage to ping my host (172.16.1.97) since the execution of the .yml. An idea has my problem?

VlaDDy
  • 1
  • 1

1 Answers1

1

Looks like you did not add the public key into your authorized_keys on the host server for the deploy-user?

For example, I use gitlab-ci to deploy my webapp, and therefore I added the user gitlab on my host machine, and added the public key to authorized_keys and then I can connect with ssh gitlab@IP -i PRIVATE_KEY to that server.

My gitlab-ci.yml looks like this:

deploy-app:
  stage: deploy
  image: ubuntu
  before_script:
    - apt-get update -qq
    - 'which ssh-agent || ( apt-get install -qq openssh-client )'
    - eval $(ssh-agent -s)
    - ssh-add <(cat "$DEPLOY_SERVER_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - chmod 755 ./deploy.sh
  script:
    - ./deploy.sh

where I added the private key's content as a variable to my gitlab-instance. (see https://docs.gitlab.com/ee/ci/variables/)

The deploy.sh looks like this:

#!/bin/bash
set -eo pipefail

scp app/docker-compose.yml gitlab@"${DEPLOY_SERVER_IP}":~/apps/${NGINX_SERVER_NAME}/
ssh gitlab@$DEPLOY_SERVER_IP "apps/${NGINX_SERVER_NAME}/app.sh update" # this is just doing docker-compose pull && docker-compose up in the app's directory.

Maybe this helps? It's working fine for me and scp/ssh are giving more intuitive error messages than what you got with rsync in this particular case.

ElectRocnic
  • 1,275
  • 1
  • 14
  • 25