Google cloud build webhooks on specific commit SHA.
Ivan Starkov
Posted on November 2, 2021
The gcloud build has webhook triggers that allow you to trigger the build using a simple http api.
How to create such a trigger can be found here
The disadvantage for me was that the web trigger cannot be applied to a specific commit, but only to a branch such as master,
moreover the branch name is fixed.
Today I found an interesting way to trigger a build using a webhook trigger for any commit.
Briefly, I create a normal push branch trigger using a branch name that can't exist. Then I create a webhook trigger which
using the command gcloud alpha builds triggers run --sha={COMMIT_SHA}
to run the first trigger with the commitSha
of specific commit.
Now the pictures.
This is push branch trigger.
This is the webhook trigger.
with following substitutions
and following inline source
steps:
- name: 'google/cloud-sdk'
args:
- '-c'
- |
set -eou pipefail
shopt -s inherit_errexit
if [[ "${COMMIT_SHA}" ]]; then
echo "HERE ${COMMIT_SHA}"
gcloud alpha builds triggers run ${_TRIGGER_NAME} --sha ${COMMIT_SHA}
else
echo "MASTER"
gcloud alpha builds triggers run ${_TRIGGER_NAME} --branch=master
fi
id: aggregator-custom-build
waitFor:
- '-'
entrypoint: bash
tags:
- webhook-trigger
Finally
To invoke webhook trigger you need just.
curl -X POST -H "application/json" \
"https://cloudbuild.googleapis.com/v1/projects/${PROJECT}/triggers/webhook-invoke-trigger:webhook?key=${API_KEY}" \
-d '{"COMMIT_SHA": "${COMMIT_SHA}", "TRIGGER_NAME": "webhook-invoke-agg-custom-update"}'
That allows me to run specific build steps like periodic data updates using "sha fixed" scripts.
Posted on November 2, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 25, 2024