打赏

使用gitlab作为go mod私服

使用gitlab作为go mod私服

在go 1.13.3之前,由于 gitlab 的版本问题,如果使用了 subgroup,则不能正确返回 meta tag。也就是说,我们使用的 gitlab 版本只支持一层 namespace 下建的项目,如果使用了两层namespace就会拉取不到仓库依赖。
问题原因:
https://gitlab.com/gitlab-org/gitlab/-/issues/30612
解决办法:
a.迁移项目,将项目整理到一个namespace下。
b.在go.mod中使用replace替换。

replace (
        gitlab.company.com/aa/bb/xlib => gitlab.company.com/aa/bb/xlib.git v1.0.0
)

c.修改go源码

go/src/cmd/go/internal/modload/query.go

d.在gitlab下创建.netrc文件
https://stackoverflow.com/questions/56817601/how-to-use-go-dep-with-gitlab-subgroups/57099566#57099566

(1.)设置代理

# direct 表示代理走不通时,使用直接连接
go env -w GOPROXY="https://goproxy.cn,direct"

(2.)设置私服
设置私有库后,go会自动设置GONOPROXY,GONOSUMDB两个环境变量,表示私有库不走代理,也不检查代码sum。

go env -w GOPRIVATE="github.com/myprivate/*,gitlab.ce/*"
go env -w GOPRIVATE="*.company.io,*.company.com," 

(3.)权限问题
将 http 形式的的请求,转换成 ssh 的形式,在.gitconfig中添加一行配置就可以:

[url "git@github.com:"]
    insteadOf = https://github.com/
[url "git@gitlab.com:"]
    insteadOf = https://gitlab.com/

或者:
git config --global url."git@gitlab.com:groupName/projectName.git".insteadOf "https://gitlab.com/groupName/projectName.git"`

// 全局替换,拉取域名下的所有包
git config --global url."git@gitlab.yoursite.com:".insteadof "https://gitlab.yoursite.com/"

相关链接

https://iliubang.cn/go/2019/10/17/基于公司私有gitlab的go-module实践.html
https://blog.csdn.net/nxb593427560/article/details/104875291
https://www.jianshu.com/p/ca4404512cf3
https://segmentfault.com/a/1190000021127791

posted @ 2021-02-18 11:44  苍山落暮  阅读(2744)  评论(0编辑  收藏  举报