Google Cloud Build Basics
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:
gcloud auth login
gcloud auth application-default login
- Project ID: Know the target Google Cloud Project ID. Set the context:
gcloud config set project <PROJECT_ID>
- Enable Cloud Build API: The Cloud Build API must be enabled for the project.
gcloud services enable cloudbuild.googleapis.com
- Permissions: Ensure the user or service account has the necessary permissions, such as
roles/cloudbuild.builds.editor and roles/serviceusage.serviceUsageAdmin (to enable the API).
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 |
cloudbuild.yaml |
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 gcr.io/cloud-builders/gcloud, gcr.io/cloud-builders/docker, and custom containers. |
| 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. |
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.
[!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 the cb-list-build-zero-state skill). Note that region settings for triggers and builds are immutable after creation and must be chosen deliberately.
Creating a Basic Automated Trigger
This process defines an automation rule to run a build whenever code is pushed to a specified Git branch.
Step 1: Start Trigger Creation
- Navigate to the Cloud Build Triggers page (
https://console.cloud.google.com/cloud-build/triggers).
- Click Create trigger.
Step 2: Configure Trigger Settings
- 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.,
global or a specific regional endpoint). Note: Trigger and build region settings are immutable after creation and must be chosen deliberately.
- 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.,
^main$ or ^develop).
Step 3: Configure Build Settings
- 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.
Step 4: Save and Test
- Click Create. The trigger is now active and will run automatically on the next matching Git push.
[!TIP]
The cb-create-trigger skill provides detailed gcloud commands for creating triggers across all types (GitHub, Pub/Sub, Webhook) and configurations (inline, Dockerfile, YAML). Use that skill for CLI automation.
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.
Option A: Via the Cloud Console
- 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
_VERSION to a new value).
- Click Run trigger. The build will start immediately, and you can monitor its status on the History page.
Option B: Via the gcloud CLI
Use the gcloud builds triggers run command to invoke the trigger and optionally override parameters.
# Run the trigger against the 'main' branch
gcloud builds triggers run <TRIGGER_NAME> \
--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"
# Monitor the initiated build
# Note: The run command outputs the build ID. Use it to check status:
# 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.
Related Skills
cb-create-trigger: Detailed CLI-focused instructions for creating all trigger types.
cb-list-build-zero-state: Advanced management of the Cloud Build dashboard and onboarding zero state.
cb-run-trigger: Comprehensive guide to manually running triggers using various gcloud options.
External Resources & Documentation
1---2name: cloud-build-basics3description: Teaches the fundamentals of Google Cloud Build (GCB). Covers core concepts, API enablement, console navigation to the Build History page, and the end-to-end workflow for creating and manually running a basic build trigger. Do not use for managing private pools or complex pipeline architectures.4---56# Google Cloud Build Basics78## Prerequisites910Before starting, ensure the following prerequisites are met:11121. **Google Cloud SDK**: Ensure the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install) is installed and configured.132. **Authentication**: Authenticate the gcloud CLI:14 ```bash15 gcloud auth login16 gcloud auth application-default login17 ```183. **Project ID**: Know the target Google Cloud Project ID. Set the context:19 ```bash20 gcloud config set project <PROJECT_ID>21 ```224. **Enable Cloud Build API**: The Cloud Build API must be enabled for the project.23 ```bash24 gcloud services enable cloudbuild.googleapis.com25 ```265. **Permissions**: Ensure the user or service account has the necessary permissions, such as `roles/cloudbuild.builds.editor` and `roles/serviceusage.serviceUsageAdmin` (to enable the API).2728## Core Concepts2930Google 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.3132| Concept | Description |33| :--- | :--- |34| **`cloudbuild.yaml`** | The required configuration file that defines the build steps. It is written in YAML or JSON. |35| **Build Steps** | A sequence of actions (steps) GCB performs. Each step runs a command inside a specific Docker container (the builder). Common builders include `gcr.io/cloud-builders/gcloud`, `gcr.io/cloud-builders/docker`, and custom containers. |36| **Artifacts** | The output of the build, typically a container image pushed to Google Container Registry (GCR) or Artifact Registry (AR), or other deployable files. |37| **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. |3839## Navigation: Viewing Build History4041The Cloud Build Build History page is the central place to monitor the status of past and ongoing builds.42431. **Open the Cloud Console**: Navigate to the Google Cloud Console.442. **Go to Cloud Build**: Use the search bar or the navigation menu to find **Cloud Build**.453. **Select Build History**: In the left navigation pane, select **History** (or use the direct URL: `https://console.cloud.google.com/cloud-build/builds`).464. **Review Builds**:47 * **Status**: Check the status column (`SUCCESS`, `FAILURE`, `WORKING`, `QUEUED`).48 * **Region**: Use the region filter at the top to view builds that ran in a specific region (important for regional worker pools).49 * **Logs**: Click on a specific Build ID to view the detailed logs, execution steps, and build summary. This is crucial for debugging failed builds.5051> [!NOTE]52> 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 the [`cb-list-build-zero-state`](references/cb-list-build-zero-state.md) skill). Note that region settings for triggers and builds are immutable after creation and must be chosen deliberately.5354## Creating a Basic Automated Trigger5556This process defines an automation rule to run a build whenever code is pushed to a specified Git branch.5758### Step 1: Start Trigger Creation59601. Navigate to the **Cloud Build Triggers** page (`https://console.cloud.google.com/cloud-build/triggers`).612. Click **Create trigger**.6263### Step 2: Configure Trigger Settings64651. **Name**: Provide a unique, descriptive name (e.g., `github-main-branch-build`).662. **Region**: Select the region where the trigger configuration will be stored (e.g., `global` or a specific regional endpoint). **Note: Trigger and build region settings are immutable after creation and must be chosen deliberately.**673. **Event**: Select the event type. For automated CI/CD, select **Push to a branch**.684. **Source**: Select the repository source:69 * **Repository**: Connect your source repository (GitHub, Bitbucket, Cloud Source Repositories, etc.). If needed, authorize the connection.70 * **Repository Name**: Select the specific repository you want to link.715. **Branch**: Enter the branch pattern (e.g., `^main$` or `^develop`).7273### Step 3: Configure Build Settings74751. **Configuration**: Select **Cloud Build configuration file (yaml or json)**.762. **Location**: Keep the default **Repository** and specify the path to your build configuration file (e.g., `cloudbuild.yaml`).77 * *Alternative*: For very simple builds, you can choose **Inline** to paste the YAML configuration directly into the trigger.783. **(Optional) Service Account**: For production environments, select a dedicated service account with limited permissions to enforce the principle of least privilege.7980### Step 4: Save and Test81821. Click **Create**. The trigger is now active and will run automatically on the next matching Git push.8384> [!TIP]85> The `cb-create-trigger` skill provides detailed `gcloud` commands for creating triggers across all types (GitHub, Pub/Sub, Webhook) and configurations (inline, Dockerfile, YAML). Use that skill for CLI automation.8687## Running an Existing Trigger Manually8889Sometimes 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).9091> [!IMPORTANT]92> **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.9394### Option A: Via the Cloud Console95961. Navigate to the **Cloud Build Triggers** page (`https://console.cloud.google.com/cloud-build/triggers`).972. Locate the trigger you wish to run.983. Click the vertical ellipsis (⋮) next to the trigger and select **Run**.994. A dialog will appear, allowing you to specify:100 * **Source branch/tag**: Choose the specific Git reference to build from.101 * **Substitution Variables**: Override any existing substitution variables (e.g., set `_VERSION` to a new value).1025. Click **Run trigger**. The build will start immediately, and you can monitor its status on the **History** page.103104### Option B: Via the gcloud CLI105106Use the `gcloud builds triggers run` command to invoke the trigger and optionally override parameters.107108```bash109# Run the trigger against the 'main' branch110gcloud builds triggers run <TRIGGER_NAME> \111 --region=<REGION> \112 --branch=main113114# Run the trigger and override a substitution variable115gcloud builds triggers run <TRIGGER_NAME> \116 --region=<REGION> \117 --branch=main \118 --substitutions=_IMAGE_TAG="20231027-manual"119120# Monitor the initiated build121# Note: The run command outputs the build ID. Use it to check status:122# gcloud builds log <BUILD_ID> --region=<REGION>123```124125> [!NOTE]126> The `cb-run-trigger` skill provides more complex invocation examples, including running against a specific commit SHA or using tags.127128## Related Skills129130* [`cb-create-trigger`](references/cb-create-trigger.md): Detailed CLI-focused instructions for creating all trigger types.131* [`cb-list-build-zero-state`](references/cb-list-build-zero-state.md): Advanced management of the Cloud Build dashboard and onboarding zero state.132* [`cb-run-trigger`](references/cb-run-trigger.md): Comprehensive guide to manually running triggers using various `gcloud` options.133134## External Resources & Documentation135136* [Google Cloud Build Documentation](https://cloud.google.com/build/docs)137* [Cloud Build Configuration File Schema](https://cloud.google.com/build/docs/build-config-file-schema)138* [Automating Builds with Triggers](https://cloud.google.com/build/docs/automating-builds/create-manage-triggers)139* [gcloud CLI builds Reference](https://cloud.google.com/sdk/gcloud/reference/builds)