go get 使用内部私有仓库踩坑记录

更新

如果是第三方包,自己克隆到私有仓库,只需要代码打相应的版本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 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 `go get` 命令从私有仓库中获取包,需要进行以下步骤: 1. 在你的私有仓库中创建一个访问令牌或者 SSH 密钥,并将其添加到你的 Git 提供商中。这一步根据你使用Git 提供商(如 GitHubGitLab、Bitbucket 等)而有所不同,可以参考对应的文档进行操作。 2. 将访问令牌或者 SSH 密钥添加到你的本地 Git 配置中。如果你使用的是访问令牌,可以执行以下命令: ``` git config --global github.token <your_token> ``` 如果你使用的是 SSH 密钥,可以将其添加到 `~/.ssh/config` 文件中,如下所示: ``` Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa ``` 其中 `IdentityFile` 指定了你的 SSH 密钥路径。 3. 使用 `go get` 命令获取包,并在 URL 前加上访问令牌或者 SSH 协议,如下所示: ``` go get git@github.com:<username>/<repo>.git ``` 或者: ``` go get https://<token>@github.com/<username>/<repo>.git ``` 其中 `<token>` 指代访问令牌,`<username>` 和 `<repo>` 分别指代 Git 仓库的用户名和仓库名。 注意:如果你使用的是 HTTPS 协议,需要将 `<token>` 编码,否则会出现错误。可以使用以下命令进行编码: ``` echo -n "<token>" | base64 ``` 然后将编码后的字符串替换 `<token>`。 4. 等待 `go get` 命令完成,就可以在你的项目中使用私有仓库中的包了。 需要注意的是,私有仓库中的包可能会受到 Git 提供商的限制。例如,GitHub 的免费账户只能创建公共仓库,如果要创建私有仓库需要购买付费版。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值