Golang获取下载私有GitLab项目失败怎么办
Golang获取下载私有GitLab项目失败怎么办 我有一个项目需要依赖私有GitLab上的私有项目。
我通过以下命令进行了配置,但出现了URL 404错误。示例如下:
$ go env -w GOPRIVATE=a.b.com
$ go env -w GOINSECURE=a.b.com
$ go env -w GONOPROXY=a.b.com
$ git config --global url."https://username:private-token@a.b.com".insteadOf "http://a.b.com"
$ go get -v -x a.b.com/testgroup/compkg
# get https://a.b.com/?go-get=1
# get https://a.b.com/testgroup/testpkg?go-get=1
# get https://a.b.com/testgroup?go-get=1
# get https://a.b.com/testgroup?go-get=1: 404 Not Found (0.044s)
# get https://a.b.com/?go-get=1: 403 Forbidden (0.044s)
# get https://a.b.com/testgroup/testpkg?go-get=1: 404 Not Found (0.044s)
# get https://a.b.com/testgroup/testpkg?go-get=1
# get https://a.b.com/testgroup?go-get=1
# get https://a.b.com/?go-get=1
# get https://a.b.com/testgroup/testpkg?go-get=1: 404 Not Found (0.001s)
# get https://a.b.com/testgroup?go-get=1: 404 Not Found (0.001s)
# get https://a.b.com/?go-get=1: 403 Forbidden (0.002s)
go get: unrecognized import path "a.b.com/testgroup/testpkg": reading https://a.b.com/testgroup/testpkg?go-get=1: 404 Not Found
然后我尝试切换到SSH来拉取,示例如下:
$ git config --global url."git@a.b.com:testgroup/compkg.git".insteadOf "https://a.b.com/testgroup/compkg"
$ go get -v -x a.b.com/testgroup/compkg
# get https://a.b.com/?go-get=1
# get https://a.b.com/testgroup/testpkg?go-get=1
# get https://a.b.com/testgroup?go-get=1
# get https://a.b.com/testgroup?go-get=1: 404 Not Found (0.044s)
# get https://a.b.com/?go-get=1: 403 Forbidden (0.044s)
# get https://a.b.com/testgroup/testpkg?go-get=1: 404 Not Found (0.044s)
# get https://a.b.com/testgroup/testpkg?go-get=1
# get https://a.b.com/testgroup?go-get=1
# get https://a.b.com/?go-get=1
# get https://a.b.com/testgroup/testpkg?go-get=1: 404 Not Found (0.001s)
# get https://a.b.com/testgroup?go-get=1: 404 Not Found (0.001s)
# get https://a.b.com/?go-get=1: 403 Forbidden (0.002s)
go get: unrecognized import path "a.b.com/testgroup/testpkg": reading https://a.b.com/testgroup/testpkg?go-get=1: 404 Not Found
两次尝试都出现相同的错误。go get: unrecognize import path "a.b.com/testgroup/compkg": reading https://a.b.com/testgroup/compkg?go-get=1: 404 Not Found
是否有其他方法可以尝试,或者是我尝试的方式有误?期待您的回复。
注:
testgroup是GitLab的组名,不是用户名。
- GitLab版本:Gitlab Community Edition 13.6.7
- Go版本:go v1.16 和 v1.20
- 我可以通过输入用户名和密码
git clone项目
更多关于Golang获取下载私有GitLab项目失败怎么办的实战教程也可以访问 https://www.itying.com/category-94-b0.html
遗憾的是,我尝试按照链接操作并更改了地址为我自己的,但错误仍然存在。
我不确定这是否是因为我使用了 GitLab 群组而非用户名。即 gitlab.company.com/group/reponame 而不是 gitlab.company.com/myusername/reponame。
更多关于Golang获取下载私有GitLab项目失败怎么办的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在我看来,你的 GOPRIVATE 环境变量设置是正确的。我之前在使用私有模块时也这样设置过,并且运行正常,不过我对私有模块之类的并不是特别有经验。也许可以按照这个指南操作?
如何在您自己的项目中使用私有 Go 模块 | DigitalOcean
许多 Go 模块是开源的,这意味着可以自由访问和使用它们。然而,有时您需要使模块私有。在本教程中,您将…
问题在于Go模块系统没有正确识别你的私有GitLab仓库。你需要确保GitLab服务器返回正确的go-get元数据。以下是解决方案:
1. 检查GitLab的go-get支持
首先验证你的GitLab实例是否支持go-get查询:
curl -s https://a.b.com/testgroup/compkg?go-get=1
如果返回404,说明GitLab没有正确配置。
2. 配置.gitlab-ci.yml(推荐)
在私有项目的根目录添加.gitlab-ci.yml文件:
pages:
stage: deploy
script:
- mkdir .public
- cp -r * .public
- mv .public public
artifacts:
paths:
- public
only:
- main
然后在项目根目录创建index.html文件:
<!DOCTYPE html>
<html>
<head>
<meta name="go-import" content="a.b.com/testgroup/compkg git https://a.b.com/testgroup/compkg.git">
<meta name="go-source" content="a.b.com/testgroup/compkg _ https://a.b.com/testgroup/compkg/-/tree/main{/dir} https://a.b.com/testgroup/compkg/-/blob/main{/dir}/{file}#L{line}">
</head>
<body>
go get a.b.com/testgroup/compkg
</body>
</html>
3. 使用SSH方式(无需GitLab Pages)
配置SSH并设置环境变量:
# 生成SSH密钥并添加到GitLab
ssh-keygen -t ed25519 -C "your-email@example.com"
# 配置SSH
git config --global url."ssh://git@a.b.com".insteadOf "https://a.b.com"
# 设置环境变量
export GOPRIVATE="a.b.com"
export GOINSECURE="a.b.com"
export GONOPROXY="a.b.com"
# 使用SSH协议获取
go get -v -insecure a.b.com/testgroup/compkg
4. 使用Personal Access Token(HTTPS方式)
# 生成GitLab Personal Access Token(api权限)
# 配置git使用token
git config --global url."https://gitlab-ci-token:${GITLAB_TOKEN}@a.b.com".insteadOf "https://a.b.com"
# 设置环境变量
export GOPRIVATE="a.b.com"
export GONOPROXY="a.b.com"
# 获取依赖
go get a.b.com/testgroup/compkg
5. 使用replace指令(临时解决方案)
在go.mod文件中添加replace指令:
module your-module
go 1.20
require a.b.com/testgroup/compkg v0.0.0
replace a.b.com/testgroup/compkg => /path/to/local/clone
然后手动克隆仓库到本地:
git clone https://username:token@a.b.com/testgroup/compkg.git /path/to/local/clone
6. 验证配置
创建测试文件验证配置:
package main
import (
"fmt"
_ "a.b.com/testgroup/compkg"
)
func main() {
fmt.Println("Import successful")
}
运行:
go mod init test
go mod tidy
go run main.go
最常见的问题是GitLab没有正确响应?go-get=1查询。推荐使用第2种方法(GitLab Pages)或第3种方法(SSH方式)。如果GitLab版本较旧,可能需要升级到支持go-get元数据的版本。


