You can schedule recurring Image Builder runs by using Cloud Scheduler and Cloud Build triggers. When you schedule custom OS image builds, Cloud Build automatically updates your baseline images with operating system patches and organizational security policies without requiring manual intervention.
Before you begin
- Complete the environment setup steps in Prepare your environment.
-
If you haven't already, set up authentication.
Authentication verifies your identity for access to Google Cloud services and APIs. To run
code or samples from a local development environment, you can authenticate to
Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
gcloud
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud initIf you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
-
Required roles
To get the permissions that you need to create triggers and schedule builds, ask your administrator to grant you the following IAM roles on your project:
- Cloud Build Editor (
roles/cloudbuild.builds.editor) - Cloud Scheduler Job Runner (
roles/cloudscheduler.jobRunner) or Cloud Scheduler Admin (roles/cloudscheduler.admin) - Service Account User (
roles/iam.serviceAccountUser)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Create a scheduled build pipeline
You can configure recurring image builds by using the gcloud CLI or the Go client library:
gcloud
To schedule recurring OS image builds by using the gcloud CLI, complete the following steps:
Create a manual Cloud Build trigger that specifies your target
cloudbuild.yamlfile by running thegcloud builds triggers create manualcommand. Because Cloud Scheduler invokes this trigger manually rather than on repository events, don't specify push or pull request filters:gcloud builds triggers create manual \ --name="TRIGGER_NAME" \ --region=REGION \ --build-config=CLOUDBUILD_YAML_PATH \ --project=PROJECT_IDRetrieve the unique ID (
trigger_id) generated for your new trigger by running thegcloud builds triggers describecommand:gcloud builds triggers describe TRIGGER_NAME \ --region=REGION \ --format="value(id)" \ --project=PROJECT_IDTo create a Cloud Scheduler job that sends an
HTTP POSTrequest to the Cloud Build API (projects.locations.triggers.run) according to your required unix cron schedule, for example, weekly on Mondays at 6:00 AM using'0 6 * * 1', run thegcloud scheduler jobs create httpcommand:gcloud scheduler jobs create http JOB_NAME \ --schedule="CRON_SCHEDULE" \ --uri="https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/triggers/TRIGGER_ID:run" \ --message-body="{}" \ --oauth-service-account-email="SERVICE_ACCOUNT_EMAIL" \ --location=REGION \ --project=PROJECT_ID
Replace the following:
TRIGGER_NAME: the name for your manual Cloud Build trigger, for example,weekly-ubuntu-builder.REGION: the region to create the trigger, for example,us-central1.CLOUDBUILD_YAML_PATH: the path to yourcloudbuild.yamlfile in your local directory, for example,cloudbuild.yaml.PROJECT_ID: your project ID.JOB_NAME: the name for your Cloud Scheduler cron job, for example,weekly-custom-os-patching.CRON_SCHEDULE: the unix cron schedule expression, for example,'0 6 * * 1'.TRIGGER_ID: the unique ID (trigger_id) generated for your trigger.SERVICE_ACCOUNT_EMAIL: the service account email that authorizes Cloud Scheduler to invoke the trigger. This service account requiresroles/cloudbuild.builds.editorpermissions.
Go
To schedule pipelines programmatically by using the Cloud Build
and Cloud Scheduler Go client libraries, use the scheduleBuild
function.
Before you run this sample, complete the following prerequisites:
- Upload your
imagebuilder.yamlconfiguration file to your Cloud Storage bucket, for example,gs://STAGING_BUCKET/imagebuilder.yaml. - Grant the scheduling service account
cloud-build-trigger-scheduler@PROJECT_ID.iam.gserviceaccount.com, the Cloud Build Editor role (roles/cloudbuild.builds.editor) on your project and the Service Account User role (roles/iam.serviceAccountUser) on the service account that runs the build. For more information, see Schedule builds in the Cloud Build documentation.
What's next
- Learn how to Automate builds on repository events on Git push, tag, or merge events using Terraform.
- Review the Customization recipe schema to define software packages and validation procedures.