GitLab CI: “Permission denied” when pulling private composer package

亡梦爱人 提交于 2021-01-27 13:51:37

问题


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.


回答1:


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 


来源:https://stackoverflow.com/questions/44444727/gitlab-ci-permission-denied-when-pulling-private-composer-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!