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

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
Enter fullscreen mode Exit fullscreen mode

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"}'
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)