更新
如果是第三方包,自己克隆到私有仓库,只需要代码打相应的版本tag.
使用
go mod edit -replace=module_name_xxx=gitlab.xxxx.com/xxgroup/xx.git@v1.0.1
现象
开源的go 包,自己加了些东西,上传到私有仓库想作为内部项目依赖,遇到各种问题导致go get 不成功。
说明
1、内部的gitlab只开放了ssh协议clone,而go get 默认是通过https协议的
所以需要在.gitconfig中加一个协议替换操作
[url "ssh://git@gitlab.your.com:2222/"]
insteadOf = https://gitlab.your.com/
2、go env 需要配置声明私有库
go env -w GOPRIVATE=gitlab.your.com
3、ssh config里配置git域名端口和私钥指向
Host gitlab.your.com
StrictHostKeyChecking no
User git
Port 2222
IdentityFile ~/.ssh/id_rsa
4、github克隆的项目需要重新生成 go.mod和go.sum, 并且替换import中路径为私有库地址。
rm -f go.mod go.sum
go mod init gitlab.your.com/xxx.git # 这里一定要加.git 不然 go get获取不到
# sed -i 's/原来的导入"/gitlab.your.com/xxx.git"' *
# 这里建议用ide工具查找替换。
go mod tidy
git add .
git commit -m 'make private lib'
git push
debug方法
go get gitlab.your.com/xxx.git 报错
git ls-remote -q origin in /home/xxx/go/pkg/mod/cache/vcs/aa41f7204630d5498fd5f5cf6fa82bc842a7cf11b1851ed97e8cf219bac8af41: exit status 128:
remote:
remote: ========================================================================
remote:
remote: The project you were looking for could not be found or you don't have permission to view it.
remote:
remote: ========================================================================
remote:
fatal: Could not read from remote repository.
使用下面命令看是否可以成功clone
git clone --verbose https://gitlab.your.com/xxx.git