Scheduled builds are useful for recurring tasks such as nightly tests. Scheduling a build with Cloud Build, however, requires additional infrastructure to trigger the build. In this blog I’ll show you how to use Terraform to configure a Manual trigger and trigger it with Cloud Scheduler.
Remark Sadly we can’t use Terraform for all infrastructure. The source repository connection requires you to install the GitHub App -or similar- once.
Create a scheduled build
A scheduled build is created by defining the build trigger and configuring a job to run the build trigger. The required code is shown next.
Bonus: Reduce scheduled job permissions
You might have noticed that we granted Cloud Build Editor permissions to the Scheduled Job. This is overkill, because we only need the cloudbuild.builds.create-permission to run a trigger. In the next example, the IAM assignment is replaced with a custom role.
While this is better. We are still not applying least privilege. First, the cloudbuild.builds.create
permissions allows for more than running a trigger. I’m hoping for a future cloudbuild.builds.run
permission to replace this. Second, the role is assigned at the Project-level. In effect you can trigger any build. Again, I’m hoping for a future Build Trigger level IAM permission to replace this.
Conclusion
Creating scheduled build triggers is as easy as configuring a couple of resources. However, beware of the excessive IAM permissions required to trigger a build.
Image by Andreas Lischka from Pixabay