如何将私有的Github仓库添加为Composer依赖

59

我在我的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 update 或 composer install 时,如何实现 composer 从该私有存储库中拉取?
4个回答

87

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

JSON

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

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

文档


12
有人能解释一下,“The only requirement is the installation of SSH keys for a git client” 是什么意思吗? 这句话的意思是,“唯一的要求是为 git 客户端安装 SSH 密钥”。 - Sam Anthony
2
检查上面的composer.json示例是否有效的一种方法是首先确保像git clone git@bitbucket.org:vendor/my-private-repo.git这样的命令能够在composer所运行的计算机上正常工作。对我来说,那个克隆命令不起作用,因为我通常使用SSH别名,例如:git clone git@my-alias:vendor/my-private-repo.git - Sam Anthony
25
请注意,require 中引用的属性是指包名(即依赖项 composer.json 中的名称),而不是仓库名称,这两者可能是不同的。 - craig_h
1
@Dennis中的“dev-master”只是指主分支,您可以要求“dev-staging”或任何其他分支。您还可以在软件包存储库上要求标记的提交,例如,名为“2.0.1”的git标记可以使用composer版本约束来要求,如下所示:“vendor/my-private-repo”:“〜2.0.0”。https://getcomposer.org/doc/articles/versions.md#writing-version-constraints - James
1
这还能用吗?我的ssh密钥已经放好了,可以克隆仓库而不会出现问题,但无论何时运行composer install,我都会被要求提供一个令牌。 - clueless
显示剩余3条评论

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 命令后,会提示您输入文件名和密码。每个将用作 composer 依赖项的私有存储库都需要一个密钥。在此示例中,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为:

git@github.com:YourAccount/repo1.git

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

composer require youraccount/repo1 -n

composer require youraccount/repo2 -n

NB!当将GitHub存储库用作Composer依赖项时,您始终需要向每个Composer命令添加-n。


1
在 Windows 10 计算机上应该把密钥存储在哪里? - Geoff Salmon
1
抱歉,我是一个 Linux 用户。 - Tanel Tammik
@GeoffSalmon 通常密钥会被存储在用户目录下。例如:C:\Users\YOURUSER.ssh - Radmation
3
你好。我来自2021年。我努力了一整天,直到找到了你的答案。为什么不加“-n”就无法工作呢? - doox911
1
为什么每个代码库都需要一个单独的ssh密钥?不能使用默认的id_rsa吗? - Sithell

7

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密钥的更多信息,请参见这篇优秀文章

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

3. 运行Composer

现在只需像往常一样运行composer require或composer install即可安装软件包。


我还需要运行 composer require vendor/secret 来安装这个包。 - Dennis

1

通过命令行,您可以让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" \
              } \
    }'

注意:我没有测试在引号属性内使用“”行继续标记。我的成功测试涉及在单行上运行所有内容。但我发现这种格式对人类更易于理解。

进一步说明:在你的ssh-keygen密钥对就位并且你的公钥已按照本问题的其他答案进行配置之前,该命令将继续抛出错误。

运行此命令会在我的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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接