如何使用私有仓库的 Go Modules

时间:2022-07-25
本文章向大家介绍如何使用私有仓库的 Go Modules,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文我们将讨论 go modules 的基本用法,以及如何创建自己的 go modules,如何在自己的 go 项目中引用私有 git 仓库的模块。模块大大提高了 go 的代码可维护性,它是 golang 官方提供的依赖管理工具,就像 Java 应用中的 Maven 一样,如果你对 go modules 了解不多,可以查看官方文档相关介绍。

创建 go module

在 Java 中,一个制品可以通过 Nexus 这样的仓库来维护的,同样,我们可以将自己的 go modules 托管在 Git 或 Bitbucket 上,比如我们这里使用非常方便的 GitHub。创建一个如下所示的目录,在目录中初始化 modules:

$ mkdir common-module
$ cd common-module

在项目中初始化 go 模块的名称,这里的名称格式为 <hosting-site>/<repository>/<module>

  • hosting-site: 就是 github.com
  • repository: 自己的 github 帐号名(ereshzealous)
  • module:模块名称 common-module

所以最后 go 模块的名称就是 github.com/ereshzealous/common-module ,用 go mod init 初始化:

$ go mod init github.com/ereshzealous/common-module

接下来我们在这个模块中创建一个简单的 go 文件,添加几个示例函数:

package common

// GetCommonData a method
func GetCommonData() string {
 return "Common Data"
}

func GetData() string {
 return "Data"
}

然后将代码提交到 GitHub 上,并将其发布为一个版本。

发布后我们就可以在其他 go 项目中使用上面的 go modules 了,由于这个项目是公开的,所以所有人都可以使用,接下来我们将这个公开版本转变成私有仓库。

开发环境

如果上面的 go modules 已经变成私有仓库了,这个时候我们使用go get 命令来获取这个模块,会得到一个 410 Gone 的错误信息:

要解决上述问题,在 go 项目中引入私有模块可以用如下所示的方式。

1. 通过 Go Mod 代理网站

Go Module 的代理站点默认的 repo 是 https://proxy.golang.org/,direct ,由于我们的模块发布在 github 上,所以我们需要提供一种机制来绕过代理站点的私有仓库,这里可以使用 GOPRIVATE 来实现,GOPRIVATE 环境变量用来表示不对外公开的模块路径。

在开发环境中我们可以按照如下命令设置GOPRIVATE,多个值用逗号隔开。下面设置的是我的账户级别,我们也可以设置成仓库级别,比如 github.com/ereshzealous/common-module

$ go env -w GOPRIVATE=github.com/ereshzealous

2. 在构建过程中传递仓库凭证

由于我们使用的是 Github,所以我们在构建过程中需要提供 auth token。在开发环境中,这很简单,有一个 git 命令来实现这个功能,在 gitconfig 文件中添加一个配置即可。

点击这里进入 GitHub 访问令牌页面,创建一个具有相应权限的 token,创建一个新的 token 或使用一个现有的 token 都可以。

在生成 token 后,执行以下命令。

Github

$ git config --global url."https://${username}:${access_token}@github.com".insteadOf /
"https://github.com"

Bitbucket

在 Bitbucket 中,同样创建一个访问 token 并执行以下命令。

$ git config --global url."https://${bitbucket_user_id}:${bitbucket_access_token}@privatebitbucket.com".insteadOf 
  "https://privateaccount.com"

gitconfig 中进行上述设置后,我们再来尝试使用 go modules。

我们可以看到是可行的,当然对于 GitLab 私有仓库也是一样的操作方式,这对于开发环境来说都很方便,那么对于 CI/CD 或者 Docker 容器化环境应该怎么办呢?

Docker

其实对于 Docker 容器中,私有私有 go modules 也很方便,我们可以直接在 Dockerfile 中配置私有仓库,如下所示:

# Start from the latest golang base image
FROM golang:alpine

RUN GOCACHE=OFF

RUN go env -w GOPRIVATE=github.com/ereshzealous

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy everything from the current directory to the Working Directory inside the container
COPY . .

RUN apk add git

RUN git config --global url."https://golang:<access-token>@github.com".insteadOf "https://github.com"

# Build the Go app
RUN go build -o main .

# Expose port 8080 to the outside world
EXPOSE 8080

在上面的 Dockerfile 中,第6行我们通过环境变量 GOPRIVATE 设置了私有仓库地址;在第17行通过 gitconfig 设置了一个访问 token 和用户名,这会把凭证传递给 go mod,为了使用 git 命令,还特地在镜像中添加了 git 命令。

上面构建过程中的 pvt 就是一个私有的 go mod 仓库,可以看到正常获取。当然在 CI/CD 的流水线当中也可以直接使用这个方式。

参考链接

  • https://medium.com/swlh/go-modules-with-private-git-repository-3940b6835727
  • https://blog.golang.org/using-go-modules