【问题标题】:How to add private github repository as Composer dependency如何将私有 github 存储库添加为 Composer 依赖项
【发布时间】:2017-03-29 21:35:12
【问题描述】:

我的 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


    【解决方案1】:

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

    JSON

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

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

    Docs

    【讨论】:

    • 谁能澄清The only requirement is the installation of SSH keys for a git client 的确切含义?
    • 检查上述示例 composer.json 是否可以工作的一种方法是首先确保像 git clone git@bitbucket.org:vendor/my-private-repo.git 这样的命令在 composer 将运行的机器上工作。对我来说,克隆命令不起作用,因为我通常使用 SSH 别名,例如 git clone git@my-alias:vendor/my-private-repo.git
    • 请注意require中引用的属性指的是包名(即依赖的composer.json中的名称)而不是存储库名称,可能不同。
    • 在require指令中是否总是必须是dev-master
    • @SamAnthony 他表示您将有权访问 git 可以看到的任何密钥,因此任何需要这些密钥的存储库。如果您可以使用 git 克隆 repo,composer 可以将其拉入 vendor/.
    【解决方案2】:

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

    生成 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 存储库分配一个别名。否则作曲家会尝试使用默认的 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.json 文件中,您需要添加所需的存储库作为依赖项:

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

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

    git@github.com:YourAccount/repo1.git

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

    composer require youraccount/repo1 -n

    composer require youraccount/repo2 -n

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

    【讨论】:

    • 在 Windows 10 机器上将密钥存储在哪里?
    • 对不起,我是linux用户。
    • @GeoffSalmon 通常密钥存储在您的用户目录中。示例:C:\Users\YOURUSER\.ssh
    • 你好。我是 2021 年的。我挣扎了一整天,直到找到你的答案。为什么没有-n 就不行?
    • 为什么每个存储库都需要一个单独的 ssh 密钥?我不能使用默认的 id_rsa 吗?
    【解决方案3】:

    1.指向 Git 存储库

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

        "repositories":[
          {
            "type": "vcs",
            "url": "git@github.com:vendor/secret.git"
          }
        ]
    

    2。创建 SSH 密钥

    在您要安装软件包的机器上创建一个 SSH 密钥。

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

    有关如何添加 Github、Bitbucket 或 Gitlab SSH 密钥的更多信息,请参阅excellent article

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

    3.运行作曲家

    现在只需像往常一样使用 composer require 或 composer install 包。

    【讨论】:

    • 我还必须运行 composer require vendor/secret 来安装软件包
    【解决方案4】:

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

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

    注意:我没有在引用的属性中使用 '' line continuance 标记进行测试。我成功的测试涉及在一条线上运行所有这些。但我发现这种格式在这种格式下更容易让人理解。

    进一步说明:此命令将继续抛出错误,直到您的 ssh-keygen 密钥对到位并且您的公钥已在 repo 中配置,如此问题的其他答案中所述。

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

        "repositories": {
            "drupal": {
                "type": "composer",
                "url": "https://packages.drupal.org/8"
            },
            "my_alias": {
                "type": "vcs",
                "url": "git@git.example.com: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

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 2016-03-18
      • 2015-04-28
      • 2019-02-20
      • 2012-04-03
      • 1970-01-01
      • 2021-03-22
      • 2017-02-03
      相关资源
      最近更新 更多