Skip to content

Commit d4977c4

Browse files
committed
Enforce dedicated service account for Cloud Build triggers
1 parent 1e0fed7 commit d4977c4

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

skills/gcp-cicd-design/references/how_to_create_cloudbuild_trigger.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ This document outlines the standard, idempotent procedure for creating a Google
44

55
---
66

7-
## ## Core Principle: Idempotency
7+
## Core Principle: Idempotency
88

99
Every step in this process must be **idempotent**. This means the agent must **always check if a resource already exists** before attempting to create it. This prevents errors and ensures the process can be run multiple times safely.
1010

1111
---
1212

13-
## ## Prerequisite Checklist
13+
## Prerequisite Checklist
1414

1515
The following dependencies must be satisfied in order before creating the trigger.
1616

17-
### ### 1. Ensure `cloudbuild.yaml` Exists
17+
### 1. Ensure `cloudbuild.yaml` Exists
1818

1919
The trigger needs a build configuration file to execute.
2020

2121
* **Action**: Check for a `cloudbuild.yaml` file at the root of the source repository.
2222
* **If it does not exist**: Generate one by translating the user-approved plan. The steps in the generated YAML must be a direct translation of the components defined in the plan's `stages` object. The specifics of the steps (e.g., using `pytest` vs. `mvn test`) should be informed by discovering the application archetype (e.g., by finding a `pyproject.toml` or `pom.xml`).
2323

24-
### ### 2. Ensure Artifact Registry Repository Exists
24+
### 2. Ensure Artifact Registry Repository Exists
2525

2626
The `cloudbuild.yaml` file will reference an Artifact Registry repository to push container images. This repository must exist before a build can succeed.
2727

@@ -30,7 +30,7 @@ The `cloudbuild.yaml` file will reference an Artifact Registry repository to pus
3030
* **Check** if this repository already exists in the target GCP project.
3131
* **If it does not exist**: Create it using the available tools.
3232

33-
### ### 3. Ensure Developer Connect and Repository Link Exist
33+
### 3. Ensure Developer Connect and Repository Link Exist
3434

3535
Cloud Build triggers connect to source code via Developer Connect. The entire connection and repository link must be in place.
3636

@@ -40,8 +40,20 @@ Cloud Build triggers connect to source code via Developer Connect. The entire co
4040
4. **Check for Repository Link**: Check if a repository link for that specific URI already exists within the Developer Connect connection.
4141
5. **Create Repository Link (if needed)**: If the link does not exist, create it. This link is the resource that the Cloud Build trigger will formally point to.
4242

43+
### 4. Ensure Dedicated Service Account Exists
44+
45+
Cloud Build triggers should **always** use a dedicated, user-managed service account instead of the default Compute Engine service account to follow the principle of least privilege.
46+
47+
1. **Check for Service Account**: Check if a dedicated service account for Cloud Build (e.g., `cloud-build-runner@<PROJECT_ID>.iam.gserviceaccount.com`) already exists.
48+
2. **Create Service Account (if needed)**: If no dedicated service account exists, create one using `gcloud iam service-accounts create`.
49+
3. **Grant Necessary Roles**: Ensure the dedicated service account has the following roles at minimum:
50+
* `roles/logging.logWriter`: To write build logs to Cloud Logging.
51+
* `roles/artifactregistry.writer`: To push built images to Artifact Registry.
52+
* `roles/developerconnect.tokenAccessor`: To access source code via Developer Connect.
53+
* *Note*: Additional roles (e.g., `roles/run.admin`, `roles/iam.serviceAccountUser`) may be required depending on the specific deployment steps in the `cloudbuild.yaml`.
54+
4355
---
4456

45-
## ## Final Step: Creating the Trigger
57+
## Final Step: Creating the Trigger
4658

47-
Once all prerequisites are met, the agent can create the trigger itself using the available tools.
59+
Once all prerequisites are met, the agent can create the trigger itself using the `create_build_trigger` tool. **You MUST provide the email of the dedicated service account identified or created in Step 4 to the `service_account` parameter.**

0 commit comments

Comments
 (0)