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

CloudBuild触发器是否支持CLI的patch命令?API已支持但CLI未列出

Question: Does Cloud Build provide a CLI patch command for triggers?

I know that the GCB API v1 includes a patch option, but the gcloud beta builds triggers command list doesn't have this option. I tried running gcloud beta builds triggers patch $trigger_name but it didn't work.

My use case: I have a folder storing all GCB trigger configurations. Currently, I use Cloud Build itself to create or update triggers from the trigger.yaml files in this folder. The current workflow is to delete all triggers first and then recreate them. Maybe using the patch method is a better choice, as it can avoid generating too many trigger IDs. Here's the current step code:

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    echo 'Starting bash'
    for gcb_trigger in folder/triggers/*.yaml; do
      gcloud beta builds triggers delete "$(basename $gcb_trigger .yaml)" --quiet
    done
    for gcb_trigger in folder/triggers/*.yaml; do
      gcloud beta builds triggers create cloud-source-repositories --trigger-config="$gcb_trigger"
    done
    echo 'Finishing bash'

Answer

Great question! Unfortunately, as of now, the gcloud CLI (even the beta version) doesn't offer a dedicated gcloud builds triggers patch command—even though the Cloud Build REST API v1 does support the patch operation. That's exactly why your attempt to run that command failed.

But don't worry, you can still achieve the patch behavior you want by directly calling the Cloud Build REST API from your Cloud Build steps. Here's how you can adjust your workflow to update existing triggers without deleting them (and keep your original trigger IDs):

Updated Workflow Script

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    echo 'Starting trigger update process'
    # Install yq to convert YAML configs to JSON (required for the API)
    apt-get update && apt-get install -y yq
    PROJECT_ID=$(gcloud config get-value project)
    
    for gcb_trigger in folder/triggers/*.yaml; do
      TRIGGER_NAME=$(basename "$gcb_trigger" .yaml)
      # Fetch the existing trigger ID using its name
      TRIGGER_ID=$(gcloud beta builds triggers list \
        --filter="name:$TRIGGER_NAME" \
        --format="value(id)")
      
      if [ -n "$TRIGGER_ID" ]; then
        echo "Updating existing trigger: $TRIGGER_NAME (ID: $TRIGGER_ID)"
        # Convert YAML config to JSON (API accepts JSON payloads)
        TRIGGER_JSON=$(yq -o json "$gcb_trigger")
        # Send PATCH request to the Cloud Build API
        curl -X PATCH \
          "https://cloudbuild.googleapis.com/v1/projects/$PROJECT_ID/triggers/$TRIGGER_ID" \
          -H "Authorization: Bearer $(gcloud auth print-access-token)" \
          -H "Content-Type: application/json" \
          -d "$TRIGGER_JSON"
      else
        echo "Creating new trigger: $TRIGGER_NAME"
        # Fall back to creating the trigger if it doesn't exist
        gcloud beta builds triggers create cloud-source-repositories --trigger-config="$gcb_trigger"
      fi
    done
    echo 'Trigger update process completed'

Key Notes:

  • YAML to JSON Conversion: The Cloud Build API expects JSON payloads, so we use yq to convert your existing YAML configs. The gcr.io/cloud-builders/gcloud image doesn't include yq by default, so we install it via apt-get.
  • Trigger ID Lookup: We use gcloud beta builds triggers list to find the existing trigger ID by its name—this lets us target the exact trigger to patch.
  • Permissions: Ensure the Cloud Build service account has the Cloud Build Editor role (or a custom role with cloudbuild.triggers.update permission) to successfully call the patch API.

This approach keeps your original trigger IDs intact, avoids the overhead of deleting and recreating triggers, and aligns with the patch functionality the API provides.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 10:17:51