DEV Community

Ivan Starkov
Ivan Starkov

Posted on

Google cloud build webhooks on specific commit SHA.

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.

push trigger

This is the webhook trigger.

webhook trigger

with following substitutions


and following inline source

  - name: 'google/cloud-sdk'
      - '-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}
          echo "MASTER"
          gcloud alpha builds triggers run ${_TRIGGER_NAME} --branch=master
    id: aggregator-custom-build
      - '-'
    entrypoint: bash
  - webhook-trigger
Enter fullscreen mode Exit fullscreen mode


To invoke webhook trigger you need just.

    curl -X POST -H "application/json" \
    "${PROJECT}/triggers/webhook-invoke-trigger:webhook?key=${API_KEY}" \
    -d '{"COMMIT_SHA": "${COMMIT_SHA}", "TRIGGER_NAME": "webhook-invoke-agg-custom-update"}'
Enter fullscreen mode Exit fullscreen mode

That allows me to run specific build steps like periodic data updates using "sha fixed" scripts.

Top comments (0)