php 如何将私有 github 存储库添加为 Composer 依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40619393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add private github repository as Composer dependency
提问by epic_antihero
I have the following in my Laravel 5.1 projects composer.json to add a public github repository as a dependency.
我在 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": "*",
},
...
This works as long as the repository is public. Now I've set this repository to private. The git credentials I use for pulling/pushing to 'my_private_repo' are the one of a colaborator of the project. How can I achieve that composer pulls from that private repository when I run composer updateor composer install?
只要存储库是公开的,这就会起作用。现在我已将此存储库设置为私有。我用于拉/推到“my_private_repo”的 git 凭据是该项目的合作者之一。当我运行composer update或composer install时,如何实现从该私有存储库中提取的composer?
回答by Sofiene Djebali
Work with private repositories at GitHub and BitBucket:
在 GitHub 和 BitBucket 上使用私有存储库:
JSON
JSON
{
"require": {
"vendor/my-private-repo": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "[email protected]:vendor/my-private-repo.git"
}
]
}
The only requirement is the installation of SSH keys for a git client.
唯一的要求是为 git 客户端安装 SSH 密钥。
回答by Tanel Tammik
I hope my answer does not come too late as i just learned this my self. Also made an entry to my blog: https://keevitaja.com/posts/using-github-private-repositories-as-composer-dependencies
我希望我的答案不会来得太晚,因为我刚刚学会了自己。还对我的博客做了一个条目:https: //keevitaja.com/posts/using-github-private-repositories-as-composer-dependencies
Generating a ssh key
生成 ssh 密钥
You can generate n+1 ssh keys with ssh-keygen command. Make sure you do this in the server!
您可以使用 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]-----+
After using the ssh-keygen command you will be prompted for the filename and passphrase. You need a key for each private repository you're going to use as composer dependency. In this example the repo1 is the filename.
使用 ssh-keygen 命令后,系统会提示您输入文件名和密码。您需要为要用作作曲家依赖项的每个私有存储库提供一个密钥。在这个例子中,repo1 是文件名。
Make sure you leave the passphrase and confirmation empty.
确保将密码和确认留空。
Configuring the ssh to pick up the correct key
配置 ssh 以获取正确的密钥
In servers ~/.ssh/config file you can assign an alias for each GitHub repository. Otherwise composer tries to use the default id_rsa.
在服务器 ~/.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
Configuring Composer
配置作曲家
In projects composer.json file you need to add the repositories you want as dependencies:
在项目 composer.json 文件中,您需要添加所需的存储库作为依赖项:
"repositories": [
{
"type": "vcs",
"url": "repo1:YourAccount/repo1.git"
},
{
"type": "vcs",
"url": "repo2:YourAccount/repo2.git"
}
],
repo1 and repo2 are the aliases you created in ~/ssh/config file. The full GitHub ssh url for repo1 would be:
repo1 和 repo2 是您在 ~/ssh/config 文件中创建的别名。repo1 的完整 GitHub ssh url 将是:
[email protected]:YourAccount/repo1.git
[email protected]:YourAccount/repo1.git
And now you should be set for good. You can now require your dependencies:
现在你应该准备好了。您现在可以要求您的依赖项:
composer require youraccount/repo1 -n
composer require youraccount/repo1 -n
composer require youraccount/repo2 -n
composer require youraccount/repo2 -n
NB! When using GitHub repositories as composer dependencies you always need to add -n to each composer command.
注意!使用 GitHub 存储库作为 composer 依赖项时,您始终需要在每个 composer 命令中添加 -n。
回答by BassMHL
1. Point to the Git repository
1.指向Git仓库
Update composer.json and add a repository:
更新 composer.json 并添加一个存储库:
"repositories":[
{
"type": "vcs",
"url": "[email protected]:vendor/secret.git"
}
]
2. Create an SSH key
2. 创建 SSH 密钥
Create an SSH Key on the machine on which you want to install the package.
在要安装软件包的计算机上创建 SSH 密钥。
If you are working on a development machine, you probably want to add the SSH key to your GitHub/BitBucket/GitLab account. This gives access to all private repositories that your account has access to.
如果您在开发机器上工作,您可能希望将 SSH 密钥添加到您的 GitHub/BitBucket/GitLab 帐户。这可以访问您的帐户有权访问的所有私有存储库。
For more information on how to add Github, Bitbucket or Gitlab SSH keys, see this excellent article
有关如何添加 Github、Bitbucket 或 Gitlab SSH 密钥的更多信息,请参阅这篇优秀文章
In case you are configuring a deployment server, it would be better to configure an access key or deploy key. An access key only provides access to a single repository and thus allows for more specific access management.
如果您正在配置部署服务器,最好配置访问密钥或部署密钥。访问密钥仅提供对单个存储库的访问,因此允许进行更具体的访问管理。
3. Run composer
3. 运行作曲家
Now just composer require or composer install
the package as usual.
现在只是 composer require 或composer install
像往常一样的包。