在 Artifact Registry 中存储 Go 模块
设置专用 Artifact Registry Go 代码库,向其中上传模块,并将该模块用作依赖项。
准备工作
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安装 Google Cloud CLI。
-
配置 gcloud CLI 以使用您的联合身份。
如需了解详情,请参阅使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - 安装 Go1.15 或更高版本。
- 安装
package-go-modulegcloud CLI 插件:
gcloud components install package-go-module
所需的角色
如需获得创建和管理 Artifact Registry Go 模块代码库所需的权限,请让管理员为您授予项目的Artifact Registry Administrator (roles/artifactregistry.admin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
创建代码库
如需创建和配置新代码库,请执行以下操作:
运行以下命令创建新代码库。
gcloud artifacts repositories create REPOSITORY \ --repository-format=go \ --location=LOCATION \ --description=DESCRIPTION替换以下内容:
运行以下命令以查看代码库详细信息。
gcloud artifacts repositories describe --location=LOCATION REPOSITORY
输出将类似以下内容:
Encryption: Google-managed key Repository Size: 0.000MB createTime: '2022-06-03T20:20:01.644813Z' format: GO mode: STANDARD_REPOSITORY name: projects/my-project/locations/us-west1/repositories/my-repo updateTime: '2022-06-03T20:20:01.644813Z'
设置 gcloud 命令的默认值
您可以为项目、代码库和位置值配置默认值,从而简化 gcloud CLI 命令。配置默认值后,就不再需要 --project、--location 和 --repository 标记。
打包并上传 Go 模块
package-go-module gcloud CLI 插件会打包您的 Go 模块,让您可以使用 gcloud 命令对模块进行版本控制并将其上传到 Artifact Registry。
创建 Go 模块
首先,创建一个简单的 Go 模块以上传到您的代码库。
在您的主目录中,为您的模块创建一个名为“foo”的目录
mkdir foo
将目录更改为模块的目录,然后运行
go mod init以创建 模块的 go.mod 文件。cd foo \ go mod init example.com/foo
将
example.com/foo替换为模块路径。如需了解详情,请参阅 Go 模块参考文档 。在 foo 目录中创建一个
foo.go文件,其中包含以下内容:package foo const HelloWorld = "Hello World!"
打包并上传模块
将模块打包并上传到您的代码库:
gcloud artifacts go upload --project=PROJECT \
--repository=REPOSITORY \
--location=LOCATION \
--module-path=example.com/foo \
--version=VERSION \
--source=SOURCE_LOCATION
替换以下内容:
- PROJECT 替换为您的 Google Cloud 项目 ID。
- REPOSITORY 替换为存储软件包的代码库的名称。
- LOCATION 替换为代码库的区域或多区域 位置 。
example.com/foo替换为模块路径。如需了解详情,请参阅 Go 模块参考文档 。- VERSION 替换为
模块的语义化版本
,格式为
vX.Y.Z,其中X是主要版本,Y是 次要版本,Z是补丁程序版本。 - SOURCE_LOCATION 替换为 Go
模块的根目录的路径。如果您省略
--source标志,则默认值为当前目录。
该模块已上传到 Artifact Registry。
如需详细了解如何创建 Go 模块,请参阅本 教程。
列出模块
配置默认值后,运行以下命令以检查默认 项目、代码库和位置中上传的 Go 模块:
gcloud artifacts packages list
输出将类似以下内容:
Listing items under project my-project, location us-west1, repository my-repo. PACKAGE CREATE_TIME UPDATE_TIME example.com/foo 2022-06-03T20:43:39 2022-06-20T20:37:40
查看模块版本详细信息
配置默认值后,运行以下命令以查看默认 项目、代码库和位置中模块的版本:
gcloud artifacts versions list --package=MODULE_PATH
输出将类似以下内容:
Listing items under project my-project, location us-west1, repository my-repo, package example.com/foo. VERSION DESCRIPTION CREATE_TIME UPDATE_TIME v0.1.0 2022-06-03T20:43:39 2022-06-03T20:43:39 v0.1.1 2022-06-20T20:37:40 2022-06-20T20:37:40
下载模块
如需导入存储在 Artifact Registry 中的模块,您需要指示 Go 来 从 Artifact Registry 查找依赖项,并绕过 校验和数据库。
设置 Go 环境
指示 Go 从 Artifact Registry、公共 Go 模块代理,然后按该顺序从来源下载模块:
export GOPROXY=https://LOCATION-go.pkg.dev/PROJECT/REPOSITORY,https://proxy.golang.org,direct
替换以下内容:
排除您的模块,使其不使用公共校验和数据库进行检查:
export GONOSUMDB=MODULE_PATH_REGEX
如果您要排除多个模块,请将 MODULE_PATH_REGEX 替换为模块路径或正则表达式 。
如需排除模块
example.com/foo,使其不使用公共校验和数据库进行检查,请运行以下命令:export GONOSUMDB=example.com/foo
如果您希望排除模块路径以
example.com开头的所有模块,使其不使用公共校验和数据库进行检查,请运行以下命令:export GONOSUMDB=example.com/*
向 Artifact Registry 进行身份验证
从 Artifact Registry 下载打包的 Go 模块以用作依赖项时,Go 二进制文件会使用 netrc 文件中的凭据向 Artifact Registry 进行身份验证。如需简化身份验证过程,您可以使用 Go 凭据帮助程序刷新 netrc 文件中的令牌,以便向 Artifact Registry 进行身份验证。
您可以使用 netrc 环境变量设置 netrc 文件的位置。
如果未设置 NETRC 变量,则 go 命令将在类 UNIX 平台上读取 $HOME/.netrc,或在 Windows 上读取 %USERPROFILE%\_netrc。
Artifact Registry 支持以下身份验证方法。
- 短期凭据(推荐)
- 使用 Artifact Registry Go 凭据帮助程序工具 更新 netrc 文件中的身份验证令牌。
- 使用服务帐号密钥
如果您无法在环境中将凭据用于身份验证,请使用此选项。将未加密的服务帐号密钥添加到 netrc 文件。
将 Go 凭据帮助程序添加到 GONOPROXY
在使用 Go 凭据帮助程序之前,您需要将其添加到 GONOPROXY 列表,以强制 Go 直接从 GitHub 下载它。如果您有其他模块希望直接从来源下载,则可以将其添加到以英文逗号分隔的列表中,如以下示例所示:
export GONOPROXY=MODULE_PATH1, MODULE_PATH2
其中,MODULE_PATH1 和 MODULE_PATH2 是要从来源下载的模块的模块路径。
如需将 Go 凭据帮助程序添加到 GONOPROXY 列表并运行它以设置凭据,请执行以下操作:
将 Go 凭据帮助程序添加到
GONOPROXYexport GONOPROXY=github.com/GoogleCloudPlatform/artifact-registry-go-tools
运行以下命令,使用 Go 模块软件包工具将 Artifact Registry 凭据添加到 netrc 文件:
GOPROXY=proxy.golang.org \ go run github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@v0.4.0 \ add-locations --locations=LOCATION \ [--json_key=path/to/service/account/key.json]其中,LOCATION 是代码库的区域或多区域 位置 。如需添加多个位置,请以英文逗号分隔的列表形式输入这些位置。
Go 凭据帮助程序会将设置添加到 netrc 文件,以便向 Artifact Registry 进行身份验证。如果您传递
--json_key标志,则会将密钥添加到 netrc 文件以进行密码身份验证。
将模块用作依赖项
如果您使用 短期凭据 向 Artifact Registry 进行身份验证 ,则需要运行 以下命令来刷新 OAuth 令牌:
GOPROXY=proxy.golang.org \ go run github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@v0.4.0 refresh
在您的主目录中,创建一个名为“bar”的目录
mkdir bar
将目录更改为模块的目录,然后运行
go mod init以为软件包创建go.mod文件。cd bar \ go mod init example.com/bar
将
example.com/bar替换为模块路径。如需了解详情,请参阅 Go 模块参考文档 。如需要求存储在 Artifact Registry 中的 foo 版本,请修改
go.mod文件,使其类似于以下内容:module example.com/bar go 1.19 require example.com/foo v0.1.0替换以下内容:
example.com/foo是所需模块的模块路径v0.1.0是存储在 Artifact Registry 中的版本
在
bar目录中创建一个main.go文件,其中包含以下内容:package main import ( "fmt" foo "example.com/foo" ) func main() { fmt.Println(foo.HelloWorld) }运行 go mod tidy 以下载依赖项,包括 foo 软件包:
go mod tidy
运行 bar 模块:
go run .
输出将类似以下内容:
Hello World!
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。在移除代码库之前,请确保您要保留的所有模块都位于其他位置。
如需删除代码库,请执行以下操作:
gcloud artifacts repositories delete \ --location=LOCATION \ --project=PROJECT \ REPOSITORY替换以下内容:
如果您要移除为活跃 gcloud 配置设置的默认代码库和位置设置,请运行以下命令:
gcloud config unset artifacts/repository gcloud config unset artifacts/location
后续步骤
- 详细了解如何配置身份验证。
- 了解如何管理代码库。
- 了解如何管理 Go 模块。
- 阅读我们关于 DevOps 的资源,并了解 DevOps 研究和评估 研究项目。