GitLab:从内网拉取私有仓库go module

43 篇文章 3 订阅
9 篇文章 1 订阅

GitLab:从内网拉取私有仓库go module


golang通过go mod管理项目依赖。

通常,我们从github等项目托管平台拉取依赖go module。

例如:go get -u github.com/kataras/iris/v12

假如,公司有内网gitlab托管项目代码,如何从私有仓库拉取依赖?

假设,公司的内网gitlab地址是:gitlab.xxx.com,是yyy团队下的test1280仓库。


1.设置GOPRIVATE

设置windows中的环境变量:GOPRIVATE

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

重启GoLand,并打开Terminal终端查看:

在这里插入图片描述

C:\Users\EB\Desktop\test1281>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\EB\AppData\Local\go-build
set GOENV=C:\Users\EB\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\workspace\gopath\pkg\mod
set GONOPROXY=gitlab.xxx.com
set GONOSUMDB=gitlab.xxx.com
set GOOS=windows
set GOPATH=D:\workspace\gopath
set GOPRIVATE=gitlab.xxx.com
set GOPROXY=https://goproxy.io
set GOROOT=C:\Program Files\Go
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.4
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\EB\Desktop\test1281\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\EB\AppData\Local\Temp\go-build1897173819=/tmp/go-build -gno-record-
gcc-switches

注意:

a.如果windows设置GOPRIVATE,则自动设置GONOPROXY、GONOSUMDB。

b.不设置windows用户变量,仅设置GoLand项目配置也可以。


2.go get拉取私有仓库module

在终端执行:go get -v -x gitlab.xxx.com/yyy/test1280

提示输入gitlab鉴权的用户名称、密码:

在这里插入图片描述

