Report this

What is the reason for this report?

Gitlab Pipelines Permission Denied (PublicKey)

Posted on November 10, 2019

I’m able to ssh into my droplet from my local machine. However, I have a gitlab pipelines job that fails to SSH to the production server at digital ocean. As far as I can tell, I’ve done everything correctly:

  1. Copied public key into the production server’s ~/.ssh/authorized_keys
  2. ran chmod -R go= ~/.ssh and chown -R $USER:$USER ~/.ssh as this guide suggests.
  3. Copied private key into the PRODUCTION_PRIVATE_KEY variable in the CI/CD settings.

after running the job, it still fails when connecting to the server with the error code:

root@123.456.789.10: Permission denied (publickey).
ERROR: Job failed: exit code 1

Here’s my .gitlab-ci.yml:

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - 'apt-get update -y && apt-get -y install rsync'
  - mkdir -p ~/.ssh
  - eval $(ssh-agent -s)
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

stage_deploy:
  artifacts:
    paths:
      - /
  #only run script when pushed to master branch 
  only:
    - master
  script:
    #get private key
    - ssh-add <(echo "$PRODUCTION_PRIVATE_KEY") 
    #make a _tmp directory on server **THIS IS WHERE IT FAILS**
    - ssh -p22 root@123.456.789.10 "mkdir /var/www/html/example.com_tmp"
    #copy all repo files to _tmp
    - rsync -avz --exclude=.git --exclude=src -r /builds/geochanto/example-wp/ george@123.456.789.10:/var/www/html/example.com_tmp
    #move site folder to _old, move _tmp to site
    - ssh -p22 j8rqv2sd9lt6@107.180.54.236 "mv /var/www/html/example.com/ /var/www/html/example.com_old && mv /var/www/html/example.com_tmp /var/www/html/example.com"
    #remove _old folder
    - ssh -p22 j8rqv2sd9lt6@107.180.54.236 "rm /var/www/html/example.com_old"


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello, @geochanto

May I ask if the IP address is actually legit - 123.456.789.10 ? Or you need to change this to 107.180.54.236?

Looking forward to your reply.

Regards, Alex

Did you get a solution for this? I have the same problem.

Br

Frank

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.