gitlab 搭建go modules私有仓库


gitlab 搭建go modules私有仓库

重点:

1.go get需要用域名的方式。 2.go get默认访问的是https。

目前策略

go get ---> nginx(ssl 反代gitlab)-->gitlab

实现
go get gittest.xxxx.com/Goland/gotogit

以gitlab(192.168.128.128)仓库为例测试

1.域名准备
1.1 域名分配
1.2  域名解析
gittest.xxxx.com  192.168.128.128
2.准备https(gittest.xxxx.com)反向代理

​ 2.1 申请ssl证书 ​ 2.2 配置nginx代理 ​ 2.3 访问https://gittest.xxxx.com测试

server {
        listen    443 ssl;
        server_name  gittest.xxxx.com;
        ssl_certificate     /etc/nginx/cert/gittest.xxxx.com_chain.crt;
        ssl_certificate_key   /etc/nginx/cert/gittest.xxxx.com_key.key;

        ssl_session_timeout  5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        if ($args ~* "^go-get=1") {
                set $condition goget;
        }
        if ($uri ~ "(/[^/]+/[^/]+)(/.*)?") {
                set $condition "${condition}path";
        }
        if ($condition = gogetpath) {
                return 200 "<!DOCTYPE html><html><head><meta content='gittest.xxxx.com$1 git https://gittest.xxxx.com$1.git' name='go-import'></head></html>";
        }
        location / {
                proxy_pass http://192.168.128.128:8088/; #gitlab的实际http地址
        }
}
3.gitlab上 开启go-proxy 功能
sudo gitlab-rails console
#执行
Feature.enable(:go_proxy)
4.创建api token
创建个人访问令牌(注意要有仓库权限)
4.1.点击右上角,选择您的头像。
4.2.选择settings。
4.3.点击左侧边栏上,选择Access Tokens。
4.4.输入令牌的名称和可选的到期日期。
4.5.勾选api Access your API。
4.6.粘贴生成的token即可。
ku8aG8E1MxnHA-6nH4at
4.gitlab仓库准备
http://192.168.128.128:8088/Goland/gotogit.git

go get gittest.xxxx.com/Goland/gotogit

5.gitlab仓库打tag(一定要)
git tag <tagName> //创建本地tag
git push origin <tagName> //推送到远程仓库
例如: v1.0.0
6.项目中 go.mod 中路径要与url路径一致
module gittest.xxxx.com/Goland/gotogit
7.gitlab主机下配置 ~/.netrc
linux and windows(通过bash执行)
cat >  ~/.netrc  <<EOF
machine gittest.xxxx.com login felix password ku8aG8E1MxnHA-6nH4at
EOF
#chmod 600 ~/.netrc

#windows执行(cmd)
#echo "machine gittest.xxxx.com login felix password ku8aG8E1MxnHA-6nH4at"  >  %HOMEPATH%\.netrc
8.go配置goprivate
GOPRIVATE=gittest.xxxx.com
9.安装调试
go get gittest.xxxx.com/Goland/gotogit
10.项目导入
package main

import "gittest.xxxx.com/Goland/gotogit"
func main()  {
    seanbao.SeanPrint()
}
11.加餐

如果不知道go get具体执行的步骤,那么就自己根据报错,curl去测试。你看回应信息。

curl  -s https://gittest.xxxx.com/Goland/gotogit?go-get=1
参考:

Gitlab下私有Go modules的搭建配置

Using "go get" on a personal git repo

https://docs.gitlab.com/ee/user/packages/go_proxy/