GitLab私有库必须在repositories中显式声明"type":"vcs",否则Composer无法识别并报“Could not find a matching version”;正确配置为{"type":"vcs","url":"https://gitlab.example.com/myorg/mylib.git"},且URL须指向带.git后缀的裸仓库。

GitLab私有库必须显式声明为 vcs 类型,否则 composer require 会报 “Could not find a matching version”
Composer 不会从 URL 自动推断仓库类型。把 "https://gitlab.example.com/myorg/mylib.git" 直接写进 repositories 数组是无效的——它会被当成普通 HTTP 源,根本不会尝试 clone。
正确写法是嵌套对象:
{
"type": "vcs",
"url": "https://gitlab.example.com/myorg/mylib.git"
}
url 必须指向裸仓库(结尾带 .git),不能是 GitLab Web UI 路径(如 /mylib/-/tree/main)。
- 若用 HTTPS 认证,需提前在
auth.json中配置gitlab-token或http-basic - 若用 SSH,URL 应为
git@gitlab.example.com:myorg/mylib.git,且本地 SSH agent 已加载对应密钥 - 多个私有库时,
repositories数组要按优先级从上到下排列,Composer 会顺序查找
auth.json 配置必须匹配 GitLab 域名和认证方式,写错一个字符就失败
常见错误:把 "gitlab.example.com" 写成 "https://gitlab.example.com" 或漏掉端口(如 gitlab.example.com:8443);或者误用 gitlab-oauth 字段对接口不支持 OAuth 的内网 GitLab。
内网 GitLab 最常用的是 gitlab-token 方式:
{ "gitlab-token": { "gitlab.example.com": "glpat-xxxxxx" } }
如果 GitLab 启用了自定义域名或非标准端口,gitlab-domains 必须显式声明:
将 GitHub Actions OIDC 令牌与 GitHub OIDC 提供商 JWKS 端点进行验证。验证主体声明、受众限制和仓库所有权,以确保云部署的安全性。
{ "gitlab-domains": ["gitlab.example.com", "gitlab.internal:8443"] }
-
auth.json必须放在项目根目录(与composer.json同级),且已加入.gitignore - CI 环境推荐用
COMPOSER_AUTH环境变量注入,避免文件残留 - 执行
composer config --global --unset gitlab-token.gitlab.example.com可安全清除旧 token
版本必须靠 tag,dev-main 在协作中极不稳定
私有库没发到 Packagist,Composer 默认只认 dev-main 这类开发分支;而 v1.2.0 这样的 tag 默认不被识别为可安装版本——除非主项目明确允许稳定版本。
主项目的 composer.json 必须设:
"config": {
"minimum-stability": "stable",
"prefer-stable": true
}
否则 composer require myorg/mylib 可能装上 dev-main,别人 composer install 时因稳定性策略不一致直接失败。
- 私有库打 tag 后(如
v1.2.0),主项目才能写"myorg/mylib": "^1.2" - CI 构建中禁用交互(
--no-interaction)时,绝对不要依赖dev-main—— 它随时可能变,结果不可控 - 用
composer show myorg/mylib验证实际解析出的版本,别只看require字段
secure-http 开关不匹配会导致 404 或认证绕过
当 GitLab 只支持 HTTP(如内网测试环境),composer.json 中需显式关闭安全限制:
"config": { "secure-http": false }
但一旦开启 HTTPS,这个配置必须删掉或设为 true,否则 Composer 会自动拼接 /api/v4/projects/... 路径去调用 GitLab API,结果返回 404(因为内网 GitLab 可能未启用 API 或路径不同)。
- 错误现象:
The "https://gitlab.example.com/api/v4/projects/..." file could not be downloaded (HTTP/2 404) - 根本原因:Composer 把
secure-http: false当作“所有请求走 HTTP”,却仍试图用 HTTPS 去调 API - 最稳妥做法:统一用 HTTPS + 正确证书,彻底移除
secure-http配置项
GitLab 私有库的安全连接不是配对 URL 和 token 就完事——repositories 类型、auth.json 域名粒度、minimum-stability 策略、secure-http 行为这四者必须咬合,缺一不可。最容易被跳过的,是确认私有库自身 composer.json 里 name 字段是否与 require 中的包名完全一致(包括大小写和 vendor 名)。










