在 Artifact Registry 中存储 Go 模块
设置私有 Artifact Registry Go 代码库,向其中上传模块,然后将该模块用作依赖项。
准备工作
- 
    
      
        
        Sign in to your Google Account.If you don't already have one, sign up for a new account. 
- 
    
    
      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
      (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 Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
      Install the Google Cloud CLI. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: 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
      (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 Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
      Install the Google Cloud CLI. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: gcloud init
- 安装 Go 1.15 或更高版本。
- 安装 package-go-modulegcloud CLI 插件:
 gcloud components install package-go-module 
- 运行以下命令创建新代码库。 - 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' 
- 在您的主目录中,为模块创建一个名为“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!"
- 将 PROJECT 替换为您的 Google Cloud项目 ID。
- 将 REPOSITORY 替换为存储相应软件包的代码库的名称。
- LOCATION 替换为代码库的单区域或多区域位置。
- 将 example.com/foo替换为模块路径。如需了解详情,请参阅 Go 模块参考。
- VERSION,其中包含模块的语义化版本(格式为 vX.Y.Z,其中X是主要版本,Y是次要版本,Z是补丁程序版本)。
- 将 SOURCE_LOCATION 替换为 Go 模块根目录的路径。如果您省略 --source标志,则默认值为当前目录。
- 指示 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 Go 凭据帮助程序工具更新.netrc 文件中的身份验证令牌。
- 使用服务账号密钥
- 如果您无法在环境中通过凭据进行身份验证,请使用此选项。将未加密的服务账号密钥添加到您的 .netrc 文件。 
- 将 Go 凭据帮助程序添加到 - GONOPROXY- export 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 
- 运行条状模块: - go run . - 输出将类似以下内容: - Hello World! 
- 如需删除代码库,请执行以下操作: - gcloud artifacts repositories delete \ --location=LOCATION \ --project=PROJECT \ REPOSITORY- 替换以下内容: 
- 如果要移除您为活跃的 gcloud 配置而配置的默认代码库和位置信息设置,请运行以下命令: - gcloud config unset artifacts/repository gcloud config unset artifacts/location 
- 详细了解如何配置身份验证。
- 了解如何管理代码库。
- 了解如何管理 Go 模块。
- 阅读我们关于 DevOps 的资源,并了解 DevOps 研究和评估研究项目。
创建代码库
如需创建和配置新代码库,请执行以下操作:
为 gcloud 命令设置默认值
您可以为项目、代码库和位置值配置默认值,从而简化 gcloud CLI 命令。配置默认值后,无需使用 --project、--location 和 --repository 标记。
打包并上传 Go 模块
package-go-module gcloud CLI 插件可将您的 Go 模块打包,让您可以使用 gcloud 命令进行版本控制并上传到 Artifact Registry。
创建 Go 模块
首先,创建一个简单的 Go 模块以上传到代码库。
打包并上传模块
将模块打包并上传到代码库:
  gcloud artifacts go upload --project=PROJECT \
      --repository=REPOSITORY \
      --location=LOCATION \
      --module-path=example.com/foo \
      --version=VERSION \
      --source=SOURCE_LOCATION
替换以下内容:
模块已上传到 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 环境
向 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 支持以下身份验证方法。
将 Go 凭据帮助程序添加到 GONOPROXY
在使用 Go 凭据帮助程序之前,您需要将其添加到 GONOPROXY 列表中,以强制 Go 直接从 GitHub 下载它。如果您有其他模块想要直接从来源下载,可以将其添加到以英文逗号分隔的列表中,如以下示例所示:
export GONOPROXY=MODULE_PATH1, MODULE_PATH2
其中,MODULE_PATH1 和 MODULE_PATH2 是要从源代码下载的模块的模块路径。
如需将 Go 凭据帮助程序添加到 GONOPROXY 列表并运行它来设置凭据,请执行以下操作:
将您的模块用作依赖项
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。在移除代码库之前,请确保已在其他位置保存了您要保留的任何模块。