如何将私有github存储库添加为Composer依赖项

epi*_*ero 24 php git github laravel composer-php

我在Laravel 5.1项目composer.json中有以下内容,用于添加公共github存储库作为依赖项.

...    
"repositories": [
  {
    "type": "package",
    "package": {
      "name": "myVendorName/my_private_repo",
      "version": "1.2.3",
      "source": {
        "type" : "git",
        "url" : "git://github.com/myVendorName/my_private_repo.git",
        "reference" : "master"
      },
      "dist": {
        "url": "https://github.com/myVendorName/my_private_repo/archive/master.zip",
        "type": "zip"
      }
    }
  }
],
"require": {
     ....
    "myVendorName/my_private_repo": "*",
},
...
Run Code Online (Sandbox Code Playgroud)

只要存储库是公共的,这就可以工作.现在我将此存储库设置为私有.我用来拉/推到'my_private_repo'的git凭证是该项目的合作者之一.当我运行composer updatecomposer install时,如何从该私有存储库中获取该作曲家?

Sof*_*ali 36

在GitHub和BitBucket上使用私有存储库:

JSON

{
    "require": {
        "vendor/my-private-repo": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "git@bitbucket.org:vendor/my-private-repo.git"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

唯一的要求是为git客户端安装SSH密钥.

文件

  • 请注意,`require`中引用的属性是指包名称(即依赖关系的`composer.json`中的名称)而不是存储库名称,它可能不同. (8认同)
  • 任何人都可以澄清究竟是什么`唯一的要求是为git客户端安装SSH密钥'意味着什么? (4认同)
  • 检查上面的示例composer.json是否有效的一种方法是,首先确保像“git clone git@bitbucket.org:vendor/my-private-repo.git”这样的命令在composer将要运行的计算机上运行继续运行。对我来说,克隆命令不起作用,因为我通常使用 SSH 别名,例如 `git clone git@my-alias:vendor/my-private-repo.git` (2认同)

Tan*_*mik 18

我希望我的回答不会太晚,因为我刚刚学会了这一点.还进入了我的博客:https://keevitaja.com/posts/using-github-private-repositories-as-composer-dependencies

生成ssh密钥

您可以使用ssh-keygen命令生成n + 1个ssh密钥.确保在服务器中执行此操作!

?  ~ cd ~/.ssh
?  .ssh ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): repo1
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in repo1.
Your public key has been saved in repo1.pub.
The key fingerprint is:
SHA256:EPc79FoaidfN0/PAsjSAZdomex2J1b/4zUR6Oj7IV2o user@laptop
The key's randomart image is:
+---[RSA 2048]----+
|      . . o ..   |
|       o B o ..  |
|      . + B o  . |
|       . * B = .o|
|        S B O B+o|
|         o B =.+*|
|          o....Bo|
|            o E.o|
|             +.o |
+----[SHA256]-----+
Run Code Online (Sandbox Code Playgroud)

使用ssh-keygen命令后,系统将提示您输入文件名和密码.您需要为将要用作作曲家依赖项的每个私有存储库创建一个密钥.在此示例中,repo1是文件名.

确保将密码和确认留空.

配置ssh以获取正确的密钥

在服务器〜/ .ssh/config文件中,您可以为每个GitHub存储库分配别名.否则,composer会尝试使用默认的id_rsa.

Host repo1
HostName github.com
User git
IdentityFile ~/.ssh/repo1
IdentitiesOnly yes

Host repo2
HostName github.com
User git
IdentityFile ~/.ssh/repo2
IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)

配置Composer

在项目composer.json文件中,您需要添加所需的存储库作为依赖项:

"repositories": [
    {
        "type": "vcs",
        "url": "repo1:YourAccount/repo1.git"
    },
    {
        "type": "vcs",
        "url": "repo2:YourAccount/repo2.git"
    }
],
Run Code Online (Sandbox Code Playgroud)

repo1和repo2是您在〜/ ssh/config文件中创建的别名.repo1的完整GitHub ssh url将是:

git@github.com:YourAccount/repo1.git

而现在你应该做好.您现在可以要求您的依赖项:

composer require youraccount/repo1 -n

composer require youraccount/repo2 -n

NB!使用GitHub存储库作为编写器依赖项时,您总是需要为每个编写器命令添加-n.

  • 你好。我来自 2021 年。我挣扎了一整天,直到找到你的答案。为什么没有“-n”就不起作用? (3认同)

Bas*_*MHL 5

1.指向Git仓库

更新 composer.json 并添加一个存储库:

    "repositories":[
      {
        "type": "vcs",
        "url": "git@github.com:vendor/secret.git"
      }
    ]
Run Code Online (Sandbox Code Playgroud)

2. 创建 SSH 密钥

在要安装软件包的计算机上创建 SSH 密钥。

如果您在开发机器上工作,您可能希望将 SSH 密钥添加到您的 GitHub/BitBucket/GitLab 帐户。这可以访问您的帐户有权访问的所有私有存储库。

有关如何添加 Github、Bitbucket 或 Gitlab SSH 密钥的更多信息,请参阅这篇优秀文章

如果您正在配置部署服务器,最好配置访问密钥或部署密钥。访问密钥仅提供对单个存储库的访问,因此允许进行更具体的访问管理。

3. 运行作曲家

现在只是 composer require 或composer install像往常一样的包。