本页面介绍如何使用 Cloud Build 构建和测试 Go 应用、将工件上传到 Artifact Registry、生成来源信息,以及将测试日志保存在 Cloud Storage 中。
准备工作
本页面的说明假定您熟悉 Go。此外:
-
Enable the Cloud Build, Cloud Run, and Artifact Registry APIs.
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. - 如需运行此页面中的
gcloud命令,请安装 Google Cloud CLI。 - 准备好 Go 项目。
- 在 Artifact Registry 中拥有 Go 代码库。如果您没有代码库,请创建一个新代码库。
- 如果您要在 Cloud Storage 中存储测试日志,请在 Cloud Storage 中创建存储桶。
- 确保您知道 Cloud Run 的运行时服务账号的 ID。
创建自定义 Cloud Build 服务账号
在 Google Cloud CLI 中运行以下命令,创建自定义 Cloud Build 服务账号:
gcloud iam service-accounts create cloud-build-go \
--description="Build and test Go applications" \
--display-name="Cloud Build Go" \
--project="PROJECT_NAME"
您将使用此服务账号构建和测试 Go 应用。
配置 IAM 权限
如需为新服务账号配置构建和部署 Go 应用所需的权限,请执行以下操作:
-
在 Google Cloud 控制台中,前往 settings Cloud Build 权限页面:
前往服务账号菜单,然后选择您的
cloud-build-go服务账号。将以下角色的状态设置为已启用:
- Cloud Run Admin (
roles/run.admin):允许 Cloud Build 将新服务部署到 Cloud Run。- 在“分配服务账号用户角色”面板中,选择您的运行时服务账号,然后点击授予权限。 此配置允许您的自定义 Cloud Build 服务账号在与 Cloud Run 代管式服务互动时模拟运行时服务账号。如需了解详情,请参阅为受管服务配置 Cloud Build 服务账号模拟。
- Storage Admin (
roles/storage.admin):可用于从 Cloud Storage 读取数据和向 Cloud Storage 写入数据。 - Artifact Registry Writer (
roles/artifactregistry.writer):允许从 Artifact Registry 拉取映像以及向 Artifact Registry 写入映像。 - 日志写入者 (
roles/logging.logWriter):允许将日志条目写入 Cloud Logging。 - Cloud Build Editor (
roles/cloudbuild.builds.editor):允许您的服务账号运行 build。
- Cloud Run Admin (
配置 Go 构建
Docker Hub 中的公共 golang 映像支持使用 Go 模块进行构建。将此映像用作 Cloud Build 配置文件中的构建步骤,这样您就可以调用映像中的 go 命令。传递到此构建步骤的参数会直接传递到 golang 工具,让您可以在此映像中运行任何 go 命令。
本部分展示了如何从 cloud-build-samples Git 代码库中为 Go 应用创建示例 build 配置文件。构建配置文件包含以下步骤:构建应用、添加单元测试,以及测试通过后部署应用。
如需构建示例 Go 应用,请执行以下操作:
配置单元测试:如果您已在应用中定义单元测试,则可以通过在构建步骤中添加以下字段将 Cloud Build 配置为运行测试:
name:将此字段的值设置为golang以将 Docker Hub 中的 golang 映像用于您的任务。entrypoint:将此字段的值设置为/bin/bash。这样,您就可以直接从构建步骤运行多行 bash 命令。args:构建步骤的args字段采用一系列参数,并将其传递给name字段引用的映像。在以下示例中,args字段采用参数进行:- 运行测试日志格式化程序以下载测试日志输出。
- 打印日志输出。
- 将测试结果保存在
sponge.log中。 将
sponge.log中的结果输出到 JUNIT XML 文件。JUNIT XML 文件的名称是使用与您的 build 相关联的提交 ID 的短版本构建的。后续构建步骤会将此文件中的日志保存到 Cloud Storage。steps: # Run tests and save to file - name: golang:1.23 entrypoint: /bin/bash args: - -c - | go install github.com/jstemmer/go-junit-report/v2@latest 2>&1 go test -timeout 1m -v ./... | /go/bin/go-junit-report -set-exit-code -iocopy -out ${SHORT_SHA}_test_log.xml
上传到 Artifact Registry:在配置文件中,使用
goModules字段指定应用路径和 Artifact Registry 中的 Go 代码库:# Upload Go module to artifact registry artifacts: goModules: - repositoryName: 'repositoryName' repositoryLocation: 'location' repositoryProjectId: 'projectId' sourcePath: 'sourcePath' modulePath: 'appPath' moduleVersion: 'version'替换以下值:
可选:启用来源生成功能
Cloud Build 可以生成可验证的软件制品的供应链等级 (SLSA) 构建来源元数据,以帮助保护您的持续集成流水线。
如需启用来源生成,请将
requestedVerifyOption: VERIFIED添加到配置文件中的options部分。构建完成后,您可以在 Artifact Registry 中查看代码库详情。
您还可以查看 build 来源元数据和验证来源。
将测试日志保存到 Cloud Storage:您可以配置 Cloud Build,以便通过指定现有存储桶位置和测试日志的路径在 Cloud Storage 中存储所有测试日志。
以下构建步骤会将保存在 JUNIT XML 文件中的测试日志存储到 Cloud Storage 存储分区:
# Save test logs to Google Cloud Storage artifacts: objects: location: gs://$_BUCKET_NAME/ paths: - ${SHORT_SHA}_test_log.xml以下代码段展示了针对上述步骤的完整构建配置文件:
steps: # Run tests and save to file - name: golang:1.23 entrypoint: /bin/bash args: - -c - | go install github.com/jstemmer/go-junit-report/v2@latest 2>&1 go test -timeout 1m -v ./... | /go/bin/go-junit-report -set-exit-code -iocopy -out ${SHORT_SHA}_test_log.xml # Store golang modules in Google Artifact Registry artifacts: goModules: - repositoryName: 'repositoryName' repositoryLocation: 'location' repositoryProjectId: 'projectId' sourcePath: 'sourcePath' modulePath: 'appPath' moduleVersion: 'version'使用 gcloud CLI 启动 build,或创建build 触发器:
Google Cloud CLI
gcloud builds submit --region=us-west2 --config=cloudbuild.yaml \
--substitutions=_AR_REPO_NAME="AR_REPO_NAME"
构建触发器
按照创建 build 触发器中的步骤操作。 在替换变量字段中,您还必须提供您的 Artifact Registry 制品库的名称以及用于测试日志的 Cloud Storage 存储桶的名称。
后续步骤
- 了解如何在 Compute Engine 上执行蓝绿部署。
- 了解如何排查构建错误。