This repository contains a prescriptive implementation of Cloud Build to bootstrap the creation of GCP resources that support the use of GitOps-style Continuous Delivery.
It also contains an example of how a code repository can be setup to support Continuous Deployment into multiple environments (e.g. dev, prod ) using Terraform.
The below diagram illustrates how this can be used to support GitOps with possible integration with a supported external repo.
- Google Cloud SDK
- Terraform version 0.13.7
- An existing Google Cloud organization
- An existing terraform service account (for service account impersonation)
- An existing Cloud Storage bucket for storing Terraform state
- Rename
terraform.example.tfvarstoterraform.tfvarsand update the file with values from your environment - Run
terraform init - Run
terraform plan. Review the plan. - Run
terraform apply. - Run
terraform output sourcerepo_urlto get URLs of the created Cloud Source Repositories.
Note: At this point, the following should have been deployed in your organization
- A GCP Project to contain the resources created
- A Cloud Source Repository for each
source_repodefined - A Cloud Build trigger for each
branch_triggersdefined, persource_repo - An artifact bucket for artifact storage (non-container images)
- An artifact registry for generated Docker containers
You will need to set up your repository in the following structure:
example_repo/
└── environments/
└── dev/
└── prod/
└── modules/
└── module1/
└── module2/
└── ...
- Each environment definition is contained in its own folder in `environments/'
- We make use of the reusability of Terraform modules to prevent drift between the environments.
- Terraform state is stored in a Cloud Storage bucket
Note: Instructions on how to clone a CSR can be found here.
-
Clone the Cloud Source Repository
-
Navigate into the repo
cd gcp_test_repo -
Create a
devbranchgit checkout -b dev
-
Copy contents of example_repo to the new repo
cp -R ../cloud-build-bootstrap/example_repo/ . -
Update
terraform.tfvarsto point to your dev and prod projects# for OSX, use sed -i '' 's/PROJECT_ID/...' sed -i 's/PROJECT_ID/<your_dev_project>/' environments/dev/terraform.tfvars sed -i 's/PROJECT_ID/<your_prod_project>/' environments/prod/terraform.tfvars
-
Update
backend.tfto point to your state bucketsed -i 's/BUCKET_ID/<your_bucket_name/' environments/dev/backend.tf sed -i 's/BUCKET_ID/<your_bucket_name/' environments/prod/backend.tf
-
Commit changes
git add . git commit -m 'initial commit`
-
Push your changes, this should fire off the trigger to deploy in your dev environment
git push --set-upstream origin dev
-
Optional Make some changes to a module in the
devbranch and commit the change -
Merge changes to the
prodbranch. This should fire off the trigger for prod.git checkout -b prod git push --set-upstream origin prod
