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

问题描述 投票:0回答:5

我的 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": "*",
},
...

只要存储库是公开的,这就有效。现在我已将此存储库设置为私有。我用于拉/推“my_private_repo”的 git 凭据是该项目合作者的凭据。当我运行 composer updatecomposer install 时,如何实现 Composer 从该私有存储库中提取?

php git laravel github composer-php
5个回答
87
投票

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

JSON

{
    "require": {
        "vendor/my-private-repo": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "[email protected]:vendor/my-private-repo.git"
        }
    ]
}

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

文档


37
投票

我希望我的答案不会来得太晚,因为我自己才知道这一点。

生成 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]-----+

使用 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

配置 Composer

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

"repositories": [
    {
        "type": "vcs",
        "url": "repo1:YourAccount/repo1.git"
    },
    {
        "type": "vcs",
        "url": "repo2:YourAccount/repo2.git"
    }
],

repo1 和 repo2 是您在 ~/ssh/config 文件中创建的别名。 repo1 的完整 GitHub ssh URL 为:

[电子邮件受保护]:您的帐户/repo1.git

现在你应该已经做好了一切准备。您现在可以要求您的依赖项:

composer require youraccount/repo1 -n

composer require youraccount/repo2 -n

注意!当使用 GitHub 存储库作为 Composer 依赖项时,您始终需要将 -n 添加到每个 Composer 命令中。


7
投票

1。指向 Git 存储库

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

    "repositories":[
      {
        "type": "vcs",
        "url": "[email protected]:vendor/secret.git"
      }
    ]

2。创建 SSH 密钥

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

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

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

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

3.运行作曲家

现在只需作曲家要求或

composer install
照常打包。


1
投票

从命令行,您可以让composer确保您在composer.json文件中保留有效的json,使用如下命令来配置您的存储库:

composer config repositories.my_alias \
    '{"type": "vcs", \
      "url": "[email protected]:my_repo.git", \
      "ssh2": { "username": "git", \
                "privkey_file": "/var/lib/jenkins/.ssh/id_rsa", \
                "pubkey_file": "/var/lib/jenkins/.ssh/id_rsa.pub" \
              } \
    }'

注意: 我尚未在引用的属性中使用 '' 行连续标记进行测试。我成功的测试涉及在一行上运行这一切。但我发现这种格式对于人们来说更容易理解。

进一步注意: 此命令将继续抛出错误,直到您的

ssh-keygen
密钥对就位并且您的公钥在存储库上配置,如此问题的其他答案中所述。

运行此命令的结果是我的composer.json 文件中的以下条目:

    "repositories": {
        "drupal": {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        },
        "my_alias": {
            "type": "vcs",
            "url": "[email protected]:my_repo.git",
            "ssh2": {
                "username": "git",
                "privkey_file": "/var/lib/jenkins/.ssh/id_rsa",
                "pubkey_file": "/var/lib/jenkins/.ssh/id_rsa.pub"
            }
        }
    },

此用法记录在此处: https://getcomposer.org/doc/03-cli.md#config


0
投票

您也可以通过命令行将其添加到

composer.json
文件中:

composer config repositories.repo-name vcs https://github.com/<orgname or username>/repo-name

例如:

存储库:https://github.com/chapagain/auto-currency-switcher-2

composer config repositories.auto-currency-switcher vcs https://github.com/chapagain/auto-currency-switcher-2

运行上述命令会将以下内容添加到

composer.json
文件中:

repositories": {
    "auto-currency-switcher": {
      "type": "vcs",
      "url": "https://github.com/chapagain/auto-currency-switcher-2"
    },

之后,您可以运行以下命令来安装模块。

composer require <orgname or username>/repo:branchname

对于上面的示例,我们将运行以下命令来安装模块:

composer require chapagain/magento2-autocurrency

因为

composer.json
文件中的模块名称为
chapagain/magento2-autocurrency

https://github.com/chapagain/auto-currency-switcher-2/blob/master/composer.json#L2C12-L2C43

© www.soinside.com 2019 - 2024. All rights reserved.