Scheduling Cloud Builds

Neil Kolban
Google Cloud - Community
4 min readJun 19, 2020

— — — — — — — — — — — — — — — — — — —

Update: 2021–03–23: Following the writing of this post, a feature has been added to Cloud Builds that natively provides scheduling within the product. The documentation for this can be found here:

— — — — — — — — — — — — — — — — — — —

Let us imagine that we want to perform a GitHub build every night at 2:00am … how might we do this?

One recipe is to create a Cloud Build trigger associated with your GitHub project and then initiate that build on a schedule. We will now look at this in more depth.

When we create the Cloud Build trigger we will disable the automated firing of the trigger that would normally occur as the result of a GIT commit. The purpose of this is that we can then manually trigger the execution of the trigger which, when fired, will cause the build to start processing.

Cloud Build allows us to manually trigger builds in a variety of ways.

We can manually trigger from the console:

We can manually trigger from the command line using gcloud beta builds triggers run:

gcloud beta builds triggers run 640ff527-dcb0-45a8-b4fd-4b8e1fe446e8 
--branch master

In the gcloud command, the hex number shown above is the trigger id. This is a unique id for the trigger that is automatically generated by Google when we create the trigger definition. We can find this value by listing the triggers:

gcloud beta builds triggers list
---
createTime: '2020-06-04T17:15:04.492691319Z'
disabled: true
github:
name: test1
owner: kolbancloud
push:
branch: ^master$
id: 640ff527-dcb0-45a8-b4fd-4b8e1fe446e8
name: test1

We will see it in the id field.

Finally, we can manually trigger the build using a REST request as documented here:

The REST request is composed of:

POST https://cloudbuild.googleapis.com/v1/projects/{projectId}/triggers/{triggerId}:run

Where:

  • projectId — The ID of the project hosting the Cloud Build.
  • triggerId — The generated identity of the trigger.

The body must contain a JSON document that looks like:

{
"branchName": "master"
}

If we think of the final part, we have now changed our story from how to run a Cloud Build on a repeating schedule to a story of how to invoke a REST request on a repeating schedule. The solution to that puzzle is to leverage Cloud Scheduler. Cloud Scheduler can invoke an arbitrary REST endpoint on a regular schedule. If we configure Cloud Scheduler to invoke the GCP API to run a Cloud Build, we will have achieved our goal. Before we can configure the Cloud Scheduler job, we must first create a service account that has the authority to trigger a Cloud Build run. To do this, create a new Service Account that has been granted the role:

Cloud Build Editor

We can create a new Cloud Scheduler job through the console.

  1. Visit the Cloud Scheduler page in the GCP Console.
  2. Click on the “[+] CREATE JOB” button.

For the URL supply:

https://cloudbuild.googleapis.com/v1/projects/{PROJECT_ID}/triggers/{TRIGGER_ID}:run

For body supply:

{
"branchName": "master"
}

Click SHOW MORE:

Set:

  • Auth Header: Add OAuth token
  • Service account: The email of the service account to be used to trigger the build.

Click CREATE.

And that is it. What happens now is that every day at 2:00am, Cloud Scheduler will send a REST request to the GCP API to cause the Cloud Build trigger to kick-off.

In addition to this written tutorial, here is a short video walking through each of these steps and showing the result in action:

--

--

Neil Kolban
Google Cloud - Community

IT specialist with 30+ years industry experience. I am also a Google Customer Engineer assisting users to get the most out of Google Cloud Platform.