Effortless Cloud Run Repo Setup with a Handy Repository Template
Are you tired of the repetitive process of deploying with gcloud run deploy
every time you make a change? Do you find managing Cloud Run settings with Terraform a hassle? And copying configurations for GitHub Actions becoming cumbersome? If so, we've got just the solution for you!
Motivation
Cloud Run offers incredible convenience for quickly deploying and testing Proof of Concepts (PoCs) and other applications. However, the manual deployment process and managing configurations can be tedious over time.
Goal
Our goal is to provide a template that streamlines the setup process, allowing you to create repositories with basic configurations effortlessly.
Repository Template
Introducing our repository template, designed to simplify your Cloud Run workflow:
https://github.com/nakamasato/cloud-run-app-template
What’s Included:
Terraform Setup
- Cloud Run service (deployed with the
hello
image) - Configuration for GitHub Actions, including Service Account setup and WorkloadIdentity configuration
GitHub Actions
Automatic execution of Terraform plan and apply on PR creation and merge.
Usage
1. Create a New Repository from the Template
Let’s say you want to create a repository named cloud-run-app-sample
:
NEW_REPO=cloud-run-app-sample
gh repo create $NEW_REPO --public --template=nakamasato/cloud-run-app-template
2. Write a Configuration File for Your Application
Some initial setup is required for Terraform backend and resources used by GitHub Actions. You’ll need to clone the repository locally.
Clone the repository:
gh repo clone $NEW_REPO
cd terraform
Copy the example terraform.tfvars
:
cp terraform.tfvars.example terraform.tfvars
Fill in the terraform.tfvars
file with the necessary information for your Cloud Run application:
project = "<gcp project>"
region = "<gcp region e.g. asia-northeast1>"
service_name = "<cloud run service name e.g. cloud-run-app-sample>"
github_owner = "<github owner e.g. nakamasato>"
github_repository = "<github repository e.g. cloud-run-app-sample>"
github_actions_sa_name = "<GitHub Actions Service Account name e.g. ga-cloud-run-app-sample>"
gh_oidc_pool_id = "<GitHub Actions OIDC Pool ID e.g. ga-cloud-run-app-sample>"
gh_oidc_provider_id = "<GitHub Actions OIDC Provider ID e.g. ga-cloud-run-app-sample>"
For detailed Cloud Run configurations, you can modify the cloud_run.tf
file according to the inputs of the terraform-google-cloud-run
module used internally.
Set up Terraform backend configuration in terraform.tfbackend
:
cp terraform.tfbackend.example terraform.tfbackend
Fill in the backend configuration:
bucket = "<gcs bucket>"
prefix = "<prefix>"
To use a single bucket for Terraform backend across multiple repositories, you can use the repository name as the prefix for clarity.
3. Create GCP Resources
With the configurations in place, it’s time to create GCP resources:
gcloud auth application-default login
terraform init -backend-config=terraform.tfbackend
terraform apply
There might fail during terraform apply
due to necessary APIs being enabled. Retry if you encounter any such failures.
4. Set GitHub Repository Secrets
Set the workload identity provider PROVIDER_NAME
and service account email SA_EMAIL
as GitHub Repository Secrets, which GitHub Actions will use.
Ensure you’re authenticated with gh
:
gh auth login
Set the secrets:
gh secret set PROVIDER_NAME --body=$(terraform output github_actions_provider_name | tr -d '"')
gh secret set SA_EMAIL --body=$(terraform output github_actions_sa_email | tr -d '"')
5. Create a Pull Request!
With everything set up, commit your local changes and create a PR!
GitHub Actions will execute Terraform plan within the PR 🎉
Since you’ve already applied terraform apply
locally, at this point, it should show No changes
.
From here on, any changes to your Cloud Run configurations can be reviewed, merged, and applied through PRs.
Application
Once your repository is set up, you can dive into application development:
ToDo
I'm continually improving the template with more features:
- Building and deploying the latest version of the app image with GitHub Actions
- Option to create separate Cloud Run services for Dev and Prod environments
- Setting up environments for PRs
- Adding Cloud Run monitoring to the template (Setting up SLO monitoring for GCP Cloud Run with Terraform)
This template aims to streamline your Cloud Run workflow, providing a hassle-free experience from setup to deployment. Give it a try and let us know your feedback!