TerraRun is a project that automates the deployment of containerized applications to Google Cloud Run using GitHub Actions and Terraform. This guide will help you set up, configure, and deploy your application with ease.
- β¨ Overview
- π Features
- π Prerequisites
- βοΈ Setup
- π€ GitHub Actions Workflow
- π οΈ Terraform Configuration
- π¦ Usage
- β Troubleshooting
- π References
TerraRun automates the following:
| Task | Description |
|---|---|
| π Dockerization | Builds and pushes Docker images to Google Artifact Registry (GAR). |
| βοΈ Cloud Deployment | Deploys Docker containers to Google Cloud Run. |
| π CI/CD Workflow | Uses GitHub Actions for seamless build and deployment automation. |
- π§ Infrastructure as Code: Terraform manages the entire infrastructure.
- π¦ Containerization: Leverages Docker for consistent and portable deployments.
- π€ CI/CD Integration: GitHub Actions automate the build-deploy pipeline.
- π GCP Integration: Securely deploys applications on Google Cloud Platform.
Ensure you have the following:
| Requirement | Description |
|---|---|
| π οΈ Google Cloud Project | A GCP project with Cloud Run and Artifact Registry enabled. |
| π GCP Credentials | A service account key in JSON format with permissions to manage Cloud Run and GAR. |
| π GitHub Secrets | Add the required secrets for the workflow (see below). |
| π³ Docker | Installed and running locally for testing and building images. |
| Secret Name | Description |
|---|---|
GCP_CREDS |
JSON credentials for GCP. |
GAR_REGION |
Region for GAR (e.g., us-central1). |
PROJECT_ID |
GCP Project ID. |
GAR_REPO_NAME |
Name of the GAR repository. |
GAR_IMAGE |
Name of the Docker image. |
GCR_PORT |
Cloud Run container port (e.g., 8080). |
GCR_AUTH |
Authentication for Cloud Run (allUsers or specific account). |
GCR_ROLE |
IAM role for Cloud Run (roles/run.invoker). |
TF_BUCKET |
GCS bucket name for storing Terraform state. |
GCP_REGION |
Region for GCP services. |
-
Clone the repository:
git clone https://github.com/your-username/TerraRun.git cd TerraRun -
Install dependencies:
npm install
-
Ensure Docker is installed and running.
-
Enable the required APIs:
- Cloud Run API
- Artifact Registry API
- IAM API
-
Create a Google Artifact Registry:
gcloud artifacts repositories create <GAR_REPO_NAME> --repository-format=docker --location=<GAR_REGION> --description="Docker repository for TerraRun"
-
Set up a service account:
- Assign the required roles: Artifact Registry Admin, Cloud Run Admin, and IAM Admin.
- Generate a JSON key and add it as the
GCP_CREDSsecret in GitHub.
The workflow automates the CI/CD pipeline:
| Step | Description |
|---|---|
| π¦ Checkout Code | Fetches the latest code from the repository. |
| π§ Setup Node.js | Installs Node.js version 20 for the project. |
| π Install Dependencies | Runs npm install to install project dependencies. |
| π Authenticate to GCP | Authenticates using the provided service account credentials. |
| π Build Docker Image | Builds the Docker image and tags it with the commit SHA. |
| βοΈ Push to GAR | Pushes the Docker image to Google Artifact Registry. |
| π οΈ Terraform Apply | Deploys the Cloud Run service using the Terraform configuration. |
The main.tf file manages the infrastructure:
| Resource | Description |
|---|---|
| π Cloud Run Service | Deploys the Docker image to Cloud Run. |
| π IAM Policy | Configures access control for the Cloud Run service. |
| π¦ Docker Image Variable | Uses the image pushed to GAR for deployment. |
| Variable Name | Description |
|---|---|
TF_VAR_gcp_project_id |
GCP Project ID. |
TF_VAR_gcp_region |
GCP Region for deployment. |
TF_VAR_cloud_run_location |
Region for Cloud Run. |
TF_VAR_image_name |
Full path of the Docker image. |
TF_VAR_cloud_run_container_port |
Cloud Run container port (e.g., 8080). |
TF_VAR_cloud_run_auths |
Authentication for Cloud Run (allUsers or specific account). |
TF_VAR_cloud_run_invoker |
IAM role for Cloud Run invocation (roles/run.invoker). |
Push changes to the master branch to start the GitHub Actions workflow:
git push origin master -
Initialize Terraform:
terraform init -backend-config="bucket=<TF_BUCKET>" -
Apply Terraform Configuration:
terraform apply -auto-approve
-
Verify Deployment:
gcloud run services list
| Issue | Solution |
|---|---|
| β Authentication Error | Ensure the GCP_CREDS secret is correctly configured and matches the GCP JSON key. |
| π οΈ Terraform State Error | Verify the GCS bucket for Terraform state is properly configured and accessible. |
| Check Cloud Run logs for detailed errors: | |
bash gcloud run services logs read <SERVICE_NAME> |
Enjoy deploying with TerraRun! π




