Connecting Cloud Build triggers with Infrastructure Manager lets you automate infrastructure deployments directly from your Git repositories. In this workflow, pull requests generate a deployment preview, and merged commits automatically create or update the deployment.
Before you begin
Enable the required APIs in your project:
- Infra Manager API (
config.googleapis.com) - Cloud Build API (
cloudbuild.googleapis.com) - Secret Manager API (
secretmanager.googleapis.com) - Identity and Access Management (IAM) API (
iam.googleapis.com) - Cloud Storage API (
storage.googleapis.com)
Enable these APIs using the
gcloudCLI:gcloud services enable \ config.googleapis.com \ cloudbuild.googleapis.com \ secretmanager.googleapis.com \ iam.googleapis.com \ storage.googleapis.com- Infra Manager API (
Ensure you have a service account with the required roles. For details, see Configure the service account.
Identify the existing Git repository that has the Terraform configuration. When you set up the automation described in this document, Infra Manager uses this Terraform configuration to update resources.
Recommended: Set up branch protection for your Git repository.
Required roles
To automate deployments, your user account, the deploying service account, and the Cloud Build service agent each require specific IAM roles.
Your user account roles
To get the permissions that you need to create deployments, ask your administrator to grant you the following IAM roles:
- Cloud Infrastructure Manager Admin (
roles/config.admin) on your project - Service Account User (
roles/iam.serviceAccountUser) on the service account used to deploy theim_cloudbuild_workspacemodule
For details about configuring the service account, see Configure the service account.
Deploying service account roles
To ensure that the service account that you specify in the
--service-account flag has the necessary
permissions to deploy the im_cloudbuild_workspace
module,
ask your administrator to grant the
following IAM roles to the service account that you specify in the
--service-account flag on your project:
- Cloud Infrastructure Manager Agent (
roles/config.agent) - Cloud Build Connection Admin (
roles/cloudbuild.connectionAdmin) - Cloud Build Editor (
roles/cloudbuild.builds.editor) - Secret Manager Admin (
roles/secretmanager.admin) -
Create the Cloud Build and Infra Manager service accounts (if you don't specify existing
cloudbuild_saandinfra_manager_savalues in theim_cloudbuild_workspacemodule): Service Account Admin (roles/iam.serviceAccountAdmin) -
Grant project-level roles to the created service accounts (if you don't specify existing
cloudbuild_saandinfra_manager_savalues in theim_cloudbuild_workspacemodule): Project IAM Admin (roles/resourcemanager.projectIamAdmin)
For details about specifying existing service accounts with the cloudbuild_sa
and infra_manager_sa variables, see the
im_cloudbuild_workspace module README.
Cloud Build service agent roles
To ensure that the Cloud Build service agent
(service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com) has the necessary
permissions to access the GitHub personal access token secret,
ask your administrator to grant the
Secret Manager Secret Accessor (roles/secretmanager.secretAccessor) IAM role to the Cloud Build service agent
(service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com) on the Secret Manager secret that stores the
GitHub token.
For more information about granting roles, see Manage access to projects, folders, and organizations.
Your administrator might also be able to give the Cloud Build service agent
(service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com)
the required permissions through custom
roles or other predefined
roles.
In the preceding email address, PROJECT_NUMBER is the
project number of your project. In the im_cloudbuild_workspace module
configuration
(github.tf file),
the module grants roles/secretmanager.secretAccessor on the secret when the
deploying service account has roles/secretmanager.admin. If you use an
existing secret and manage its allow policy separately, grant
roles/secretmanager.secretAccessor on that secret to the Cloud Build
service agent.
Automate the deployment of Google Cloud resources
The following steps describe the setup required to automate the preview and deployment of a Terraform configuration stored in a Git repository. A deployment of a Terraform configuration can be a revision to an existing deployment. Revisions might delete or replace provisioned Google Cloud resources.
When the setup is complete, a pull request in the Git repository causes Infra Manager to create a preview of the deployment. When a pull request is merged, Infra Manager automatically deploys the Terraform configuration.
The im_cloudbuild_workspace module creates several resources, including:
- A Cloud Build repository connection.
- A Cloud Build trigger that is invoked when a pull request is created. This trigger causes Infra Manager to create a preview deployment using the Terraform configuration in the repository.
- A Cloud Build trigger that is invoked when a pull request is merged with the Git repository. This trigger causes Infra Manager to create or update the deployment using the Terraform configuration in the repository.
For more information about modifying the im_cloudbuild_workspace module's
behavior, see the
im_cloudbuild_workspace module README.
To automate Terraform configuration deployments when the repository is updated:
- Install the Cloud Build GitHub App on your GitHub account or in an organization you own.
Create a classic GitHub personal access token.
Cloud Build requires a classic GitHub personal access token; fine-grained personal access tokens aren't supported.
Make sure to set your token to have no expiration date and select the following permissions when prompted in GitHub:
repoandread:user. If your app is installed in an organization, make sure to also select theread:orgpermission.After you generate your personal access token, save your generated token in a secure place. You use the generated token in the following steps.
Copy the
im_cloudbuild_workspacemodule into themain.tffile within your Git repository:module "im-workspace" { source = "terraform-google-modules/bootstrap/google//modules/im_cloudbuild_workspace" version = "~> 7.0" project_id = "PROJECT_ID" deployment_id = "DEPLOYMENT_ID" im_deployment_repo_uri = "GIT_REPO" im_deployment_ref = "REF" github_app_installation_id = "GIT_APP_ID" github_personal_access_token = "TOKEN" }Replace the following:
PROJECT_ID: the project ID where Infra Manager runs.DEPLOYMENT_ID: the deployment identifier that you specify. See Deployment ID for details about constraints on the deployment identifier.GIT_REPO: the URI of the Git repository.REF: the Git reference of the configuration. The reference is optional. If you don't specify the reference, the Git repository's default configured branch is used.GIT_APP_ID: the installation ID of the Cloud Build GitHub App used for pull and push request triggers.TOKEN: a personal access token for a GitHub repository. If you provide a token, the module creates a secret in Secret Manager.
Create a deployment (or update your existing deployment) with the
im_cloudbuild_workspacemodule:- If your Terraform configuration is in a public Git repository, deploy it by
using the
gcloud infra-manager deployments applycommand with the--git-source-repoflag. If your Terraform configuration is in a private Git repository, a Cloud Build connection to the repository must exist before you can use the
--git-source-repoflag. Complete one of the following tasks:- Connect your GitHub host and repository to Cloud Build,
and then run
gcloud infra-manager deployments apply --git-source-repo. - Deploy the
im_cloudbuild_workspaceconfiguration from a local directory (--local-sourceflag) or a Cloud Storage bucket (--gcs-sourceflag) to create the connection first. For details, see Deploy infrastructure using Infrastructure Manager.
- Connect your GitHub host and repository to Cloud Build,
and then run
gcloud infra-manager deployments apply \ projects/PROJECT_ID/locations/LOCATION/deployments/DEPLOYMENT_ID \ --service-account projects/SERVICE_ACCOUNT_PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT \ --git-source-repo=GIT_REPO \ --git-source-directory=DIRECTORY \ --git-source-ref=REFReplace the following:
PROJECT_ID: the project ID where Infra Manager runs.LOCATION: the location where Infra Manager runs. See Infra Manager locations for the list of locations.DEPLOYMENT_ID: the deployment identifier that you specify. See Deployment ID for details about constraints on the deployment identifier.SERVICE_ACCOUNT_PROJECT_ID: the project ID of the service account. It's common for this to be the same project where Infra Manager runs.SERVICE_ACCOUNT: the email address of the service account that you use to call Infra Manager.GIT_REPO: the URI of the Git repository.DIRECTORY: the directory that has the Terraform configuration.REF: the Git reference of the configuration. The reference is optional. If you don't specify the reference, the Git repository's default configured branch is used.
- If your Terraform configuration is in a public Git repository, deploy it by
using the
To confirm that your Git repository is connected correctly with Infra Manager, create a pull request on your repository.
To see the results of the validation of the Terraform configuration, view the pull request in GitHub.
Now that this automation is set up, the Cloud Build triggers cause the following:
- When a pull request is made to the repository, Infra Manager creates a preview deployment using the Terraform configuration stored in the Git repository.
- When a pull request is merged into the repository, Infra Manager updates the deployment using the Terraform configuration stored in the Git repository.
What's next
- Use Cloud Build to view build results for your build triggers.
- View resources deployed with Infra Manager.
- Monitor your Google Cloud resources with Infrastructure Manager audit logging.