构建和测试 Go 应用

本页介绍如何使用 Cloud Build 构建、测试和部署 Go 应用。

准备工作

本页面的说明假定您熟悉 Go。此外:

  • 启用 Cloud Build, Cloud Run, and Artifact Registry API。

    启用 API

  • 如需运行此页面中的 gcloud 命令,请安装 Google Cloud CLI
  • 准备好 Go 项目。
  • 如果您要使用 Cloud Build 将 Go 应用容器化,将需要 Dockerfile 以及源代码。
  • 如果您要在 Artifact Registry 中存储构建的容器,请在 Artifact Registry 中创建 Docker 代码库
  • 如果您要在 Cloud Storage 中存储测试日志,请在 Cloud Storage 中创建存储桶

必需的 IAM 权限

如需了解如何授予这些角色,请参阅使用 IAM 页面授予角色

配置 Go 构建

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

本部分逐步演示了 Go 应用的构建配置文件示例。该部分包含构建应用、添加单元测试,以及测试通过后容器化和部署应用的构建步骤。

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

  1. 在项目根目录中,创建一个名为 cloudbuild.yamlCloud Build 配置文件

  2. 构建和测试:如果您已在应用中定义单元测试,则可以通过在构建步骤中添加以下字段将 Cloud Build 配置为运行测试:

    • name:将此字段的值设置为 golang 以将 Docker Hub 中的 golang 映像用于您的任务。
    • entrypoint:将此字段的值设置为 /bin/bash。这样,您就可以直接从构建步骤运行多行 bash 命令
    • args:构建步骤的 args 字段采用一系列参数,并将其传递给 name 字段引用的映像。在以下示例中,args 字段采用参数进行:

      • 运行测试日志格式化程序以下载测试日志输出。
      • 打印日志输出。
      • 将测试结果保存在 sponge.log 中。
      • sponge.log 中的结果输出到 JUNIT XML 文件。JUNIT XML 文件的名称是使用与您的构建相关联的提交 ID 的短版本构建的。后续构建步骤会将此文件中的日志保存到 Cloud Storage。
      steps:
        # Run tests and save to file
        - name: golang:1.21
          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
  3. 将应用容器化:添加构建步骤以确保测试通过后,您可以构建应用。Cloud Build 提供预构建的 Docker 映像,可用于将 Go 应用容器化。如需将您的应用容器化,请在构建步骤中添加以下字段:

    • name:将此字段的值设置为 gcr.io/cloud-builders/docker,以将预构建的 Docker 映像用于您的任务。
    • args:为 docker build 命令添加参数作为此字段的值。

    以下构建步骤会构建映像 myimage,并使用提交 ID 的短版本来标记该映像。该构建步骤使用替代变量作为项目 ID、代码库名称和短 SHA 值,因此这些值会在构建时自动替换。请注意,您需要在 Artifact Registry 中创建 Docker 代码库或拥有现有的代码库才能存储映像。

    # Docker Build
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '-t',
             'us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA', '.']
  4. 将容器推送到 Artifact Registry:您可以将构建的容器存储在 Artifact Registry 中,这是一种 Google Cloud 服务,可用于存储、管理和保护构建工件。为此,您需要在 Artifact Registry 中已有 Docker 代码库。如需配置 Cloud Build 以将映像存储在 Artifact Registry Docker 代码库中,请添加具有以下字段的构建步骤:

    • name:将此字段的值设置为 gcr.io/cloud-builders/docker,以便为您的任务使用官方 docker 构建器映像。
    • args:为 docker push 命令添加参数作为此字段的值。对于目标网址,输入要在其中存储映像的 Artifact Registry Docker 代码库。

    以下构建步骤会将您在上一步中构建的映像推送到 Artifact Registry:

    # Docker push to Google Artifact Registry
    - name: 'gcr.io/cloud-builders/docker'
      args: ['push', 'us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA']
  5. 将容器部署到 Cloud Run:如需在 Cloud Run 上部署映像,请添加包含以下字段的构建步骤:

    • name:将此字段的值设置为 google/cloud-sdk,以使用 gcloud CLI 映像调用 gcloud 命令,以在 Cloud Run 上部署映像。
    • args:为 gcloud run deploy 命令添加参数作为此字段的值。

    以下构建步骤会将之前构建的映像部署到 Cloud Run:

    # Deploy to Cloud Run
    - name: 'gcr.io/cloud-builders/gcloud'
      args: ['run', 'deploy', 'helloworld-${SHORT_SHA}',
             '--image=us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA',
             '--region', 'us-central1', '--platform', 'managed']
  6. 将测试日志保存到 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.21
        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
    
      # Docker Build
      - name: 'gcr.io/cloud-builders/docker'
        args: ['build', '-t',
               'us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA', '.']
    
      # Docker push to Google Artifact Registry
      - name: 'gcr.io/cloud-builders/docker'
        args: ['push', 'us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA']
    
      # Deploy to Cloud Run
      - name: 'gcr.io/cloud-builders/gcloud'
        args: ['run', 'deploy', 'helloworld-${SHORT_SHA}',
               '--image=us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA',
               '--region', 'us-central1', '--platform', 'managed']
    
    # Save test logs to Google Cloud Storage
    artifacts:
      objects:
        location: gs://$_BUCKET_NAME/
        paths:
          - ${SHORT_SHA}_test_log.xml
    # Store images in Google Artifact Registry
    images:
      - us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA
  7. 使用 gcloud CLI构建触发器启动构建。开始构建时,您必须指定 Artifact Registry 代码库名称。

    如需使用 gcloud CLI 启动构建时指定 Artifact Registry 代码库,请执行以下操作:

    gcloud builds submit --region=us-west2 --config=cloudbuild.yaml \
        --substitutions=_REPO_NAME="REPO_NAME"
    

    REPO_NAME 替换为您的 Artifact Registry 代码库的名称。

    如需在使用构建触发器进行构建时指定 Artifact Registry 代码库,请在创建构建触发器时,在替代变量字段中指定 Artifact Registry 代码库的名称。

后续步骤