<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Go module 使用 Gitlab 私有倉庫

          共 3801字,需瀏覽 8分鐘

           ·

          2022-03-19 02:42

          本文介紹了 Gitlab 私有倉庫建設的實踐。

          包管理是 Go 一直被詬病做的不好的功能。在 1.11 之前,go get 缺乏對依賴包版本管理和 reproducible build 的支持。當時在 Go 社區(qū)當時誕生了許多好用的工具,比如 glide,dep 等。在 1.11 版本之后, Go 引入了 Go Module,再也沒有 GOPATH 的限制,你可以隨意在任何路徑寫項目,但是此時對私有倉庫的支持還不是很好。而在 1.13 版本之后, Go 對 Go Module 又進行了優(yōu)化,支持了?GOPRIVATE?環(huán)境變量,可以指定私有倉庫的地址,使用十分便捷。大家在使用過程中,或多或少地會遇到一些問題,下面我針對自己遇到的問題進行總結。

          go get

          如果在沒有進行任何設置的情況下直接執(zhí)行?go get your.gitlab.com/pkg/example,你很可能會遇到以下錯誤:

          go?get:?module?your.gitlab.com/pkg/example:?git?ls-remote?-q?origin?in?/go/pkg/mod/cache/vcs/a39fc2dbfb0a9645950d24df5d7e922bb7a6a877aecfe2b20f74b96385a83109:?exit?status?128:
          ????fatal:?could?not?read?Username?for?'https://your.gitlab.com':?terminal?prompts?disabled
          Confirm?the?import?path?was?entered?correctly.
          If?this?is?a?private?repository,?see?https://golang.org/doc/faq#git_https?for?additional?information.

          其實錯誤提示已經把解決方案給到我們了,我們只需要點擊 golang.org/doc/faq#git_https 查看即可。

          下面是原文:

          Why does “go get” use HTTPS when cloning a repository?

          Companies often permit outgoing traffic only on the standard TCP ports 80 (HTTP) and 443 (HTTPS), blocking outgoing traffic on other ports, including TCP port 9418 (git) and TCP port 22 (SSH). When using HTTPS instead of HTTP, git enforces certificate validation by default, providing protection against man-in-the-middle, eavesdropping and tampering attacks. The go get command therefore uses HTTPS for safety.

          Git can be configured to authenticate over HTTPS or to use SSH in place of HTTPS. To authenticate over HTTPS, you can add a line to the $HOME/.netrc file that git consults:

          machine?github.com?login?USERNAME?password?APIKEY

          For GitHub accounts, the password can be a personal access token. Git can also be configured to use SSH in place of HTTPS for URLs matching a given prefix. For example, to use SSH for all GitHub access, add these lines to your ~/.gitconfig:

          [url?"ssh://[email protected]/"]
          ??????insteadOf?=?https://github.com/

          大概意思是,HTTPS 更安全,所以?go get?命令使用 HTTPS。

          如果你要用 HTTPS,那你就需要配置 HTTPS 的用戶名和密碼:

          machine?github.com?login?USERNAME?password?APIKEY

          當然也可以使用 ssh,需要修改你的 git 配置,

          修改當前用戶的?~/.gitconfig,添加:

          [url?"ssh://[email protected]/"]
          ??????insteadOf?=?https://your.gitlab.com/

          另外執(zhí)行下面的命令也能達到同樣的效果:

          git?config?--global?url."[email protected]/".insteadof?"https://your.gitlab.com/"

          操作完之后,我們就可以使用?go get?了,使用?go get -v?可以展示執(zhí)行日志。

          GONOPROXY

          眾所周知,國內用戶大多設置代理,我們在 Go 1.12 之前如果使用?GOPROXY?這個環(huán)境變量設置代理,并使用私有倉庫,很有可能會遇到下面的錯誤:

          go?get?your.gitlab.com/pkg/example:?module?your.gitlab.com/pkg/example:?
          reading?https://goproxy.cn/your.gitlab.com/pkg/example/@v/list:?404?Not?Found

          這是因為代理服務不可能訪問到我們的私有代碼倉庫,所以報錯 404。而且,就算使用上文提到的?ssh?鑒權也不行。

          Go 1.13 后可以設置?GONOPROXY?這個環(huán)境變量來指定不使用代理的域名,支持逗號分隔多個值。

          GONOSUMDB

          go mod 需要對下載后的依賴包進行 checksum 校驗,當你的 git 倉庫是開放的話沒問題,但是如果是不可訪問的私有倉庫,甚至在公司內網。很可能出現(xiàn)校驗失敗的錯誤:

          get?"your.gitlab.com/pkg/example":?found?meta?tag?get.metaImport{Prefix:"your.gitlab.com/pkg/example",?VCS:"git",?RepoRoot:"https://your.gitlab.com/pkg/example.git"}?at?//your.gitlab.com/pkg/example?go-get=1
          ??verifying?your.gitlab.com/pkg/example@v0.0.0:?your.gitlab.com/pkg/example@v0.0.0:?reading?https://sum.golang.org/lookup/your.gitlab.com/pkg/[email protected]:?410?Gone

          和代理一樣,我們的私有倉庫對 sum.golang.org 是不可見的,所以肯定沒辦法執(zhí)行安全校驗。

          同樣的在 Go 1.13 后可以設置?GONOSUMDB?環(huán)境變量指定跳過校驗的的域名,支持逗號分割多個值。

          GOPRIVATE

          最后 Go 1.13 還引入的?GOPRIVATE?環(huán)境變量,可以說設置后一勞永逸,能自動跳過 proxy server 和 校驗檢查, 這個變量值也支持逗號分割,可以填寫多個值,如:

          GOPRIVATE=*.corp.example.com,your.gitlab.com

          當然,設置?GOPRIVATE?之后,還可以在通過?GONOPROXY?和?GONOSUMDB?來單獨進行控制,

          不過需要注意下?GOPRIVATE?失效的問題,

          舉個例子,如果公司內部有私有倉庫:your.corp.com,如果這樣設置:

          GOPRIVATE=your.corp.com
          GOPROXY=https://goproxy.cn?
          GONOPROXY=none???

          因為?GONOPROXY?的值是?none,那么用戶還是會從?GOPROXY?的地址下載所有私有和共有的倉庫, 此時可能還是會報錯,GONOSUMDB?同理,大家注意一下這個問題。

          轉自:

          h1z3y3.me/posts/go-private-git-repository/

          文章轉載:進擊云原生

          (版權歸原作者所有,侵刪)


          點擊下方“閱讀原文”查看更多

          瀏覽 89
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大香伊人久久 | 青娱乐国产在线视频 | 北条麻妃日B视频 | 爽到高潮免费视频 | 五月丁香综合在线 |