You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在一个Google Cloud Project中为另一项目的Cloud Source Repository创建Cloud Build trigger?

Can I create a Cloud Build trigger for a Cloud Source Repository in another GCP project?

Absolutely! You can set up a Cloud Build trigger in one Google Cloud Project (let’s call this the build project) that targets a Cloud Source Repository (CSR) hosted in a separate GCP project (the repo project). Here’s a straightforward, step-by-step guide to make this work smoothly:

1. First, configure critical IAM permissions

Cross-project access lives or dies by proper IAM setup—don’t skip this step to avoid frustrating permission errors later:

  • In the repo project (where your Cloud Source Repository is stored), grant the Cloud Build service account of your build project the roles/source.reader (Cloud Source Repository Reader) role. This lets the trigger pull code from the external repo.

    To find your build project’s Cloud Build service account: It follows the format [PROJECT_NUMBER]@cloudbuild.gserviceaccount.com. Grab the project number from your build project’s GCP Console settings page.

  • Ensure the same service account has roles/cloudbuild.editor (or a higher role like roles/cloudbuild.admin) in your build project—this is usually enabled by default, but double-check if you’ve modified IAM settings in the past.

2. Create the trigger via GCP Console

If you prefer a graphical approach:

  • Navigate to the Cloud Build > Triggers page in your build project’s GCP Console.
  • Click Create trigger.
  • Under the Repository section, select Connect repository, then choose Cloud Source Repository.
  • Click Select another project, enter the ID of your repo project, pick the target repository from the dropdown, and hit Connect.
  • Configure the rest of your trigger details:
    • Choose the event type (e.g., push to a branch, tag creation).
    • Set branch/tag patterns (e.g., ^main$ to trigger only on pushes to the main branch).
    • Specify your build configuration file (like cloudbuild.yaml)—this file can live directly in the external repo.
  • Review your settings and click Create to finalize the trigger.

3. Create the trigger via gcloud CLI

For automation or scripted workflows, use the gcloud command-line tool:

  1. First, set your active project to the build project:
    gcloud config set project YOUR_BUILD_PROJECT_ID
    
  2. Run the trigger creation command, explicitly specifying the repo project and target repository:
    gcloud builds triggers create cloud-source-repositories \
      --name=cross-project-repo-trigger \
      --repo=YOUR_REPO_NAME \
      --repo-project=YOUR_REPO_PROJECT_ID \
      --branch-pattern=^main$ \
      --build-config=cloudbuild.yaml
    
    Adjust parameters like --branch-pattern or --tag-pattern to match your desired trigger conditions.

Quick Notes

  • All build resources (compute instances, logs, etc.) will run in your build project, so billing will be charged to that project.
  • If you hit "permission denied" errors during trigger execution, circle back to verify the IAM roles assigned to the Cloud Build service account in both projects.

内容的提问来源于stack exchange,提问作者Mangesh Borade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:22:18