How to run a Google Cloud Build trigger via cli / rest api / cloud functions?
How to run a Google Cloud Build trigger via cli / rest api / cloud functions?
Is there such an option? My use case would be running a trigger for a production build (deploys to production). Ideally, that trigger doesn't need to listen to any change since it is invoked manually via chatbot.
I saw this video CI/CD for Hybrid and Multi-Cloud Customers (Cloud Next '18) announcing there's an API trigger support, I'm not sure if that's what I need.
4 Answers
4
you can trigger a function via
gcloud functions call NAME --data 'THING'
inside your function you can do pretty much anything possibile within Googles Public API's
if you just want to directly trigger Google Cloud Builder from git then its probably advisable to use Release version tags - so your chatbot might add a release tag to your release branch in git at which point cloud-builder will start the build.
more info here https://cloud.google.com/cloud-build/docs/running-builds/automate-builds
i've never tried it but i believe you can add a tag to a commit ID git tag -a v2.0 COMMIT_HASH
– Dan
Aug 19 at 8:14
You misunderstood, you can certainly tag via commit hash but that requires cloning the repo first. Which make it a slow solution versus simply triggering the build via cli/api. You are suggesting git tagging via cloud functions or my chatbot right? That requires manual clone.
– chriz
Aug 19 at 9:51
If you just want to create a function that you can invoke directly, you have two choices:
The first is the more common approach, as you are effectively creating a web API that any client can call with an HTTP library of their choice.
I did same thing few days ago.
You can submit your builds using gcloud and rest api
gcloud:
gcloud builds submit --no-source --config=cloudbuild.yaml --async --format=json
Rest API:
Send you cloudbuild.yaml as JSON with Auth Token to this url https://cloudbuild.googleapis.com/v1/projects/standf-188123/builds?alt=json
https://cloudbuild.googleapis.com/v1/projects/standf-188123/builds?alt=json
example cloudbuild.yaml:
steps:
- name: 'gcr.io/cloud-builders/docker'
id: Docker Version
args: ["version"]
- name: 'alpine'
id: Hello Cloud Build
args: ["echo", "Hello Cloud Build"]
example rest_json_body:
"steps": ["args": ["version"], "id": "Docker Version", "name": "gcr.io/cloud-builders/docker", "args": ["echo", "Hello Cloud Build"], "id": "Hello Cloud Build", "name": "alpine"]
You should be able to manually trigger a build using curl and a json payload.
For details see: https://cloud.google.com/cloud-build/docs/running-builds/start-build-manually#running_builds.
Given that, you could write a Python Cloud function to replicate the curl call via the requests module.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Right now, the only available Google Cloud Builder API is creating a trigger, but not running them. I was thinking of pushing a tag with my chatbot, but I think I have to clone it first before I can push a tag, which seems slow. I'm using cloud functions for our chatbot and don't want to run a dedicated instance just for this.
– chriz
Aug 19 at 8:04