git - Composer 使用 https 而不是 ssh(对于 GitLab)

标签 git ssh gitlab composer-php

当使用这样的composer.json来安装私有(private)包时:

{
    "require": {
        "foobar/example-package": "dev-master"
    },
    "repositories": [{
        "type": "vcs",
        "url": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9cec0dde9cec0ddc5c8cb87cac6c4" rel="noreferrer noopener nofollow">[email protected]</a>:foobar/example-package.git"
    }]
}

出于某种原因,Composer 尝试使用 https 而不是 ssh:

$ composer update
Loading composer repositories with package information
Failed to download foobar/example-package:The "https://gitlab.com/api/v4/projects/foobar%2Fexample-package" file could not be downloaded (HTTP/1.1 404 Not Found)      
Your credentials are required to fetch private repository metadata (<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bece2ffcbece2ffe7eae9a5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>:foobar/example-package.git)
A token will be created and stored in "…\composer\home/auth.json", your password will never be stored
To revoke access to this token you can visit https://gitlab.com/profile/applications
Username:

即使使用 -vvv 也没有任何迹象表明为什么 Composer 需要通过 https 访问存储库 - 尽管完全可以将代码推送和 pull 到私有(private)通过 ssh 存储库(使用存储在 SSH 代理中的私钥)。

Composer 1.x 和 2.x 会发生这种情况。有没有办法使用ssh来进行这样的操作?这个问题似乎也是 GitLab 特有的,因为我从未在 GitHub 存储库中遇到过这个问题。

最佳答案

仔细检查 Install composer packages from private repository from GitLab 中的“Syed Sirajul Islam Anik

So, to install a package from GitLab which is not a public repository will need a Personal Access Token.
You can issue your PAT by going to Profile Icon > Settings > Access Token. Then to issue a new PAT, write a name and select a scope api/read_api, read_api is a minimum. api will work too. If you want to set an expiry, can put value in that. Then press the Create personal access token Button.

Next, from the terminal. Run the following command.

composer config --global --auth gitlab-token.gitlab.com PAT_TOKEN

Replace the PAT_TOKEN with the token you found from the above process. And if you’re using a self-hosted GitLab, then you’ll need to change the URL gitlab.com to your self-hosted URL.

Then, in your composer.json file, add the following snippet.

"repositories": [
   {
       "type": "vcs",
       "url": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81e6e8f5c1e6e8f5ede0e3afe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>:namespace/repo-name.git"
   }
]

或者,as commented ,使用 "type": "git" 而不是 "vcs":

"repositories": [
    {
        "type": "git",
        "url": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cebe5f8ccebe5f8e0edeea2efe3e1" rel="noreferrer noopener nofollow">[email protected]</a>:namespace/repo-name.git"
    }
]

If your composer.json file already contains the repository key, then add the object to it.

Finally,

composer require vendor-name/package-name

换句话说,即使 URL 是 SSH,您仍然需要 PAT( token )才能使用 composer 调用的 HTTPS GitLab API。

关于git - Composer 使用 https 而不是 ssh(对于 GitLab),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67744374/

相关文章:

selenium - 通过 SSH 运行测试 NG Selenium 测试

upload - 如何将项目从本地文件系统上传到 GitLab

git - 在 git shell 中运行批处理文件

Gitignore 排除子文件夹(带有 **/pattern)除了特定的

git - 我如何告诉 git 总是选择我的本地版本来 merge 特定文件上的冲突?

git - 通过 SSH 将 VS Code 连接到 Github 会导致 SSH 错误

通过 SSH 协议(protocol)的 Github Gist 不起作用

git - 我如何找到 Git 仓库的版本

ruby-on-rails - 来自 gitlab-ci 的状态不再显示在 gitlab 合并请求中

git fsck : duplicateEntries: contains duplicate file entries - cannot push to gitlab