shanghaipc 发表于 2018-1-11 23:43:31

Go的Get命令兼容公司Gitlab仓库的HTTP协议

  对于公司的私有Gitlab仓库,没有对https支持,在使用最新版本的go get命令时,需要使用-insecure参数来支持http,但如果导入的包里边依赖了需要https的仓库,就不好使了,折腾了一下,解决方案如下:
  一、为Gitlab添加SSH Keys
  1.生成 ssh keys
  ssh-keygen -t rsa -C "myname@mycompany.com"
  2.查看内容
  cat ~/.ssh/id_rsa.pub
  3.复制到剪贴板
  Windows
  

clip < ~/.ssh/id_rsa.pub  

  Mac
  

pbcopy < ~/.ssh/id_rsa.pub  

  Linux (requires xclip)
  

xclip -sel clip < ~/.ssh/id_rsa.pub  

  4.粘贴到Gitlab个人中心的SSH Keys Settings
  二、配置.gitconfig文件
  Windows
  

notepad C:\Users\{你的Windows用户名}\.gitconfig  

  Linux/Mac
  

vi ~/.gitconfig  

  写入
  

  insteadOf
= https://git.mygitlab.com  

  这里简化一下可以直接使用命令
  

git config --global url."git@git.mygitlab.com:".insteadOf "https://git.mygitlab.com"  

  三、验证
  go get git.mygitlab.com/myname/xxx.git
  然后到go的src目录就可以看到新增了git.mygitlab.com/myname/目录了,当然源码也在这个目录下
  考虑到https可是以后的趋势,所以还是推动一下公司尽快用上https的私有仓库吧。
  四、补充
  补充一下可能遇到的问题:
  1、提示信息:package git.mygitlab/myname/gopublic.git: no buildable Go source files in d:\gosrc\src\git.mygitlab.com\myname\gopublic.git
  出现这个提示其实表示你的文件已经下载成功了
  2、这种方法拉回来的包是不会下载包内的依赖的,需要自己补充。原因见上提示信息。
  3、在使用时要注意需要修改目录名,如
  d:\gosrc\src\git.mygitlab.com\myname\gopublic.git
  import时应该是这样
  import "git.mygitlab.com/myname/gopublic.git/redis"
  但实际因为代码里边引用的包路径是
  import "gopublic/redis"
  导致在go build时会报错,找不到包,所以要将
  d:\gosrc\src\git.mygitlab.com\myname\gopublic.git
  移动到
  d:\gosrc\src\gopublic
  以上是使用上要注意的地方
页: [1]
查看完整版本: Go的Get命令兼容公司Gitlab仓库的HTTP协议