From 16bc2b0dac9790c06ad22f7dae44cbe400b9deab Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 3 Apr 2025 11:15:03 -0700 Subject: [PATCH] infra.new code version 0.0.1 --- .github/workflows/deploy.yml | 42 +++++++++++++++++++++++++ cloudbuild.yaml | 27 ++++++++++++++++ infra/environments/dev/main.tf | 47 ++++++++++++++++++++++++++++ infra/environments/dev/variables.tf | 2 ++ infra/environments/prod/main.tf | 47 ++++++++++++++++++++++++++++ infra/environments/prod/variables.tf | 2 ++ 6 files changed, 167 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 cloudbuild.yaml create mode 100644 infra/environments/dev/main.tf create mode 100644 infra/environments/dev/variables.tf create mode 100644 infra/environments/prod/main.tf create mode 100644 infra/environments/prod/variables.tf diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9354dc1 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,42 @@ + +name: Build and Deploy to Cloud Run + +on: + push: + branches: + - main + +env: + PROJECT_ID: launchflow-services-dev + SERVICE_NAME: fastapi-app + REGION: us-west1 + +jobs: + setup-build-deploy: + name: Setup, Build, and Deploy + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Google Auth + id: auth + uses: google-github-actions/auth@v1 + with: + credentials_json: ${{ secrets.GCP_SA_KEY }} + + - name: Set up Cloud SDK + uses: google-github-actions/setup-gcloud@v1 + + - name: Build and push Docker image + run: | + gcloud builds submit --tag gcr.io/$PROJECT_ID/$SERVICE_NAME:${{ github.sha }} + + - name: Deploy to Cloud Run + run: | + gcloud run deploy $SERVICE_NAME \ + --image gcr.io/$PROJECT_ID/$SERVICE_NAME:${{ github.sha }} \ + --platform managed \ + --region $REGION \ + --allow-unauthenticated diff --git a/cloudbuild.yaml b/cloudbuild.yaml new file mode 100644 index 0000000..8266131 --- /dev/null +++ b/cloudbuild.yaml @@ -0,0 +1,27 @@ + +steps: + # Build the container image + - name: 'gcr.io/cloud-builders/docker' + args: ['build', '-t', 'gcr.io/$PROJECT_ID/fastapi-app:$COMMIT_SHA', '.'] + + # Push the container image to Container Registry + - name: 'gcr.io/cloud-builders/docker' + args: ['push', 'gcr.io/$PROJECT_ID/fastapi-app:$COMMIT_SHA'] + + # Deploy container image to Cloud Run + - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' + entrypoint: gcloud + args: + - 'run' + - 'deploy' + - 'fastapi-app' + - '--image' + - 'gcr.io/$PROJECT_ID/fastapi-app:$COMMIT_SHA' + - '--region' + - 'us-west1' + - '--platform' + - 'managed' + - '--allow-unauthenticated' + +images: + - 'gcr.io/$PROJECT_ID/fastapi-app:$COMMIT_SHA' diff --git a/infra/environments/dev/main.tf b/infra/environments/dev/main.tf new file mode 100644 index 0000000..3aa1cdf --- /dev/null +++ b/infra/environments/dev/main.tf @@ -0,0 +1,47 @@ + +provider "google" { + project = "launchflow-services-dev" + region = "us-west1" +} + +resource "google_cloud_run_v2_service" "fastapi_service" { + name = "fastapi-app" + location = "us-west1" + + template { + containers { + image = "gcr.io/launchflow-services-dev/fastapi-app:latest" + + ports { + container_port = 80 + } + + resources { + limits = { + cpu = "1" + memory = "512Mi" + } + } + } + + scaling { + min_instance_count = 0 + max_instance_count = 2 + } + } + + # Allow unauthenticated access + depends_on = [google_cloud_run_service_iam_member.public_access] +} + +resource "google_cloud_run_service_iam_member" "public_access" { + location = google_cloud_run_v2_service.fastapi_service.location + service = google_cloud_run_v2_service.fastapi_service.name + role = "roles/run.invoker" + member = "allUsers" +} + +# Output the service URL +output "service_url" { + value = google_cloud_run_v2_service.fastapi_service.uri +} diff --git a/infra/environments/dev/variables.tf b/infra/environments/dev/variables.tf new file mode 100644 index 0000000..3c41c9b --- /dev/null +++ b/infra/environments/dev/variables.tf @@ -0,0 +1,2 @@ + +# No variables needed for this basic setup diff --git a/infra/environments/prod/main.tf b/infra/environments/prod/main.tf new file mode 100644 index 0000000..324f2e3 --- /dev/null +++ b/infra/environments/prod/main.tf @@ -0,0 +1,47 @@ + +provider "google" { + project = "launchflow-services-dev" # Change this to your production project ID + region = "us-west1" +} + +resource "google_cloud_run_v2_service" "fastapi_service" { + name = "fastapi-app" + location = "us-west1" + + template { + containers { + image = "gcr.io/launchflow-services-dev/fastapi-app:latest" # Update with your production image + + ports { + container_port = 80 + } + + resources { + limits = { + cpu = "2" + memory = "1Gi" + } + } + } + + scaling { + min_instance_count = 1 # Always have at least one instance running + max_instance_count = 5 # Scale up to 5 instances + } + } + + # Allow unauthenticated access + depends_on = [google_cloud_run_service_iam_member.public_access] +} + +resource "google_cloud_run_service_iam_member" "public_access" { + location = google_cloud_run_v2_service.fastapi_service.location + service = google_cloud_run_v2_service.fastapi_service.name + role = "roles/run.invoker" + member = "allUsers" +} + +# Output the service URL +output "service_url" { + value = google_cloud_run_v2_service.fastapi_service.uri +} diff --git a/infra/environments/prod/variables.tf b/infra/environments/prod/variables.tf new file mode 100644 index 0000000..3c41c9b --- /dev/null +++ b/infra/environments/prod/variables.tf @@ -0,0 +1,2 @@ + +# No variables needed for this basic setup