Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI: "Permission denied" when pulling private composer package

Tags:

gitlab-ci

I'm trying to setup CI with GitLab but I'm getting this build error:

  [RuntimeException]         

  Failed to execute git clone --no-checkout 'git@***.git' '/builds/***' && cd '/builds/***' && git remote add composer 'git@***.git' && git fetch composer  
  Cloning into '/builds/***'...       

  Permission denied, please try again. 

  Permission denied, please try again.                                                                                 
  Permission denied (publickey,password).                                                                            
  fatal: Could not read from remote repository.                                                                        
  Please make sure you have the correct access rights                                                                       
  and the repository exists.   

My composer.json looks like this:

{
    "repositories": [
        {
            "type": "git",
            "url": "git@***.git"
        }],
}

It obviously has something to do with the ssh key pair, but I don't know where to store the private/public keys.

like image 284
Pascal Avatar asked Oct 30 '22 06:10

Pascal


1 Answers

I just implemented a solution for this, based on https://docs.gitlab.com/ee/ci/ssh_keys/README.html and the example on https://gitlab.com/gitlab-examples/ssh-private-key/blob/master/.gitlab-ci.yml.

You basically need to

  • set up the SSH agent (especially when working with docker)
  • add private SSH key (preferably from a variable stored inside GitLab, as credentials should not be checked in)

Example, in case the link gets moved:

before_script:
  # install ssh-agent
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

  # run ssh-agent
  - eval $(ssh-agent -s)

  # add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
  - ssh-add <(echo "$SSH_PRIVATE_KEY")

  # Set up project.
  - composer install 
like image 62
pdu Avatar answered Jan 02 '23 21:01

pdu