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

kx5bkwkv  于 2024-01-04  发布在  Git
关注(0)|答案(5)|浏览(157)

在我的Laravel 5.1项目composer.json中,我有以下内容来添加公共github仓库作为依赖项。

  1. ...
  2. "repositories": [
  3. {
  4. "type": "package",
  5. "package": {
  6. "name": "myVendorName/my_private_repo",
  7. "version": "1.2.3",
  8. "source": {
  9. "type" : "git",
  10. "url" : "git://github.com/myVendorName/my_private_repo.git",
  11. "reference" : "master"
  12. },
  13. "dist": {
  14. "url": "https://github.com/myVendorName/my_private_repo/archive/master.zip",
  15. "type": "zip"
  16. }
  17. }
  18. }
  19. ],
  20. "require": {
  21. ....
  22. "myVendorName/my_private_repo": "*",
  23. },
  24. ...

字符串
只要仓库是公共的,就可以使用。现在我已经将这个仓库设置为私有。我用于拉/推到'my_private_repo'的git凭据是项目的colaborator的凭据。当我运行composer updatecomposer install时,如何实现composer从该私有仓库拉取?

h9vpoimq

h9vpoimq1#

使用GitHub和BitBucket上的私有仓库:

JSON

  1. {
  2. "require": {
  3. "vendor/my-private-repo": "dev-master"
  4. },
  5. "repositories": [
  6. {
  7. "type": "vcs",
  8. "url": "[email protected]:vendor/my-private-repo.git"
  9. }
  10. ]
  11. }

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

展开查看全部
t0ybt7op

t0ybt7op2#

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

生成ssh密钥

你可以使用ssh-keygen命令生成n+1个ssh密钥。确保你在服务器上这样做!

  1. ~ cd ~/.ssh
  2. .ssh ssh-keygen
  3. Generating public/private rsa key pair.
  4. Enter file in which to save the key (/home/user/.ssh/id_rsa): repo1
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in repo1.
  8. Your public key has been saved in repo1.pub.
  9. The key fingerprint is:
  10. SHA256:EPc79FoaidfN0/PAsjSAZdomex2J1b/4zUR6Oj7IV2o user@laptop
  11. The key's randomart image is:
  12. +---[RSA 2048]----+
  13. | . . o .. |
  14. | o B o .. |
  15. | . + B o . |
  16. | . * B = .o|
  17. | S B O B+o|
  18. | o B =.+*|
  19. | o....Bo|
  20. | o E.o|
  21. | +.o |
  22. +----[SHA256]-----+

字符串
在使用ssh-keygen命令后,系统会提示你输入文件名和密码。你需要为每个要用作composer依赖的私有仓库提供一个密钥。在这个例子中,repo 1是文件名。
请确保密码和确认为空。

配置ssh获取正确的密钥

在servers ~/.ssh/config文件中,你可以为每个GitHub仓库分配一个别名。否则composer会尝试使用默认的id_rsa。

  1. Host repo1
  2. HostName github.com
  3. User git
  4. IdentityFile ~/.ssh/repo1
  5. IdentitiesOnly yes
  6. Host repo2
  7. HostName github.com
  8. User git
  9. IdentityFile ~/.ssh/repo2
  10. IdentitiesOnly yes

配置Composer

在项目composer.json文件中,你需要添加你想要的仓库作为依赖:

  1. "repositories": [
  2. {
  3. "type": "vcs",
  4. "url": "repo1:YourAccount/repo1.git"
  5. },
  6. {
  7. "type": "vcs",
  8. "url": "repo2:YourAccount/repo2.git"
  9. }
  10. ],


repo 1和repo 2是您在~/ssh/config文件中创建的别名。repo 1的完整GitHub ssh url为:
email protected(https://stackoverflow.com/cdn-cgi/l/email-protection):YourAccount/repo1.git
现在你应该已经设置好了。你现在可以要求你的依赖项:
第一个月
composer require youraccount/repo2 -n
注意!当使用GitHub仓库作为composer依赖时,您总是需要在每个composer命令中添加-n。

展开查看全部
xkftehaa

xkftehaa3#

1.指向Git仓库

更新composer.json并添加仓库:

  1. "repositories":[
  2. {
  3. "type": "vcs",
  4. "url": "[email protected]:vendor/secret.git"
  5. }
  6. ]

字符串

2.创建SSH密钥

在要安装软件包的计算机上创建SSH密钥。
如果你正在开发机器上工作,你可能想将SSH密钥添加到你的GitHub/BitBucket/GitLab帐户。这将允许访问你的帐户可以访问的所有私有存储库。
有关如何添加Github,Bitbucket或Gitlab SSH密钥的更多信息,请参阅此excellent article
如果您正在配置部署服务器,最好配置访问密钥或部署密钥。访问密钥仅提供对单个存储库的访问,因此允许更具体的访问管理。

3.运行composer

现在只是 composer 要求或composer install包像往常一样。

展开查看全部
dauxcl2d

dauxcl2d4#

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

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

字符串

**注意:**我还没有在引用的属性中使用''行连续标记进行测试。我成功的测试涉及在一行上运行所有这些。但我发现这种格式更容易让人理解。
**进一步注意:**此命令将继续抛出错误,直到您的ssh-keygen密钥对到位,并且您的公钥已在存储库上配置,如本问题的其他答案所述。

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

  1. "repositories": {
  2. "drupal": {
  3. "type": "composer",
  4. "url": "https://packages.drupal.org/8"
  5. },
  6. "my_alias": {
  7. "type": "vcs",
  8. "url": "[email protected]:my_repo.git",
  9. "ssh2": {
  10. "username": "git",
  11. "privkey_file": "/var/lib/jenkins/.ssh/id_rsa",
  12. "pubkey_file": "/var/lib/jenkins/.ssh/id_rsa.pub"
  13. }
  14. }
  15. },


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

展开查看全部
vatpfxk5

vatpfxk55#

您也可以通过命令行将其添加到composer.json文件中:

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

字符串
举例来说:
存储库:https://github.com/chapagain/auto-currency-switcher-2

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


运行上面的命令会将以下内容添加到composer.json文件中:

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


类型可以是vcs,或者我们也可以通过使用git来指定类型。
要使用的MySQL驱动程序会根据URL自动检测。但是,如果您出于任何原因需要指定一个驱动程序,您可以使用bitbucketgithubgitlabperforcefossilgitsvnhg作为存储库类型,而不是vcs
https://getcomposer.org/doc/05-repositories.md#types:~:text=The%20VCS%20driver,vcs
之后,您可以运行以下命令来安装该模块。

  1. composer require <orgname or username>/repo:dev-<branchname>


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

  1. composer require chapagain/magento2-autocurrency


因为composer.json文件中模块的名称是chapagain/magento2-autocurrency:https://github.com/chapagain/auto-currency-switcher-2/blob/master/composer.json#L2C12-L2C43

展开查看全部

相关问题