在我們使用golang開發項目時,會遇到私有倉庫問題,本文章講解golang中私有倉庫的使用
本文由 簡悅 SimpRead 轉碼, 原文地址 http://holys.im/2016/09/20/go-get-in-gitlab/
據此 issue,gitlab 7.8 就開始支持 go get private repo。
假設 gitlab 服務是: mygitlab.com
使用方式:
$ go get -v mygitlab.com/user/repo
如果 mygitlab.com 不支持 https, 還得加上 -insecure 參數
$ go get -v -insecure mygitlab.com/user/repo
但是 -insecure 參數是 go 1.5 以后才有的,所以如果低於 1.5 版本,趕緊升級一下吧。
默認需要輸入用戶名和密碼,比較繁瑣。 由於 go get 底層實際還是用了 git 去操作。可以配置 .gitconfig 使之用 http => ssh 的訪問方式 (個人感覺就是重寫了 url)
$ git config --global url."git@mygitlab.com:".insteadOf "http://mygitlab.com/"
// 其實就是在 .gitconfig 增加了配置
$ cat ~/.gitconfig
[url "git@mygitlab.com:"]
insteadOf = http://mygitlab.com/
注意: git@mygitlab.com: 后面有個冒號 :, 且 http://mygitlab.com 后面有 /
另外: url.”aaa”.insteadOf “bbb” 是個相當有用的技巧,對 github 同樣適用。
$ git config --global url."git@github.com:".insteadOf "https://github.com/"
$ cat ~/.gitconfig
[url "git@github.com:"]
insteadOf = https://github.com/
這樣 git clone https://github.com/golang/go.git 就變成 git clone git@github.com:golang/go.git , 免去輸入賬號密碼的煩惱。
總結:
gitlab >= 7.8
go >= 1.5 (如果 gitlab 不支持 https)
git url insteadOf
參考:
go get如何下載私有倉庫
因為工作需要,我們用得都是自己搭建得git倉庫,go get無法下載私有倉庫,
go get -insture 倒是可以下載,但是在go mod下無法自動
網上看了一大堆文章,各種方法說得亂七八糟,不頂用,最終解決方法如下
1、代碼倉庫一定要使用https(非常重要)
2、使用https后go get就自動會給你提示了
fatal: could not read Username for 'https://xxx.com': terminal prompts disabled
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
看到重點沒有,go開發人員的工作還是做得比較仔細,連文檔地址都告訴我們了,哈哈😄
打開https://golang.org/doc/faq#git_https查看
這一步是不是感覺又撞牆了,無法打開😂,但是沒關系,我們有 godoc,執行
godoc -http :1088
瀏覽器輸入http://localhost:1088/doc/faq#git_https
查看文檔如下
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
總結這段話就是說在 你的用戶根目錄下的.netrc文件加上git倉庫登錄賬號就行了,是不是很簡單
echo "machine 倉庫地址 login 用戶名 password 密碼" > ~/.netrc
ok,再次執行go get 一切順利
作者:悟道人
鏈接:https://www.jianshu.com/p/e48621f71e9b
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。