希望我的回答不会太迟,因为我刚刚自学了这个。
生成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。
git clone git@bitbucket.org:vendor/my-private-repo.git
这样的命令能够在composer所运行的计算机上正常工作。对我来说,那个克隆命令不起作用,因为我通常使用SSH别名,例如:git clone git@my-alias:vendor/my-private-repo.git
。 - Sam Anthonyrequire
中引用的属性是指包名(即依赖项composer.json
中的名称),而不是仓库名称,这两者可能是不同的。 - craig_h