Go Module访问私有Git仓库


本文摘自网络,作者,侵删。

【译文】原文地址
Go Module 极大地改进了Go中依赖的管理过程。如果您是Go模块的新手,希望阅读更多关于如何入门Go module内容,请查看官方文档
一旦配置正确,就可以很容易地从公共仓库引入特定版本的Go包。一个典型的例子如下所示:

module github.com/samplerepo/sampleproject

go 1.12

require (
    github.com/pkg/errors v0.8.0
    github.com/spf13/cobra v0.0.4
    github.com/spf13/viper v1.3.2
)

如果想扩展包的引入范围到私有代码库中,该如何处理呢?实际上也很简单,确保您的Go安装能够访问私有Git仓库即可。但是具体怎么做呢?

私有仓库

在底层,Go使用Git来获取指定版本的依赖模块。因此,无论Go运行在哪里(Docker容器或者笔记本电脑中)必须有权限访问私有存储库。
幸运的是,有一个Git命令可以解决这个问题。下面的命令将在.gitconfig文件中添加一个条目,告诉Git使用带有凭证格式的URL来访问标准的URL。使用私有token代替密码,是因为需要在纯文本当中存储的。关于这方面的讨论可以查看Stack Overflow。
导入须知:

认证token必须是URL编码的

以下gits使用反斜杠处理,在不同行中显示:

BitBucket
git config   --global   url."https://${bitbucket_id}:${bitbucket_token}@privatebitbucket.com".insteadOf   "https://privatebitbucket.com"

URL中私有bitbucket域名需要用私有的BitBucket服务器代替。

GitHub
git config   --global   url."https://${user}:${personal_access_token}@github.com".insteadOf   "https://github.com"
Gitlab
git config   --global   url."https://oauth2:${personal_access_token}@privategitlab.com".insteadOf   "https://privategitlab.com"

#or 

git config   --global   url."https://${user}:${personal_access_token}@privategitlab.com".insteadOf   "https://privategitlab.com"

同样需要使用私有Gitlab服务器替换URL中privategitlab.com。

这种配置方式对于本地开发很好用,但是在CI/CD流水线上面会怎么样呢?如下是一个Dockerfile的例子允许在构建时注入凭证:

# ---------------------------------------------------------------------
#  The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.12.1-stretch as builder

COPY . /app

# Add the keys
ARG bitbucket_id
ENV bitbucket_id=$bitbucket_id
ARG bitbucket_token
ENV bitbucket_token=$bitbucket_token

WORKDIR /app/cmd/webapp

RUN git config     --global     url."https://${bitbucket_id}:${bitbucket_token}@privatebitbucket.com/".insteadOf     "https://privatebitbucket.com/"

RUN GIT_TERMINAL_PROMPT=1     GOARCH=amd64     GOOS=linux     CGO_ENABLED=0     go build -v --installsuffix cgo --ldflags="-s" -o myapp

# ---------------------------------------------------------------------
#  The second stage container, for running the application
# ---------------------------------------------------------------------
FROM alpine:3.8
COPY --from=builder /app/cmd/webapp/myapp /app/myapp

WORKDIR /app

ENTRYPOINT ["/myapp"]

我喜欢使用docker compose,所以这里有一个例子,将使用它来运行Dockerfile:

version: '3.0'
services:
  app:
    container_name: my_go_app_container
    build:
      # context can/may/will be different per-project setup
      context: ../
      dockerfile: GitDockerfile
      args:
        - bitbucket_id=private_user
        - bitbucket_token=private_token
    image: my_go_app_image
    # other configs...

当然,Jenkins或Travis或者其他任何方式只要在构建Docker镜像时可提供build参数,这样Go模块就可以在不被讨厌的身份验证阻塞情况下完成其工作。

另一种选择:SSH

另一种设置方法是使用你的SSH密匙连接,并像下面这样设置你的.gitconfig确保每次引入包使用SSH:

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

我个人发现,当遇到问题时,这种设置调试很困难,因此我更偏向使用auth Token URL。
以上就是所有内容,感谢您的阅读,我希望这对您的Go模块之旅有帮助。


本文来自:简书

感谢作者:汪明军_3145

查看原文:Go Module访问私有Git仓库

相关阅读 >>

Gocn酷Go推荐】Go程序配置利器-viper库

使用viper读取nacos配置(开源)

[系列] Go - 常用签名算法的基准测试

方法集与表达式

手撸Golang Go与微服务 grpc

3.树莓派常用软件&篇程语言&篇程环境(32位/64位)

Go语言 if 语句嵌套

手撸Golang Go与微服务 saga模式之4

Golang中什么是接口

Golang中关于文件的操作

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...