preface

  • The team is currently using GitlabCE as a Golang code repository, and needs to separate some common code blocks (utility classes, RPC interfaces, etc.) into modules for other two-party system calls. This allows reuse of code blocks for easy extension and maintenance. Therefore, consider using Gitlab and Go mod to achieve private and square package management.

To prepare

  • Gitlab 13.10.3
  • Golang 1.16.3
  • Point to the HTTPS domain name of Gitlab. Here we simulate the domain name as my.gitlab.private
  • Nginx 1.18.0

link

The principle of

  • The download path of go GET is in the format of domain name + project path, and HTTPS is used by default. Therefore, we can use Nginx to build HTTPS service and forward the request to GitLab through HTTP to achieve two-party package download.

practice

Gitlab preparation

To enable the go proxy

  • Log in to the Gitlab-Rails console to enable the Go Proxy function

Prepare the GO Module project

  • Here assumes that project complete path for: my gitlab. Private/mytest/mysu…
  • The module name of our go.mod file will be set tomodule my.gitlab.private/mytest/mysubtest/myproject.git
    • Note that the name of ⚠️: Go Module must be consistent with the path of gitlab

  • Tag projects to distinguish versions

Configure the user’s AccessToken

  • Note that ⚠️:Scopes select API; The user must have access to the warehouse above.

Nginx configuration

HTTPS configuration

 server {
        listen    443 ssl;
        server_name  my.gitlab.private;
        ssl_certificate     /etc/nginx/conf.d/gitlab-ssl.crt;
        ssl_certificate_key   /etc/nginx/conf.d/gitlab-ssl.key;

        ssl_session_timeout  5m;
        ssl_ciphersECDHE-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;

        location / {
            proxy_pass http://127.0.0.1:80; #gitlab HTTP address
            if ($args ~* "^go-get=1") {
                set $condition goget;
            }
            if ($uri ~ ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/.*$) {
                set $condition "${condition}path";   
            }
            if ($condition = gogetpath) {
             return 200 "<! DOCTYPE html><html><head><meta content='my.gitlab.private/The $1/$2 git https://my.gitlab.private/The $1/$2.git' name='go-import'></head></html>"; }}}Copy the code
  • The configuration is successful when you can reach gitlab’s page by accessing my.gitlab.private from your browser.

Golang environment configuration

Golang global environment configuration

The environment variable content
GO111MODULE on
GOPROXY goproxy.cn
GOPRIVATE my.gitlab.private
  • Use GOPRIVATE to get the private library from my.gitlab.private.

Configure the. Netrc file

  • This file is used by Go Get to access gitLab’s repository
  • Path for~/.netrc
  • Content as follows:machine <url> login <username> password <token>

  • Such as:machine my.gitlab.private login admin password fqq3d7LsMxxa-hoK_sNvk

Test dependency package acquisition

  • Run the command:go get my.gitlab.private/mytest/mysubtest/myproject.git
  • Open the $GOPATH/PKG/mod/my gitlab. Private, you can see the private library dependence has been downloaded to the local

conclusion

  • Go Get requires domain names. Intranet users need to set up DNS resolution services on the Intranet
  • When you go get, the last.git cannot be ignored

reference

  • Blog.zhaoweiguo.com/2019/09/24/…