The objective of the instructions below is to configure the infrastructure that allows you to run CI/CD deployments using
GitHub Actions for the Terraform Example GenAI stages (0-bootstrap, 1-org, 2-environments, 3-networks, 4-projects).
The infrastructure consists in two Google Cloud Platform projects (prj-b-seed and prj-b-cicd-wif-gh).
It is a best practice to have two separate projects here (prj-b-seed and prj-b-cicd-wif-gh) for separation of concerns.
On one hand, prj-b-seed stores terraform state and has the Service Accounts able to create / modify infrastructure.
On the other hand, the authentication infrastructure using Workload identity federation is implemented in prj-b-cicd-wif-gh.
To run the instructions described in this document, install the following:
- Google Cloud SDK version 393.0.0 or later
- terraform-tools component
- Git version 2.28.0 or later
- Terraform version 1.5.7 or later
Also make sure that you have the following:
- A GitHub account for your User or Organization.
- A private GitHub repository for each one of the stages of GenAI:
- Bootstrap
- Organization
- Environments
- Networks
- Projects
- A Fine grained personal access token configured with:
- Repository access to Only select repositories, including all the repositories in the previous item.
- Permissions:
- Actions: Read and Write
- Metadata: Read-only
- Secrets: Read and Write
- Variables: Read and Write
- Workflows: Read and Write
- A Google Cloud organization.
- A Google Cloud billing account.
- Cloud Identity or Google Workspace groups for organization and billing admins.
- Add the Identity (user or Service Account) who will run Terraform to the
group_org_adminsgroup. They must be in this group, or they won't haveroles/resourcemanager.projectCreatoraccess. - For the Identity who will run the procedures in this document, grant the following roles:
- The
roles/resourcemanager.organizationAdminrole on the Google Cloud organization. - The
roles/orgpolicy.policyAdminrole on the Google Cloud organization. - The
roles/billing.adminrole on the billing account. - The
roles/resourcemanager.folderCreatorrole.
- The
If other users need to be able to run these procedures, add them to the group
represented by the org_project_creators variable.
For more information about the permissions that are required, and the resources
that are created, see the organization bootstrap module
documentation.
Due to issue First commit pushed triggers 2 workflow runs,
The repositories created must be initialized with an initial commit,
so that the initial push for the plan branch does not trigger two workflow runs.
The instructions in the following steps will ensure the creation of an initial commit
for each one of the repositories.
-
Clone terraform-google-enterprise-genai into your local environment.
git clone https://github.com/GoogleCloudPlatform/terraform-google-enterprise-genai.git
-
Clone the private repository you created to host the
0-bootstrapterraform configuration at the same level of theterraform-google-enterprise-genaifolder. You must be authenticated to GitHub.git clone git@github.com:<GITHUB-OWNER>/<GITHUB-BOOTSTRAP-REPO>.git gcp-bootstrap
-
The layout should be:
gcp-bootstrap/ terraform-google-enterprise-genai/
-
Navigate into the repo. All subsequent steps assume you are running them from the
gcp-bootstrapdirectory. If you run them from another directory, adjust your copy paths accordingly.cd gcp-bootstrap -
Seed the repository if it has not been initialized yet.
git commit --allow-empty -m 'repository seed' git push --set-upstream origin main git checkout -b production git push --set-upstream origin production -
change to a nonproduction branch.
git checkout -b plan
-
Copy contents of genai to new repo (modify accordingly based on your current directory).
mkdir -p envs/shared cp -RT ../terraform-google-enterprise-genai/0-bootstrap/ ./envs/shared cp -RT ../terraform-google-enterprise-genai/policy-library/ ./policy-library mkdir -p .github/workflows cp ../terraform-google-enterprise-genai/build/github-tf-* ./.github/workflows/ cp ../terraform-google-enterprise-genai/build/tf-wrapper.sh . chmod 755 ./tf-wrapper.sh cd ./envs/shared
-
In the versions file
./versions.tfun-comment thegithubrequired provider -
In the variables file
./variables.tfun-comment variables in the sectionSpecific to github_bootstrap -
In the outputs file
./outputs.tfComment-out outputs in the sectionSpecific to cloudbuild_module -
In the outputs file
./outputs.tfun-comment outputs in the sectionSpecific to github_bootstrap -
Rename file
./cb.tfto./cb.tf.examplemv ./cb.tf ./cb.tf.example
-
Rename file
./github.tf.exampleto./github.tfmv ./github.tf.example ./github.tf
-
Rename file
terraform.example.tfvarstoterraform.tfvarsmv ./terraform.example.tfvars ./terraform.tfvars
-
Update the file
terraform.tfvarswith values from your Google Cloud environment -
Update the file
terraform.tfvarswith values from your GitHub repositories -
To prevent saving the
gh_tokenin plain text in theterraform.tfvarsfile, export the GitHub fine grained access token as an environment variable:export TF_VAR_gh_token="YOUR-FINE-GRAINED-ACCESS-TOKEN"
-
Use the helper script validate-requirements.sh to validate your environment:
../../../terraform-google-enterprise-genai/scripts/validate-requirements.sh -o <ORGANIZATION_ID> -b <BILLING_ACCOUNT_ID> -u <END_USER_EMAIL> -e
Note: The script is not able to validate if the user is in a Cloud Identity or Google Workspace group with the required roles.
-
Run
terraform initandterraform planand review the output.terraform init terraform plan -input=false -out bootstrap.tfplan
-
To validate your policies, run
gcloud beta terraform vet. For installation instructions, see Validate policies instructions for the Google Cloud CLI. -
Run the following commands and check for violations:
export VET_PROJECT_ID=A-VALID-PROJECT-ID terraform show -json bootstrap.tfplan > bootstrap.json gcloud beta terraform vet bootstrap.json --policy-library="../../policy-library" --project ${VET_PROJECT_ID}
A-VALID-PROJECT-IDmust be an existing project you have access to. This is necessary because Terraform-validator needs to link resources to a valid Google Cloud Platform project. -
No violations and an output with
donemeans the validation was successful. -
Run
terraform apply.terraform apply bootstrap.tfplan
-
Run
terraform outputto get the email address of the terraform service accounts that will be used to run manual steps forsharedenvironments in steps3-networks-svpc, and4-projects.export network_step_sa=$(terraform output -raw networks_step_terraform_service_account_email) export projects_step_sa=$(terraform output -raw projects_step_terraform_service_account_email) echo "network step service account = ${network_step_sa}" echo "projects step service account = ${projects_step_sa}"
-
Run
terraform outputto get the ID of your CI/CD project:export cicd_project_id=$(terraform output -raw cicd_project_id) echo "CI/CD Project ID = ${cicd_project_id}"
-
Copy the backend and update
backend.tfwith the name of your Google Cloud Storage bucket for Terraform's state. Also update thebackend.tfof all steps.export backend_bucket=$(terraform output -raw gcs_bucket_tfstate) echo "backend_bucket = ${backend_bucket}" cp backend.tf.example backend.tf cd ../../../ for i in `find -name 'backend.tf'`; do sed -i "s/UPDATE_ME/${backend_bucket}/" $i; done for i in `find -name 'backend.tf'`; do sed -i "s/UPDATE_PROJECTS_BACKEND/${backend_bucket}/" $i; done cd gcp-bootstrap/envs/shared
-
Re-run
terraform init. When you're prompted, agree to copy Terraform state to Cloud Storage.terraform init
-
(Optional) Run
terraform planto verify that state is configured correctly. You should see no changes from the previous state. -
Save the Terraform configuration to
gcp-bootstrapgithub repository:cd ../.. git add . git commit -m 'Initialize bootstrap repo' git push --set-upstream origin plan
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-BOOTSTRAP-REPO/pull/new/plan from the
planbranch to theproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein theproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-BOOTSTRAP-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
productionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
productionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-BOOTSTRAP-REPO/actions under
tf-apply.
Note 1: The stages after 0-bootstrap use terraform_remote_state data source to read common configuration like the organization ID from the output of the 0-bootstrap stage.
They will fail if the state is not copied to the Cloud Storage bucket.
Note 2: After the deploy, to prevent the project quota error described in the Troubleshooting guide, we recommend that you request 50 additional projects for the projects step service account created in this step.
-
Clone the repository you created to host the
1-orgterraform configuration at the same level of theterraform-google-enterprise-genaifolder.git clone git@github.com:<GITHUB-OWNER>/<GITHUB-ORGANIZATION-REPO>.git gcp-org
-
Navigate into the repo. All subsequent steps assume you are running them from the
gcp-orgdirectory. If you run them from another directory, adjust your copy paths accordingly.cd gcp-org -
Seed the repository if it has not been initialized yet.
git commit --allow-empty -m 'repository seed' git push --set-upstream origin main git checkout -b production git push --set-upstream origin production -
change to a nonproduction branch.
git checkout -b plan
-
Copy contents of genai to new repo.
cp -RT ../terraform-google-enterprise-genai/1-org/ . cp -RT ../terraform-google-enterprise-genai/policy-library/ ./policy-library mkdir -p .github/workflows cp ../terraform-google-enterprise-genai/build/github-tf-* ./.github/workflows/ cp ../terraform-google-enterprise-genai/build/tf-wrapper.sh . chmod 755 ./tf-wrapper.sh
-
Rename
./envs/shared/terraform.example.tfvarsto./envs/shared/terraform.tfvarsmv ./envs/shared/terraform.example.tfvars ./envs/shared/terraform.tfvars
-
Update the file
envs/shared/terraform.tfvarswith values from your GCP environment. See the shared folder README.md for additional information on the values in theterraform.tfvarsfile. -
Un-comment the variable
create_access_context_manager_access_policy = falseif your organization already has an Access Context Manager Policy.export ORGANIZATION_ID=$(terraform -chdir="../gcp-bootstrap/envs/shared" output -json common_config | jq '.org_id' --raw-output) export ACCESS_CONTEXT_MANAGER_ID=$(gcloud access-context-manager policies list --organization ${ORGANIZATION_ID} --format="value(name)") echo "access_context_manager_policy_id = ${ACCESS_CONTEXT_MANAGER_ID}" if [ ! -z "${ACCESS_CONTEXT_MANAGER_ID}" ]; then sed -i "s=//create_access_context_manager_access_policy=create_access_context_manager_access_policy=" ./envs/shared/terraform.tfvars; fi
-
Update the
remote_state_bucketvariable with the backend bucket from step Bootstrap.export backend_bucket=$(terraform -chdir="../gcp-bootstrap/envs/shared" output -raw gcs_bucket_tfstate) echo "remote_state_bucket = ${backend_bucket}" sed -i "s/REMOTE_STATE_BUCKET/${backend_bucket}/" ./envs/shared/terraform.tfvars
-
Check if a Security Command Center Notification with the default name, scc-notify, already exists in your organization.
export ORG_STEP_SA=$(terraform -chdir="../gcp-bootstrap/envs/shared" output -raw organization_step_terraform_service_account_email) gcloud scc notifications describe "scc-notify" --format="value(name)" --organization=${ORGANIZATION_ID} --location=global --impersonate-service-account=${ORG_STEP_SA}
-
If the notification exists the output will be:
organizations/ORGANIZATION_ID/notificationConfigs/scc-notify -
If the notification does not exist the output will be:
ERROR: (gcloud.scc.notifications.describe) NOT_FOUND: Requested entity was not found. -
If the notification exists, choose a different value for the
scc_notification_namevariable in the./envs/shared/terraform.tfvarsfile. -
Commit changes.
git add . git commit -m 'Initialize org repo'
-
Push your plan branch.
git push --set-upstream origin plan
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-ORGANIZATION-REPO/pull/new/plan from the
planbranch to theproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein theproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ORGANIZATION-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
productionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
productionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ORGANIZATION-REPO/actions under
tf-apply.
-
Clone the repository you created to host the
2-environmentsterraform configuration at the same level of theterraform-google-enterprise-genaifolder.git clone git@github.com:<GITHUB-OWNER>/<GITHUB-ENVIRONMENTS-REPO>.git gcp-environments
-
Navigate into the repo. All subsequent steps assume you are running them from the
gcp-environmentsdirectory. If you run them from another directory, adjust your copy paths accordingly.cd gcp-environments -
Seed the repository if it has not been initialized yet.
git commit --allow-empty -m 'repository seed' git push --set-upstream origin main git checkout -b production git push --set-upstream origin production git checkout -b nonproduction git push --set-upstream origin nonproduction git checkout -b development git push --set-upstream origin development -
change to a nonproduction branch.
git checkout -b plan
-
Copy contents of genai to new repo.
cp -RT ../terraform-google-enterprise-genai/2-environments/ . cp -RT ../terraform-google-enterprise-genai/policy-library/ ./policy-library mkdir -p .github/workflows cp ../terraform-google-enterprise-genai/build/github-tf-* ./.github/workflows/ cp ../terraform-google-enterprise-genai/build/tf-wrapper.sh . chmod 755 ./tf-wrapper.sh
-
Rename
terraform.example.tfvarstoterraform.tfvars.mv terraform.example.tfvars terraform.tfvars
-
Update the file with values from your GCP environment. See any of the envs folder README.md files for additional information on the values in the
terraform.tfvarsfile. -
Update the
remote_state_bucketvariable with the backend bucket from step Bootstrap.export backend_bucket=$(terraform -chdir="../gcp-bootstrap/envs/shared" output -raw gcs_bucket_tfstate) echo "remote_state_bucket = ${backend_bucket}" sed -i "s/REMOTE_STATE_BUCKET/${backend_bucket}/" terraform.tfvars
-
Commit changes.
git add . git commit -m 'Initialize environments repo'
-
Push your plan branch.
git push --set-upstream origin plan
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/pull/new/plan from the
planbranch to thedevelopmentbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein thedevelopmentenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
developmentbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
developmentenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/actions under
tf-apply. -
If the GitHub action is successful, apply the next environment.
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/pull/new/development from the
developmentbranch to thenonproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein thenonproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
nonproductionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
nonproductionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/actions under
tf-apply. -
If the GitHub action is successful, apply the next environment.
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/pull/new/nonproduction from the
nonproductionbranch to theproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein theproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
productionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
productionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-ENVIRONMENTS-REPO/actions under
tf-apply. -
You can now move to the instructions in the network stage. To use the Dual Shared VPC network mode go to Deploying step 3-networks-svpc.
-
Clone the repository you created to host the
3-networks-svpcterraform configuration at the same level of theterraform-google-enterprise-genaifolder.git clone git@github.com:<GITHUB-OWNER>/<GITHUB-NETWORKS-REPO>.git gcp-networks
-
Navigate into the repo. All subsequent steps assume you are running them from the
gcp-networksdirectory. If you run them from another directory, adjust your copy paths accordingly.cd gcp-networks -
Seed the repository if it has not been initialized yet.
git commit --allow-empty -m 'repository seed' git push --set-upstream origin main git checkout -b production git push --set-upstream origin production git checkout -b nonproduction git push --set-upstream origin nonproduction git checkout -b development git push --set-upstream origin development -
change to a nonproduction branch.
git checkout -b plan
-
Copy contents of genai to new repo.
cp -RT ../terraform-google-enterprise-genai/3-networks-svpc/ . cp -RT ../terraform-google-enterprise-genai/policy-library/ ./policy-library mkdir -p .github/workflows cp ../terraform-google-enterprise-genai/build/github-tf-* ./.github/workflows/ cp ../terraform-google-enterprise-genai/build/tf-wrapper.sh . chmod 755 ./tf-wrapper.sh
-
Rename
common.auto.example.tfvarstocommon.auto.tfvars, renameshared.auto.example.tfvarstoshared.auto.tfvarsand renameaccess_context.auto.example.tfvarstoaccess_context.auto.tfvars.mv common.auto.example.tfvars common.auto.tfvars mv shared.auto.example.tfvars shared.auto.tfvars mv access_context.auto.example.tfvars access_context.auto.tfvars
-
Update the file
shared.auto.tfvarswith the values for thetarget_name_server_addresses. -
Update the file
access_context.auto.tfvarswith the organization'saccess_context_manager_policy_id.export ORGANIZATION_ID=$(terraform -chdir="../gcp-bootstrap/envs/shared/" output -json common_config | jq '.org_id' --raw-output) export ACCESS_CONTEXT_MANAGER_ID=$(gcloud access-context-manager policies list --organization ${ORGANIZATION_ID} --format="value(name)") echo "access_context_manager_policy_id = ${ACCESS_CONTEXT_MANAGER_ID}" sed -i "s/ACCESS_CONTEXT_MANAGER_ID/${ACCESS_CONTEXT_MANAGER_ID}/" ./access_context.auto.tfvars
-
Update
common.auto.tfvarsfile with values from your GCP environment. See any of the envs folder README.md files for additional information on the values in thecommon.auto.tfvarsfile. -
You must add your user email in the variable
perimeter_additional_membersto be able to see the resources created in the restricted project. -
Update the
remote_state_bucketvariable with the backend bucket from step Bootstrap in thecommon.auto.tfvarsfile.export backend_bucket=$(terraform -chdir="../gcp-bootstrap/envs/shared/" output -raw gcs_bucket_tfstate) echo "remote_state_bucket = ${backend_bucket}" sed -i "s/REMOTE_STATE_BUCKET/${backend_bucket}/" ./common.auto.tfvars
-
Commit changes
git add . git commit -m 'Initialize networks repo'
-
You must manually plan and apply the
sharedenvironment (only once) since thedevelopment,nonproductionandproductionenvironments depend on it. -
Use
terraform outputto get the CI/CD project ID and the networks step Terraform Service Account from gcp-bootstrap output. -
The CI/CD project ID will be used in the validation of the Terraform configuration
export CICD_PROJECT_ID=$(terraform -chdir="../gcp-bootstrap/envs/shared/" output -raw cicd_project_id) echo ${CICD_PROJECT_ID}
-
The networks step Terraform Service Account will be used for Service Account impersonation in the following steps. An environment variable
GOOGLE_IMPERSONATE_SERVICE_ACCOUNTwill be set with the Terraform Service Account to enable impersonation.export GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=$(terraform -chdir="../gcp-bootstrap/envs/shared/" output -raw networks_step_terraform_service_account_email) echo ${GOOGLE_IMPERSONATE_SERVICE_ACCOUNT}
-
Run
initandplanand review output for environment shared../tf-wrapper.sh init shared ./tf-wrapper.sh plan shared
-
To use the
validateoption of thetf-wrapper.shscript, please follow the instructions to install the terraform-tools component. -
Run
validateand check for violations../tf-wrapper.sh validate shared $(pwd)/policy-library ${CICD_PROJECT_ID}
-
Run
applyshared../tf-wrapper.sh apply shared
-
Push your plan branch.
git push --set-upstream origin plan
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/pull/new/plan from the
planbranch to thedevelopmentbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein thedevelopmentenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
developmentbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
developmentenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/actions under
tf-apply. -
If the GitHub action is successful, apply the next environment.
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/pull/new/development from the
developmentbranch to thenonproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein thenonproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
nonproductionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
nonproductionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/actions under
tf-apply. -
If the GitHub action is successful, apply the next environment.
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/pull/new/nonproduction from the
nonproductionbranch to theproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein theproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
productionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
productionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-NETWORKS-REPO/actions under
tf-apply. -
Before executing the next steps, unset the
GOOGLE_IMPERSONATE_SERVICE_ACCOUNTenvironment variable.unset GOOGLE_IMPERSONATE_SERVICE_ACCOUNT -
You can now move to the instructions in the 4-projects stage.
-
Clone the repository you created to host the
4-projectsterraform configuration at the same level of theterraform-google-enterprise-genaifolder.git clone git@github.com:<GITHUB-OWNER>/<GITHUB-PROJECTS-REPO>.git gcp-projects
-
Navigate into the repo. All subsequent steps assume you are running them from the
gcp-projectsdirectory. If you run them from another directory, adjust your copy paths accordingly.cd gcp-projects -
Seed the repository if it has not been initialized yet.
git commit --allow-empty -m 'repository seed' git push --set-upstream origin main git checkout -b production git push --set-upstream origin production git checkout -b nonproduction git push --set-upstream origin nonproduction git checkout -b development git push --set-upstream origin development -
change to a nonproduction branch.
git checkout -b plan
-
Copy contents of genai to new repo.
cp -RT ../terraform-google-enterprise-genai/4-projects/ . cp -RT ../terraform-google-enterprise-genai/policy-library/ ./policy-library mkdir -p .github/workflows cp ../terraform-google-enterprise-genai/build/github-tf-* ./.github/workflows/ cp ../terraform-google-enterprise-genai/build/tf-wrapper.sh . chmod 755 ./tf-wrapper.sh
-
Rename
auto.example.tfvarsfiles toauto.tfvars.mv common.auto.example.tfvars common.auto.tfvars mv shared.auto.example.tfvars shared.auto.tfvars mv development.auto.example.tfvars development.auto.tfvars mv nonproduction.auto.example.tfvars nonproduction.auto.tfvars mv production.auto.example.tfvars production.auto.tfvars
-
See any of the envs folder README.md files for additional information on the values in the
common.auto.tfvars,development.auto.tfvars,nonproduction.auto.tfvars, andproduction.auto.tfvarsfiles. -
See any of the shared folder README.md files for additional information on the values in the
shared.auto.tfvarsfile. -
Use
terraform outputto get the backend bucket value from bootstrap output.export remote_state_bucket=$(terraform -chdir="../gcp-bootstrap/envs/shared/" output -raw gcs_bucket_tfstate) echo "remote_state_bucket = ${remote_state_bucket}" sed -i "s/REMOTE_STATE_BUCKET/${remote_state_bucket}/" ./common.auto.tfvars
-
Commit changes.
git add . git commit -m 'Initialize projects repo'
-
You need to manually plan and apply only once the
ml_business_unit/sharedenvironments sincedevelopment,nonproduction, andproductiondepend on them. -
Use
terraform outputto get the CI/CD project ID and the projects step Terraform Service Account from gcp-bootstrap output. -
The CI/CD project ID will be used in the validation of the Terraform configuration
export CICD_PROJECT_ID=$(terraform -chdir="../gcp-bootstrap/envs/shared/" output -raw cicd_project_id) echo ${CICD_PROJECT_ID}
-
The projects step Terraform Service Account will be used for Service Account impersonation in the following steps. An environment variable
GOOGLE_IMPERSONATE_SERVICE_ACCOUNTwill be set with the Terraform Service Account to enable impersonation.export GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=$(terraform -chdir="../gcp-bootstrap/envs/shared/" output -raw projects_step_terraform_service_account_email) echo ${GOOGLE_IMPERSONATE_SERVICE_ACCOUNT}
-
Run
initandplanand review output for environment shared../tf-wrapper.sh init shared ./tf-wrapper.sh plan shared
-
To use the
validateoption of thetf-wrapper.shscript, please follow the instructions to install the terraform-tools component. -
Run
validateand check for violations../tf-wrapper.sh validate shared $(pwd)/policy-library ${CICD_PROJECT_ID}
-
Run
applyshared../tf-wrapper.sh apply shared
-
Push your plan branch.
git push --set-upstream origin plan
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/pull/new/plan from the
planbranch to thedevelopmentbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein thedevelopmentenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
developmentbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
developmentenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/actions under
tf-apply. -
If the GitHub action is successful, apply the next environment.
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/pull/new/development from the
developmentbranch to thenonproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein thenonproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
nonproductionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
nonproductionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/actions under
tf-apply. -
If the GitHub action is successful, apply the next environment.
-
Open a pull request in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/pull/new/nonproduction from the
nonproductionbranch to theproductionbranch and review the output. -
The Pull request will trigger a GitHub Action that will run Terraform
init/plan/validatein theproductionenvironment. -
Review the GitHub Action output in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/actions under
tf-pull-request. -
If the GitHub action is successful, merge the pull request in to the
productionbranch. -
The merge will trigger a GitHub Action that will apply the terraform configuration for the
productionenvironment. -
Review merge output in GitHub https://github.com/GITHUB-OWNER/GITHUB-PROJECTS-REPO/actions under
tf-apply. -
Unset the
GOOGLE_IMPERSONATE_SERVICE_ACCOUNTenvironment variable.unset GOOGLE_IMPERSONATE_SERVICE_ACCOUNT