构建和测试 Go 应用

本页面介绍如何使用 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  • 如需运行此页面中的 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 应用所需的权限,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往 Cloud Build 权限页面:

    前往权限

  2. 前往服务账号菜单,然后选择您的 cloud-build-go 服务账号。

  3. 将以下角色的状态设置为已启用

    • Cloud Run Admin (roles/run.admin):允许 Cloud Build 将新服务部署到 Cloud Run。
    • 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。

配置 Go 构建

Docker Hub 中的公共 golang 映像支持使用 Go 模块进行构建。将此映像用作 Cloud Build 配置文件中的构建步骤,这样您就可以调用映像中的 go 命令。传递到此构建步骤的参数会直接传递到 golang 工具,让您可以在此映像中运行任何 go 命令。

本部分展示了如何从 cloud-build-samples Git 代码库中为 Go 应用创建示例 build 配置文件。构建配置文件包含以下步骤:构建应用、添加单元测试,以及测试通过后部署应用。

如需构建示例 Go 应用,请执行以下操作:

  1. 配置单元测试:如果您已在应用中定义单元测试,则可以通过在构建步骤中添加以下字段将 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
        
  2. 上传到 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'
    

    替换以下值:

    • repositoryName:您的 Artifact Registry Go 代码库的名称。
    • location:Artifact Registry 中代码库的位置
    • projectId:包含 Artifact Registry 制品库的 Google Cloud 项目的 ID。
    • sourcePath:build 工作区中 go.mod 文件的路径。
    • appPath:打包应用的路径。
    • version:应用的版本号,以数字和英文句点表示,例如 v1.0.1
  3. 可选:启用来源生成功能

    Cloud Build 可以生成可验证的软件制品的供应链等级 (SLSA) 构建来源元数据,以帮助保护您的持续集成流水线。

    如需启用来源生成,请将 requestedVerifyOption: VERIFIED 添加到配置文件中的 options 部分。

    构建完成后,您可以在 Artifact Registry 中查看代码库详情

    您还可以查看 build 来源元数据验证来源

  4. 将测试日志保存到 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'
    
  5. 使用 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 存储桶的名称。

后续步骤