An example/practice repository for setting up a CI/CD pipeline. Set up on GCP with Terraform.
Based on chapter 7 of 'Terraform in Action', but with some changes of my own.
The pipeline:
- Code is pushed to Cloud Source Repositories
- This triggers a Cloud Build build, with the template based on the
buildtemplate.yaml
file - The Cloud Build:
- Tests the code with pytest
- Builds the Docker app in the
app
folder - Pushes the image to the Container Registry
- Deploys the image to Cloud Run
- Create a GCP project
- Create a service account with the Project/Editor, Project IAM Admin, Security Admin, and Source Repository Administrator roles.
- Create and download a key for the service account
- Create a
terraform.tfvars
file in theterraform
folder that contains theproject
variable with your project ID and thecredentials
variable with the location of your credentials file. - Enable the following APIs for your project:
- https://console.cloud.google.com/apis/library/sourcerepo.googleapis.com
- https://console.cloud.google.com/apis/library/cloudbuild.googleapis.com
- https://console.cloud.google.com/apis/library/run.googleapis.com
- https://console.cloud.google.com/apis/library/iam.googleapis.com
- https://console.cloud.google.com/apis/library/cloudresourcemanager.googleapis.com
- Run
terraform init
- Run
terraform apply
- Type
yes
and hit enter. - You can run
terraform destroy
to destroy the provisioned resources.
Terraform will output 2 urls. One is the url of your app, the other is the url of the Cloud Source Repository. You can set this as a remote for the git repository you have with git remote add google <url>
and then push to that remote with git push --all google
You can set a Cloud Source Repository to mirror a GitHub repository, but you can't (yet) do this with Terraform, so if you want to do this you will have to do it manually.