栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

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

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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:UsersEBDesktoptest1281>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:UsersEBAppDataLocalgo-build
set GOENV=C:UsersEBAppDataRoaminggoenv
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:workspacegopathpkgmod
set GONOPROXY=gitlab.xxx.com
set GONOSUMDB=gitlab.xxx.com
set GOOS=windows
set GOPATH=D:workspacegopath
set GOPRIVATE=gitlab.xxx.com
set GOPROXY=https://goproxy.io
set GOROOT=C:Program FilesGo
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=C:Program FilesGopkgtoolwindows_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:UsersEBDesktoptest1281go.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:UsersEBAppDataLocalTempgo-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:UsersEBDesktoptest1281>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:workspacegopathpkgmodcachevcs # git3 https://gitlab.xxx.com/yyy/test1280.git
# lock D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7.lock# D:workspacegopathpkgmod
cachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7 for git3 https://gitlab.xxx.com/yyy/test1280.git
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; 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:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git ls-remote -q origin
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=false log -n
1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
0.144s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=fal
se log -n1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git tag -l
0.096s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git tag -l
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git fetch -f --depth=1 origin 30ab843
6b395a56b16fc3074ea2d0458e6de720b:refs/dummy
1.970s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git fetch -f --depth=1 origi
n 30ab8436b395a56b16fc3074ea2d0458e6de720b:refs/dummy
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=false log -n
1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
0.068s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=fal
se log -n1 '--format=format:%H %ct %D' 30ab8436b395a56b16fc3074ea2d0458e6de720b --
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.052s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(refname)
refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
0.045s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(
refname) refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(refname)
refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
0.044s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git for-each-ref --format %(
refname) refs/tags --merged 30ab8436b395a56b16fc3074ea2d0458e6de720b
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=false log -n
1 '--format=format:%H %ct %D' 30ab8436b395 --
0.043s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c log.showsignature=fal
se log -n1 '--format=format:%H %ct %D' 30ab8436b395 --
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.041s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.072s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.043s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
go: downloading gitlab.xxx.com/yyy/test1280 v0.0.0-20210709084921-30ab8436b395
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.060s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b395a56b16f
c3074ea2d0458e6de720b:go.mod
0.041s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git cat-file blob 30ab8436b3
95a56b16fc3074ea2d0458e6de720b:go.mod
cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c core.autocrlf=input -c core.eo
l=lf archive --format=zip --prefix=prefix/ 30ab8436b395a56b16fc3074ea2d0458e6de720b
0.049s # cd D:workspacegopathpkgmodcachevcs52b930c57b03f37a1dd325269e9a6fa1e8925ecd9255ff40da8f04ff278ba5e7; git -c core.autocrlf=input -
c core.eol=lf archive --format=zip --prefix=prefix/ 30ab8436b395a56b16fc3074ea2d0458e6de720b
WORK=C:UsersEBAppDataLocalTempgo-build4264731656
gitlab.xxx.com/yyy/test1280
mkdir -p $WORKb001
cat >$WORKb001importcfg << 'EOF' # internal
# import config
packagefile fmt=C:Program FilesGopkgwindows_amd64fmt.a
EOF
cd C:UsersEBDesktoptest1281
"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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906714.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号