C:\Users\EB\Desktop\test1281>go get -v -x gitlab.xxx.com/yyy/test1280
# get https://gitlab.xxx.com/?go-get=1
# get https://gitlab.xxx.com/yyy?go-get=1
# get https://gitlab.xxx.com/yyy/test1280?go-get=1
# get https://gitlab.xxx.com/yyy/test1280?go-get=1: 200 OK (0.130s)
get "gitlab.xxx.com/yyy/test1280": found meta tag vcs.metaImport{Prefix:"gitlab.xxx.com/yyy/test1280", VCS:"git", RepoRoot:"https://gitl
ab.xxx.com/yyy/test1280.git"} at //gitlab.xxx.com/yyy/test1280?go-get=1
mkdir -p D:\workspace\gopath\pkg\mod\cache\vcs # git3 https://gitlab.xxx.com/yyy/test1280.git
# lock D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7.lock# D:\workspace\gopath\pkg\mod\
cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7 for git3 https://gitlab.xxx.com/yyy/test1280.git
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git ls-remote -q origin
# get https://gitlab.xxx.com/?go-get=1: 200 OK (0.248s)
# get https://gitlab.xxx.com/yyy?go-get=1: 200 OK (0.259s)
68.605s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git ls-remote -q origin
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=false log -n
1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
0.144s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=fal
se log -n1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git tag -l
0.096s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git tag -l
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git fetch -f --depth=1 origin 30ab843
6b395a56b16fc3074ea2d0458e6de720b:refs/dummy
1.970s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git fetch -f --depth=1 origi
n 30ab8436b395a56b16fc3074ea2d0458e6de720b:refs/dummy
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=false log -n
1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
0.068s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=fal
se log -n1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.052s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(refname)
refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
0.045s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(
refname) refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(refname)
refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
0.044s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(
refname) refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=false log -n
1 '--format=format:%H %ct %D' 30ab8436b395 --
0.043s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=fal
se log -n1 '--format=format:%H %ct %D' 30ab8436b395 --
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.041s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.072s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.043s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
go: downloading gitlab.xxx.com/yyy/test1280 v0.0.0-20210709084921-30ab8436b395
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.060s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.041s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c core.autocrlf=input -c core.eo
l=lf archive --format=zip --prefix=prefix/ 30ab8436b395a56b16fc3074ea2d0458e6de720b
0.049s # cd D:\workspace\gopath\pkg\mod\cache\vcs\52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c core.autocrlf=input -
c core.eol=lf archive --format=zip --prefix=prefix/ 30ab8436b395a56b16fc3074ea2d0458e6de720b
WORK=C:\Users\EB\AppData\Local\Temp\go-build4264731656
gitlab.xxx.com/yyy/test1280
mkdir -p $WORK\b001\
cat >$WORK\b001\importcfg << 'EOF' # internal
# import config
packagefile fmt=C:\Program Files\Go\pkg\windows_amd64\fmt.a
EOF
cd C:\Users\EB\Desktop\test1281
"C:\\Program Files\\Go\\pkg\\tool\\windows_amd64\\compile.exe" -o "$WORK\\b001\\_pkg_.a" -trimpath "$WORK\\b001=>" -p gitlab.xxx.com/yyy/tes
t1280 -lang=go1.16 -complete -buildid qNUpqkNuqz_1UjHCaxjB/qNUpqkNuqz_1UjHCaxjB -goversion go1.16.4 -D "" -importcfg "$WORK\\b001\\importcfg" -p
ack -c=4 "D:\\workspace\\gopath\\pkg\\mod\\gitlab.xxx.com\\yyy\\test1280@v0.0.0-20210709084921-30ab8436b395\\test1280.go"
"C:\\Program Files\\Go\\pkg\\tool\\windows_amd64\\buildid.exe" -w "$WORK\\b001\\_pkg_.a" # internal
cp "$WORK\\b001\\_pkg_.a" "C:\\Users\\EB\\AppData\\Local\\go-build\\bb\\bbf4455401e93ebea348106585f9c272029c30221e418cde126f3b41dc3c07db-d" # in
ternal
go get: added gitlab.xxx.com/yyy/test1280 v0.0.0-20210709084921-30ab8436b395

提示,新增依赖库成功:

go get: added gitlab.xxx.com/yyy/test1280 v0.0.0-20210709084921-30ab8436b395

在这里插入图片描述

在go get时,自动弹出提示框,要求输入gitlab的用户名称、密码。

输入之后,用户名称、密码将会存储在windows的凭据管理器中:

win+s打开搜索框,输入“凭据管理器”:

在这里插入图片描述
在这里插入图片描述

后续如果我们再次拉取内网gitlab的仓库时,系统会自动帮我们填充认证。


3.ssh方式免密拉取

考虑一个问题:

假设gitlab的用户密码每天更新,每天都设置一次windows凭据管理器实在繁琐低效。

是否有更好的办法解决?

可设置windows用户公钥由gitlab信任,然后用户通过ssh免密拉取gitlab的代码仓库。

如何生成和设置windows用户公钥请查看:

https://blog.csdn.net/test1280/article/details/118608774

设置完成后,我们就可以使用ssh免密拉取gitlab的代码仓库。

但是!

go mod使用的是git http协议而非git ssh协议。

需要设置git配置:

git config --global url."git@gitlab.xxx.com:".insteadof "https://gitlab.xxx.com/"

其中,gitlab.xxx.com需要替换成内部域名或IP地址。

一切都没问题了吗?

4.更好的方案

当内网gitlab的ssh服务端口非ssh默认端口22时,将出现问题:

在这里插入图片描述

即使设置:

git config --global url."git@gitlab.xxx.com:10000".insteadof "https://gitlab.xxx.com/"

(其中,10000是内部gitlab的ssh服务端口)

实际测试,设置的10000端口并没有生效,go get调用git时,使用的仍然是22端口。

更好的方法是,通过设置netrc来实现git http的免密,参考:

https://blog.csdn.net/test1280/article/details/118613074

重点是!

我们可以从gitlab设置访问令牌,作为我们的永久密码存储到netrc中:

在这里插入图片描述

即使gitlab用户密码发生变化,gitlab生成的访问令牌仍然有效,我们不必修改netrc。


5.总结

从内网gitlab拉取私有仓库module,至少有三种方式:

  1. 设置netrc实现git http免密
  2. 设置gitlab信任ssh公钥,通过git config实现git ssh替代git http免密
  3. 设置windows凭据管理器

推荐使用:gitlab生成访问令牌+设置netrc。


6.参考

https://blog.zhaoweiguo.com/2019/09/24/golang-env-private-git/
https://www.cnblogs.com/lion.net/p/10408505.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值