CloudBuild触发器是否支持CLI的patch命令?API已支持但CLI未列出
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'
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
yqto convert your existing YAML configs. Thegcr.io/cloud-builders/gcloudimage doesn't includeyqby default, so we install it viaapt-get. - Trigger ID Lookup: We use
gcloud beta builds triggers listto 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 Editorrole (or a custom role withcloudbuild.triggers.updatepermission) 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

