Automatiser le déploiement des ressources Google Cloud
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette page décrit la procédure de configuration des déclencheurs Cloud Build pour automatiser les déploiements de ressources à l'aide de vos dépôts et d'Infrastructure Manager existants. Google Cloud
Assurez-vous de disposer des autorisations IAM nécessaires pour créer des déploiements :
roles/config.admin
Assurez-vous de disposer d'un compte de service disposant des autorisations nécessaires. Pour en savoir plus, consultez la section Configurer le compte de service.
Identifiez le dépôt Git existant contenant la configuration Terraform.
Lorsque vous configurez l'automatisation décrite sur cette page, c'est la configuration Terraform qu'Infra Manager utilise pour mettre à jour les ressources.
Automatiser le déploiement des ressources Google Cloud
Les étapes suivantes décrivent la configuration requise pour automatiser l'aperçu et le déploiement d'une configuration Terraform stockée dans un dépôt Git. Un déploiement d'une configuration Terraform peut être une révision d'un déploiement existant.
Les révisions peuvent supprimer ou remplacer des ressources Google Cloud provisionnées.
Une fois la configuration terminée, une demande d'extraction dans le dépôt Git entraîne la création d'un aperçu du déploiement par Infra Manager. Lorsqu'une demande d'extraction est fusionnée, Infra Manager déploie automatiquement la configuration Terraform.
Le module im_cloudbuild_workspace crée plusieurs ressources, y compris les suivantes:
Une connexion à un dépôt Cloud Build.
Déclencheur Cloud Build appelé lorsqu'une demande d'extraction est créée.
Ce déclencheur permet à Infra Manager de créer un déploiement en avant-première à l'aide de la configuration Terraform du dépôt.
Déclencheur Cloud Build appelé lorsqu'une demande d'extraction est fusionnée avec le dépôt Git. Ce déclencheur permet à Infra Manager de créer ou de mettre à jour le déploiement à l'aide de la configuration Terraform du dépôt.
Pour automatiser les déploiements de configuration Terraform lorsque le dépôt est mis à jour:
Installez l'application GitHub Cloud Build sur votre compte GitHub ou dans une organisation dont vous êtes propriétaire.
Assurez-vous de ne pas définir de date d'expiration pour votre jeton et sélectionnez les autorisations suivantes lorsque vous y êtes invité dans GitHub: repo et read:user. Si votre application est installée dans une organisation, veillez également à sélectionner l'autorisation read:org.
Après avoir généré votre jeton d'accès personnel, enregistrez-le dans un endroit sécurisé. Vous utiliserez le jeton généré dans les étapes suivantes.
Copiez le module im_cloudbuild_workspace dans le fichier main.tf de votre dépôt Git:
PROJECT_ID: ID du projet sur lequel s'exécute Infrastructure Manager.
DEPLOYMENT_ID: identifiant de déploiement que vous spécifiez. Pour en savoir plus sur les contraintes liées à l'identifiant de déploiement, consultez la section Nom du déploiement.
GIT_REPO: URI du dépôt Git.
REF: référence Git de la configuration. La référence est facultative. Si vous ne spécifiez pas de référence, la branche configurée par défaut du dépôt Git est utilisée.
GIT_APP_ID: ID d'installation de l'application GitHub Cloud Build utilisée pour les déclencheurs de requêtes d'extraction et de transfert.
TOKEN: jeton d'accès personnel pour un dépôt GitHub.
Si fourni, crée un secret dans Secret Manager.
Créez un déploiement (ou mettez à jour votre déploiement existant) avec le module im_cloudbuild_workspace:
PROJECT_ID: ID du projet sur lequel s'exécute Infrastructure Manager.
LOCATION: emplacement où Infra Manager s'exécute. Pour obtenir la liste des emplacements, consultez la page Emplacements Infrastructure Manager.
DEPLOYMENT_ID: identifiant de déploiement que vous spécifiez. Pour en savoir plus sur les contraintes liées à l'identifiant de déploiement, consultez la section Nom du déploiement.
SERVICE_ACCOUNT: nom du compte de service que vous utilisez pour appeler Infra Manager.
SERVICE_ACCOUNT_PROJECT_ID correspond à l'ID du projet du compte de service. Il s'agit généralement du même projet que celui dans lequel Infrastructure Manager s'exécute.
GIT_REPO: dépôt Git public.
DIRECTORY: répertoire contenant la configuration Terraform.
REF: référence Git de la configuration. La référence est facultative. Si vous ne spécifiez pas de référence, la branche configurée par défaut du dépôt Git est utilisée.
Maintenant que cette automatisation est configurée, les déclencheurs Cloud Build provoquent les événements suivants:
Lorsqu'une demande d'extraction est envoyée au dépôt, Infra Manager crée un déploiement d'aperçu à l'aide de la configuration Terraform dissociée stockée dans le dépôt Git.
Lorsqu'une demande d'extraction est fusionnée dans le dépôt, Infra Manager met à jour le déploiement à l'aide de la configuration Terraform dissociée stockée dans le dépôt Git.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/05 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/05 (UTC)."],[[["\u003cp\u003eThis page details how to automate Google Cloud resource deployments using Cloud Build triggers, your Git repositories, and Infrastructure Manager.\u003c/p\u003e\n"],["\u003cp\u003eSetting up this automation allows Infrastructure Manager to create deployment previews on pull requests and update deployments upon pull request merges.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eim_cloudbuild_workspace\u003c/code\u003e module is used to create a Cloud Build repository connection and the necessary triggers for pull requests and merges.\u003c/p\u003e\n"],["\u003cp\u003ePrerequisites include enabling Infrastructure Manager, ensuring proper IAM permissions and a suitable service account, and identifying the Git repository with Terraform configurations.\u003c/p\u003e\n"],["\u003cp\u003eInstalling the Cloud Build GitHub App and creating a personal access token are necessary steps to link your Git repository with Infra Manager for automated deployments.\u003c/p\u003e\n"]]],[],null,["# Automate the deployment of Google Cloud resources\n\nThis page describes the process to set up [Cloud Build triggers](/build/docs/triggers)\nto automate deployments of Google Cloud resources using your\nexisting repositories and Infrastructure Manager.\n\nOnce Infra Manager connects to your Git repository, pull requests\ninvoke a preview of a deployment, and commits invoke [creating a\ndeployment preview](/infrastructure-manager/docs/preview-deployment) or [updating the deployment](/infrastructure-manager/docs/update-deployment).\n\nBefore you begin\n----------------\n\n1. Ensure [Infra Manager is enabled](/infrastructure-manager/docs/enable-service).\n2. Ensure that you have the IAM permissions needed to create deployments:\n - `roles/config.admin`\n3. Ensure you have a service account with the needed permissions. For details, see [Configure the service account](/infrastructure-manager/docs/configure-service-account).\n4. Identify the existing Git repository that has the Terraform configuration. When you set up the automation described on this page, this is the Terraform configuration that Infra Manager uses to update resources.\n5. Recommended: Set up [branch protection](/docs/terraform/resource-management/managing-infrastructure-as-code#enforcing_cloud_build_execution_success_before_merging_branches) for your Git repository.\n\nAutomate the deployment of Google Cloud resources\n-------------------------------------------------\n\n| **Caution:** Infra Manager does not perform data migration or ensure service continuity. For example, if a revision deletes or replaces a database, it is your responsibility to manage data migration. Creating a revision can cause unrecoverable data loss. Re-applying the previous revision will not restore the data.\n\nThe following steps describe the setup required to automate the preview and\ndeployment of a Terraform configuration stored in a Git repository. A deployment\nof a Terraform configuration can be a revision to an existing deployment.\nRevisions might delete or replace provisioned Google Cloud resources.\n\nWhen the setup is complete, a pull request in the Git repository causes\nInfra Manager to create a preview of the deployment. When a pull request is\nmerged, then Infra Manager automatically deploys the Terraform configuration.\n\nThe `im_cloudbuild_workspace` module creates several resources, including:\n\n- A Cloud Build repository connection.\n- 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.\n- 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.\n\n| **Note:** For more information about how you can modify the `im_cloudbuild_workspace` module's behavior, see the [`README`](https://github.com/terraform-google-modules/terraform-google-bootstrap/tree/master/modules/im_cloudbuild_workspace).\n\nTo automate Terraform configuration deployments when the repository is\nupdated:\n\n1. Install the [Cloud Build GitHub App](https://github.com/apps/google-cloud-build) on your GitHub account or in an organization you own.\n2. [Create a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token).\n\n Make sure to set your token to have no expiration date and select the\n following permissions when prompted in GitHub: `repo` and `read:user`. If\n your app is installed in an organization, make sure to also select the\n `read:org` permission.\n\n After you generate your personal access token, save your generated token in\n a secure place. You use the generated token in the following steps.\n | **Note:** If you have an authentication token stored in a secret from a previously created connection, you can use that same secret for the new connection.\n3. Copy the `im_cloudbuild_workspace` module into the `main.tf` file within\n your Git repository:\n\n module \"im-workspace\" {\n source = \"terraform-google-modules/bootstrap/google//modules/im_cloudbuild_workspace\"\n version = \"~\u003e 7.0\"\n\n project_id = \"\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\"\n deployment_id = \"\u003cvar translate=\"no\"\u003eDEPLOYMENT_ID\u003c/var\u003e\"\n im_deployment_repo_uri = \"\u003cvar translate=\"no\"\u003eGIT_REPO\u003c/var\u003e\"\n im_deployment_ref = \"\u003cvar translate=\"no\"\u003eREF\u003c/var\u003e\"\n\n github_app_installation_id = \"\u003cvar translate=\"no\"\u003eGIT_APP_ID\u003c/var\u003e\"\n github_personal_access_token = \"\u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e\"\n }\n\n Replace:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the project ID where Infrastructure Manager runs.\n - \u003cvar translate=\"no\"\u003eDEPLOYMENT_ID\u003c/var\u003e: the deployment identifier that you specify. See [Deployment name](/infrastructure-manager/docs/deployments-revisions#deployment_name) for details about constraints on the deployment identifier.\n - \u003cvar translate=\"no\"\u003eGIT_REPO\u003c/var\u003e: the URI of the Git repository.\n - \u003cvar translate=\"no\"\u003eREF\u003c/var\u003e: 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.\n - \u003cvar translate=\"no\"\u003eGIT_APP_ID\u003c/var\u003e: Installation ID of the Cloud Build GitHub app used for pull and push request triggers.\n - \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e: Personal access token for a GitHub repository. If provided, creates a secret within Secret Manager.\n4. Create a deployment (or update your existing deployment) with\n the `im_cloudbuild_workspace` module:\n\n gcloud infra-manager deployments apply projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e/deployments/\u003cvar translate=\"no\"\u003eDEPLOYMENT_ID\u003c/var\u003e \\ \n --service-account projects/\u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT_PROJECT_ID\u003c/var\u003e/serviceAccounts/\u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT\u003c/var\u003e \\\n --git-source-repo=\u003cvar translate=\"no\"\u003eGIT_REPO\u003c/var\u003e \\\n --git-source-directory=\u003cvar translate=\"no\"\u003eDIRECTORY\u003c/var\u003e \\\n --git-source-ref=\u003cvar translate=\"no\"\u003eREF\u003c/var\u003e\n\n Replace:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the project ID where Infrastructure Manager runs.\n - \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e: the location where Infra Manager runs. See [Infrastructure Manager locations](/infrastructure-manager/docs/locations) for the list of locations.\n - \u003cvar translate=\"no\"\u003eDEPLOYMENT_ID\u003c/var\u003e: the deployment identifier that you specify. See [Deployment name](/infrastructure-manager/docs/deployments-revisions#deployment_name) for details about constraints on the deployment identifier.\n - \u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT\u003c/var\u003e: the name of the service account you use to call Infra Manager.\n - \u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT_PROJECT_ID\u003c/var\u003e is the project ID of the service account. It's common for this to be the same project where Infrastructure Manager runs.\n - \u003cvar translate=\"no\"\u003eGIT_REPO\u003c/var\u003e: the public Git repository.\n - \u003cvar translate=\"no\"\u003eDIRECTORY\u003c/var\u003e: the directory that has the Terraform configuration.\n - \u003cvar translate=\"no\"\u003eREF\u003c/var\u003e: 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.\n5. To confirm that your Git repository is connected correctly with\n Infra Manager, [create a pull request on your repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request).\n\n To see the results of the validation of the forked Terraform configuration,\n [view the pull request in GitHub](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/commenting-on-a-pull-request#discovering-and-navigating-conversations).\n | **Note:** For more information about how you can modify the `im_cloudbuild_workspace` module's behavior, see the [`README`](https://github.com/terraform-google-modules/terraform-google-bootstrap/tree/master/modules/im_cloudbuild_workspace).\n\nNow that this automation is set up, the Cloud Build triggers cause the\nfollowing:\n\n- When a pull request is made to the repository, Infra Manager creates a preview deployment using the forked Terraform configuration stored in the Git repository.\n- When a pull request is merged into the repository, Infra Manager updates the deployment using the forked Terraform configuration stored in the Git repository.\n\nWhat's next\n-----------\n\n- Use Cloud Build to [view build results for your build triggers](/build/docs/view-build-results-triggers).\n- [View resources deployed](/infrastructure-manager/docs/view-resources) with Infra Manager.\n- Monitor your Google Cloud resources with [Infrastructure Manager audit logging](/infrastructure-manager/docs/audit-logging)."]]