在 Artifact Registry 中存储 Go 模块
借助 Artifact Registry Go 制品库,您可以在各个项目中存储、版本控制和共享专用 Go 模块。在本快速入门中,您将创建一个代码库,打包并上传示例模块,然后将该模块作为依赖项导入。
准备工作
-
登录您的 Google 账号。
如果您还没有 Google 账号,请注册新账号。
-
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, if it is not already enabled.
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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
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, if it is not already enabled.
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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init - 安装最新主要版本或次新主要版本的 Go。不支持更早的版本。
- 安装
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 凭据帮助程序会将用于向 Artifact Registry 进行身份验证的设置添加到您的 netrc 文件中。如果您传递
--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 研究和评估研究项目。