cloud-build-basics
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoogle Cloud Build Basics
Google Cloud Build 基础
Prerequisites
前提条件
Before starting, ensure the following prerequisites are met:
- Google Cloud SDK: Ensure the Google Cloud SDK is installed and configured.
- Authentication: Authenticate the gcloud CLI:
bash
gcloud auth login gcloud auth application-default login - Project ID: Know the target Google Cloud Project ID. Set the context:
bash
gcloud config set project <PROJECT_ID> - Enable Cloud Build API: The Cloud Build API must be enabled for the project.
bash
gcloud services enable cloudbuild.googleapis.com - Permissions: Ensure the user or service account has the necessary permissions, such as and
roles/cloudbuild.builds.editor(to enable the API).roles/serviceusage.serviceUsageAdmin
开始之前,请确保满足以下前提条件:
- Google Cloud SDK:确保已安装并配置Google Cloud SDK。
- 身份验证:对gcloud CLI进行身份验证:
bash
gcloud auth login gcloud auth application-default login - 项目ID:了解目标Google Cloud项目ID,并设置上下文:
bash
gcloud config set project <PROJECT_ID> - 启用Cloud Build API:项目必须启用Cloud Build API。
bash
gcloud services enable cloudbuild.googleapis.com - 权限:确保用户或服务账号拥有必要权限,例如和
roles/cloudbuild.builds.editor(用于启用API)。roles/serviceusage.serviceUsageAdmin
Core Concepts
核心概念
Google Cloud Build (GCB) is a serverless platform that executes your builds on Google Cloud. It translates your source code into deployable artifacts, such as Docker containers or Java archives.
| Concept | Description |
|---|---|
| The required configuration file that defines the build steps. It is written in YAML or JSON. |
| Build Steps | A sequence of actions (steps) GCB performs. Each step runs a command inside a specific Docker container (the builder). Common builders include |
| Artifacts | The output of the build, typically a container image pushed to Google Container Registry (GCR) or Artifact Registry (AR), or other deployable files. |
| Triggers | Automation rules that invoke a build in response to an event, such as a push to a Git repository, a Pub/Sub message, or a manual request. |
Google Cloud Build(GCB)是一个无服务器平台,可在Google Cloud上执行构建任务。它将源代码转换为可部署工件,例如Docker容器或Java归档文件。
| 概念 | 说明 |
|---|---|
| 必填的配置文件,用于定义构建步骤,采用YAML或JSON格式编写。 |
| 构建步骤 | GCB执行的一系列操作(步骤)。每个步骤在特定Docker容器(构建器)内运行命令。常见构建器包括 |
| 工件 | 构建的输出,通常是推送到Google Container Registry(GCR)或Artifact Registry(AR)的容器镜像,或其他可部署文件。 |
| 触发器 | 响应事件(如Git仓库推送、Pub/Sub消息或手动请求)而触发构建的自动化规则。 |
Navigation: Viewing Build History
导航:查看构建历史
The Cloud Build Build History page is the central place to monitor the status of past and ongoing builds.
- Open the Cloud Console: Navigate to the Google Cloud Console.
- Go to Cloud Build: Use the search bar or the navigation menu to find Cloud Build.
- Select Build History: In the left navigation pane, select History (or use the direct URL: ).
https://console.cloud.google.com/cloud-build/builds - Review Builds:
- Status: Check the status column (,
SUCCESS,FAILURE,WORKING).QUEUED - Region: Use the region filter at the top to view builds that ran in a specific region (important for regional worker pools).
- Logs: Click on a specific Build ID to view the detailed logs, execution steps, and build summary. This is crucial for debugging failed builds.
- Status: Check the status column (
[!NOTE] If this is your first time visiting the page, you might see the "zero-state" experience, which offers options to run a sample build or create your first trigger (as noted in theskill). Note that region settings for triggers and builds are immutable after creation and must be chosen deliberately.cb-list-build-zero-state
Cloud Build构建历史页面是监控过往和正在进行的构建状态的中心位置。
- 打开Cloud控制台:导航至Google Cloud控制台。
- 进入Cloud Build:使用搜索栏或导航菜单找到Cloud Build。
- 选择构建历史:在左侧导航窗格中选择History(或使用直接URL:)。
https://console.cloud.google.com/cloud-build/builds - 查看构建信息:
- 状态:查看状态列(、
SUCCESS、FAILURE、WORKING)。QUEUED - 区域:使用顶部的区域过滤器查看在特定区域运行的构建(这对区域工作池很重要)。
- 日志:点击特定的构建ID查看详细日志、执行步骤和构建摘要。这对调试失败的构建至关重要。
- 状态:查看状态列(
[!NOTE] 如果是首次访问该页面,你可能会看到“零状态”界面,它提供了运行示例构建或创建第一个触发器的选项(如技能中所述)。请注意,触发器和构建的区域设置在创建后不可更改,必须谨慎选择。cb-list-build-zero-state
Creating a Basic Automated Trigger
创建基础自动化触发器
This process defines an automation rule to run a build whenever code is pushed to a specified Git branch.
此流程定义了一个自动化规则,每当代码推送到指定Git分支时就运行构建。
Step 1: Start Trigger Creation
步骤1:开始创建触发器
- Navigate to the Cloud Build Triggers page ().
https://console.cloud.google.com/cloud-build/triggers - Click Create trigger.
- 导航至Cloud Build触发器页面()。
https://console.cloud.google.com/cloud-build/triggers - 点击创建触发器。
Step 2: Configure Trigger Settings
步骤2:配置触发器设置
- Name: Provide a unique, descriptive name (e.g., ).
github-main-branch-build - Region: Select the region where the trigger configuration will be stored (e.g., or a specific regional endpoint). Note: Trigger and build region settings are immutable after creation and must be chosen deliberately.
global - Event: Select the event type. For automated CI/CD, select Push to a branch.
- Source: Select the repository source:
- Repository: Connect your source repository (GitHub, Bitbucket, Cloud Source Repositories, etc.). If needed, authorize the connection.
- Repository Name: Select the specific repository you want to link.
- Branch: Enter the branch pattern (e.g., or
^main$).^develop
- 名称:提供唯一且描述性的名称(例如)。
github-main-branch-build - 区域:选择存储触发器配置的区域(例如或特定区域端点)。注意:触发器和构建的区域设置在创建后不可更改,必须谨慎选择。
global - 事件:选择事件类型。对于自动化CI/CD,选择推送到分支。
- 源:选择仓库源:
- 仓库:连接你的源代码仓库(GitHub、Bitbucket、Cloud Source Repositories等)。如有需要,授权连接。
- 仓库名称:选择要链接的特定仓库。
- 分支:输入分支模式(例如或
^main$)。^develop
Step 3: Configure Build Settings
步骤3:配置构建设置
- Configuration: Select Cloud Build configuration file (yaml or json).
- Location: Keep the default Repository and specify the path to your build configuration file (e.g., ).
cloudbuild.yaml- Alternative: For very simple builds, you can choose Inline to paste the YAML configuration directly into the trigger.
- (Optional) Service Account: For production environments, select a dedicated service account with limited permissions to enforce the principle of least privilege.
- 配置:选择Cloud Build配置文件(yaml或json)。
- 位置:保留默认的仓库,并指定构建配置文件的路径(例如)。
cloudbuild.yaml- 替代方案:对于非常简单的构建,你可以选择内联,直接将YAML配置粘贴到触发器中。
- (可选)服务账号:对于生产环境,选择具有有限权限的专用服务账号,以遵循最小权限原则。
Step 4: Save and Test
步骤4:保存并测试
- Click Create. The trigger is now active and will run automatically on the next matching Git push.
[!TIP] Theskill provides detailedcb-create-triggercommands for creating triggers across all types (GitHub, Pub/Sub, Webhook) and configurations (inline, Dockerfile, YAML). Use that skill for CLI automation.gcloud
- 点击创建。触发器现在已激活,下次匹配Git推送时将自动运行。
[!TIP]技能提供了详细的cb-create-trigger命令,用于创建所有类型的触发器(GitHub、Pub/Sub、Webhook)和配置(内联、Dockerfile、YAML)。如需CLI自动化,请使用该技能。gcloud
Running an Existing Trigger Manually
手动运行现有触发器
Sometimes you need to run a trigger on demand, outside of its normal automation flow (e.g., to rebuild an old commit or test a new substitution).
[!IMPORTANT] Substitution Immutability: You can only override values for substitution variables that are already defined in the trigger configuration. You cannot introduce new substitution variable keys at runtime.
有时你需要按需运行触发器,而不遵循其正常自动化流程(例如,重新构建旧提交或测试新的替换变量)。
[!IMPORTANT] 替换变量不可变性:你只能覆盖已在触发器配置中定义的替换变量的值。无法在运行时引入新的替换变量键。
Option A: Via the Cloud Console
选项A:通过Cloud控制台
- Navigate to the Cloud Build Triggers page ().
https://console.cloud.google.com/cloud-build/triggers - Locate the trigger you wish to run.
- Click the vertical ellipsis (⋮) next to the trigger and select Run.
- A dialog will appear, allowing you to specify:
- Source branch/tag: Choose the specific Git reference to build from.
- Substitution Variables: Override any existing substitution variables (e.g., set to a new value).
_VERSION
- Click Run trigger. The build will start immediately, and you can monitor its status on the History page.
- 导航至Cloud Build触发器页面()。
https://console.cloud.google.com/cloud-build/triggers - 找到你要运行的触发器。
- 点击触发器旁边的垂直省略号(⋮),选择运行。
- 将弹出一个对话框,允许你指定:
- 源分支/标签:选择要构建的特定Git引用。
- 替换变量:覆盖任何现有替换变量(例如,将设置为新值)。
_VERSION
- 点击运行触发器。构建将立即启动,你可以在历史页面监控其状态。
Option B: Via the gcloud CLI
选项B:通过gcloud CLI
Use the command to invoke the trigger and optionally override parameters.
gcloud builds triggers runbash
undefined使用命令调用触发器,并可选择覆盖参数。
gcloud builds triggers runbash
undefinedRun the trigger against the 'main' branch
针对'main'分支运行触发器
gcloud builds triggers run <TRIGGER_NAME>
--region=<REGION>
--branch=main
--region=<REGION>
--branch=main
gcloud builds triggers run <TRIGGER_NAME>
--region=<REGION>
--branch=main
--region=<REGION>
--branch=main
Run the trigger and override a substitution variable
运行触发器并覆盖替换变量
gcloud builds triggers run <TRIGGER_NAME>
--region=<REGION>
--branch=main
--substitutions=_IMAGE_TAG="20231027-manual"
--region=<REGION>
--branch=main
--substitutions=_IMAGE_TAG="20231027-manual"
gcloud builds triggers run <TRIGGER_NAME>
--region=<REGION>
--branch=main
--substitutions=_IMAGE_TAG="20231027-manual"
--region=<REGION>
--branch=main
--substitutions=_IMAGE_TAG="20231027-manual"
Monitor the initiated build
监控已启动的构建
Note: The run command outputs the build ID. Use it to check status:
注意:run命令会输出构建ID。使用它检查状态:
gcloud builds log <BUILD_ID> --region=<REGION>
gcloud builds log <BUILD_ID> --region=<REGION>
> [!NOTE]
> The `cb-run-trigger` skill provides more complex invocation examples, including running against a specific commit SHA or using tags.
> [!NOTE]
> `cb-run-trigger`技能提供了更复杂的调用示例,包括针对特定提交SHA或使用标签运行。Related Skills
相关技能
- : Detailed CLI-focused instructions for creating all trigger types.
cb-create-trigger - : Advanced management of the Cloud Build dashboard and onboarding zero state.
cb-list-build-zero-state - : Comprehensive guide to manually running triggers using various
cb-run-triggeroptions.gcloud
- :针对创建所有触发器类型的详细CLI导向说明。
cb-create-trigger - :Cloud Build仪表板的高级管理和零状态入门。
cb-list-build-zero-state - :使用各种
cb-run-trigger选项手动运行触发器的综合指南。gcloud