I am using Gitlab CI to build a static generated website and send it to my server. I am using a SSH keys pair to establish the connection from rsync, but my server is refusing the connection.
I tried several things: started with ED25519 key, changed to RSA key, all public keys are in authorised_keys in the server, changed permissions in files and folders by gitlab-ci.yml, used ssh-keyscan to put the servers in know_hosts... nothing help.
The gitlab-ci.yml is the following:
image: ruby:2
pages:
stage: deploy
before_script:
- apt-get update
- apt-get install rsync -y
- eval $(ssh-agent -s)
- chmod 400 "$SSH_PRIVATE_KEY"
- ssh-add "$SSH_PRIVATE_KEY"
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $SERVER >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- bundle install
- bundle exec jekyll build -d $LOCALDIR
after_script:
- rsync -paz $LOCALDIR $USER@$SERVER:$REMOTEDIR
artifacts:
paths:
- $LOCALDIR
only:
- master
- dev
Part of log output is the following:
$ rsync -paz $LOCALDIR $USER@$SERVER:$REMOTEDIR
Permission denied, please try again.
Permission denied, please try again.
$SERVER: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(228) [sender=3.2.3]
Well, any help or tip in this topic?
EDIT 1 - SSH logs
Thank you @Andrew. I used the ssh -vvvvvv ...
option as suggested and this is an extract of the output:
debug1: Next authentication method: 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_ecdsa_sk
debug3: no such identity: /root/.ssh/id_ecdsa_sk: 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
debug1: Trying private key: /root/.ssh/id_ed25519_sk
debug3: no such identity: /root/.ssh/id_ed25519_sk: No such file or directory
debug1: Trying private key: /root/.ssh/id_xmss
debug3: no such identity: /root/.ssh/id_xmss: No such file or directory
debug2: we did not send a packet, disable method
So, maybe the command ssh-add "$SSH_PRIVATE_KEY"
in gitlab-ci.yml is not working as expected.
EDIT 2 - the answer
Thank you to @VonC the tip to copy the SSH private key fixed the problem. Following the final gitlab-ci.yml
working as expected:
image: ruby:2
pages:
stage: deploy
before_script:
- apt-get update
- apt-get install rsync -y
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- cp "$SSH_PRIVATE_KEY" ~/.ssh/id_ed25519
- chmod 600 ~/.ssh/id_ed25519
- ssh-keyscan $SERVER >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- bundle install
- bundle exec jekyll build -d $LOCALDIR
after_script:
- rsync -auz --delete --omit-dir-times $LOCALDIR $USER@$SERVER:$REMOTEDIR
artifacts:
paths:
- $LOCALDIR
only:
- master
- dev