配置使用gitlab作为模块仓库

1010阅读 0评论2022-01-10 khls27
分类:Web开发

go mod 默认使用https

    go mod 默认使用https作为代码管理的交互协议。其原因:其一,为了保证访问的兼容性,采用http通用性最好;其二,https也能有效的保证传输安全。
    以下是go官网中A&Q中,对于该问题的回答:

点击(此处)折叠或打开

  1. Why does "go get" use HTTPS when cloning a repository?
  2. 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.

  3. 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:

  4. machine github.com login USERNAME password APIKEY
  5. For GitHub accounts, the password can be a personal access token.

  6. 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:

  7. [url "ssh://git@github.com/"]
  8.     insteadOf = https://github.com/

    但是,该行为在使用私有仓库作为模块管理时(公司内部联合开发,必然有此需求),go get通常是不能够正确下载源码的。原因是,通常私有库在git访问时,都需要登录。

解决方法

    其一,采用通过git 配置,让访问私有库的git请求,自动带上用户名和密码

点击(此处)折叠或打开

  1. 其核心
  2. git clone http://[username]:[password]@[your-repo].gitlab.com/[group]/[project].git

     其二,在github/gitlab上配置 Personal Access Token,并且在本地git 配置中添加配置,用http访问时,自动添加token

点击(此处)折叠或打开

  1. [color]
  2.     ui = auto
  3. [alias]
  4.     st = status
  5.     co = checkout
  6.     ci = commit
  7.     br = branch
  8. [core]
  9.     editor = vim

  10. [http]
  11.     extraheader = PRIVATE-TOKEN: gGXLa1j8JpPxjjMvY5X7
    其三,使用ssh协议替代http[s]协议

点击(此处)折叠或打开

  1. git config --global url."git@[your-repo]".insteadOf "https://[your-repo]"

    通常,公司有自己的gitlab服务器,每个人都上传了ssh key的,因此更推荐使用ssh协议替换https协议作为内部go 模块的交互协议。

特别提醒

    在国内,github通常存在访问问题,因此,go开发者都需要配置proxy来下载包。当设置了proxy之后,对于采用自建的代码服务需要避开代理,否则无法正常访问。新版本go支持一个环境变量GOPRIVATE 来避免该问题。设置如下:

点击(此处)折叠或打开

  1. go env -w GOPRIVATE="*[your-repo]"

上一篇:一文读懂内核spin lock接口原理及使用
下一篇:后端架构师成长之路——后端开发roadmap