1

I have the following .gitlab-ci.yml file, which aims to deploy my master GitLab repository by cloning it to the test directory on a specific server.

image: ubuntu:latest
before_script:
  - apt-get install -y
  - apt-get update -y
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y && apt-get install git -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null ## /dev/null = trou noir
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan charrier.alwaysdata.net >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

deploy:
  script:
    - ssh -o StrictHostKeyChecking=no -vT user_name@xxx.xx.xxx.xxx "cd test && git clone git@gitlab-myweb.com:repo_group/repo.git"
  only:
    - master

The $SSH_PRIVATE_KEY is the private key generated when logged in as user_name in the xxx.xx.xxx.xxx, where I am trying to clone my repo.

Upon running the ci, I get this error:

user_name@xxx.xx.xxx.xxx: Permission denied (publickey).
ERROR: Job failed: exit code 1

I have clearly messed up the ssh, but I am very confused what exactly is my issue. I was loosely following this guide.

Can you help me find my error?

Newskooler
  • 211
  • 1
  • 3
  • 14

1 Answers1

2

The reason was because I had not done two things:

  1. Add the server's public key to the server's authorized keys. Inside the server, this is done by simply running this line: cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  2. I had not added the server's public key to the GitLab profile's public keys. This is done by copying in the contents of cat ~/.ssh/id_rsa.pub and pasting them to https://yourgitlabwebsite.com/profile/keys
Newskooler
  • 211
  • 1
  • 3
  • 14
  • 1
    This was helpful since I ran into similar issue today and the steps helped me in solving the errors. – NightOwl19 May 23 '20 at 08:55
  • 1
    Actually, you should config the deployer ssh public key in Settings > Repository > Deploy keys, not on your profile – Henry Bui Aug 14 '21 at 15:42