# Deployment

This guide provides a comprehensive walkthrough for deploying your multi-language compiler application—including the Rust backend, Next.js frontend, and PostgreSQL database—to a scalable, production-ready environment on Google Cloud Platform (GCP). We will use modern, serverless technologies to minimize infrastructure management.

Core GCP Services We Will Use:

* Cloud SQL for PostgreSQL: A fully managed relational database service.
* Artifact Registry: A private registry to store and manage your Docker container images.
* Cloud Run: A serverless platform to run your containerized applications.
* Secret Manager: For securely storing sensitive data like database passwords.

**Prerequisites**

Before you begin, ensure you have the following:

1. A Google Cloud Platform Account: With an active billing account.
2. Google Cloud CLI (`gcloud`): Installed and authenticated on your local machine.
3. Docker: Installed and running on your local machine.

***

**Step 1: Set Up Your GCP Project**

First, create and configure a new GCP project.

1. Create a Project:
   * Go to the [GCP Console](https://console.cloud.google.com/) and create a new project (e.g., `my-compiler-app`).
   * Make a note of your Project ID.
2. Set Project in gcloud CLI:

   Bash

   ```bash
   gcloud config set project YOUR_PROJECT_ID
   ```
3. Enable Required APIs: Enable the APIs for the services we will be using.

   Bash

   ```bash
   gcloud services enable \
       sqladmin.googleapis.com \
       artifactregistry.googleapis.com \
       run.googleapis.com \
       secretmanager.googleapis.com
   ```

**Step 2: Create the PostgreSQL Database**

We will create a managed PostgreSQL instance using Cloud SQL.

1. Create the Cloud SQL Instance: Choose a name for your instance (e.g., `compiler-db-instance`) and set a strong password.

   Bash

   ```bash
   gcloud sql instances create compiler-db-instance \
       --database-version=POSTGRES_14 \
       --region=us-central1 \
       --root-password="YOUR_STRONG_PASSWORD"
   ```
2. Create the Database: Inside your new instance, create the specific database your application will use.

   Bash

   ```bash
   gcloud sql databases create compiler_db --instance=compiler-db-instance
   ```
3. Store the Password in Secret Manager: It's best practice to store the database password securely.

   Bash

   ```bash
   echo "YOUR_STRONG_PASSWORD" | gcloud secrets create db-password --data-file=-
   ```

***

**Step 3: Set Up Artifact Registry**

This is where you will store your Docker images.

1. Create a Docker Repository:

   Bash

   ```bash
   gcloud artifacts repositories create compiler-repo \
       --repository-format=docker \
       --location=us-central1 \
       --description="Docker repository for compiler app"
   ```
2. Configure Docker Authentication: This command configures your local Docker client to authenticate with your new Artifact Registry repository.

   Bash

   ```bash
   gcloud auth configure-docker us-central1-docker.pkg.dev
   ```

***

**Step 4: Build and Push Your Docker Images**

Now, we will build the Docker images for your `rust_compiler` and `web` services and push them to the Artifact Registry.

1. Build and Push the Backend (Rust) Image:
   * Navigate to the root of your project directory.
   * Define your image name. Replace `YOUR_PROJECT_ID` with your actual GCP Project ID.

     Bash

     ```bash
     export BACKEND_IMAGE=us-central1-docker.pkg.dev/YOUR_PROJECT_ID/compiler-repo/rust-compiler:latest
     ```
   * Build and push the image using your existing `Dockerfile`.

     Bash

     ```bash
     docker build -t $BACKEND_IMAGE -f Dockerfile .
     docker push $BACKEND_IMAGE
     ```
2. Build and Push the Frontend (Next.js) Image:
   * Navigate to your `web` directory.
   * Define your image name.

     Bash

     ```bash
     export FRONTEND_IMAGE=us-central1-docker.pkg.dev/YOUR_PROJECT_ID/compiler-repo/next-frontend:latest
     ```
   * Build and push the image.

     Bash

     ```bash
     docker build -t $FRONTEND_IMAGE -f Dockerfile .
     docker push $FRONTEND_IMAGE
     ```

***

**Step 5: Deploy the Services to Cloud Run**

We will deploy the backend first, then the frontend.

**5.1. Deploy the Backend Service**

1. Get the Cloud SQL Connection Name: You'll need this to securely connect Cloud Run to Cloud SQL.

   Bash

   ```
   gcloud sql instances describe compiler-db-instance --format='value(connectionName)'
   ```

   (The output will look like `YOUR_PROJECT_ID:us-central1:compiler-db-instance`)
2. Deploy to Cloud Run: This command deploys your backend container and securely connects it to the database.

   Bash

   ```bash
   gcloud run deploy rust-compiler-service \
       --image=$BACKEND_IMAGE \
       --platform=managed \
       --region=us-central1 \
       --allow-unauthenticated \
       --add-cloudsql-instances=<YOUR_CONNECTION_NAME> \
       --set-env-vars="DATABASE_URL=postgresql://postgres:YOUR_STRONG_PASSWORD@localhost/compiler_db?host=/cloudsql/<YOUR_CONNECTION_NAME>"
   ```

   * Replace `<YOUR_CONNECTION_NAME>` with the value from the previous command.
   * Important: The `DATABASE_URL` format is specific to Cloud SQL's proxy connection.
   * When prompted, allow unauthenticated invocations for now (for public access).
3. Note the Service URL: After deployment, `gcloud` will print the Service URL. Copy this; you will need it for the frontend.

**5.2. Deploy the Frontend Service**

Now, deploy the Next.js frontend and point it to the backend service.

Bash

```bash
gcloud run deploy next-frontend-service \
    --image=$FRONTEND_IMAGE \
    --platform=managed \
    --region=us-central1 \
    --allow-unauthenticated \
    --set-env-vars="NEXT_PUBLIC_API_URL=<YOUR_BACKEND_SERVICE_URL>"
```

* Replace `<YOUR_BACKEND_SERVICE_URL>` with the URL you copied after deploying the backend.

***

**Step 6: Final Steps and Verification**

1. Apply Database Migrations: Your database is currently empty. The easiest way to apply Prisma migrations is to temporarily connect to your Cloud SQL instance from your local machine and run the command. Follow the [Cloud SQL Proxy guide](https://www.google.com/search?q=https://cloud.google.com/sql/docs/postgres/connect-proxy) for a secure connection, then run:

   Bash

   ```
   npx prisma migrate deploy
   ```
2. Access Your Application: Go to the URL of your `next-frontend-service` provided by Cloud Run. Your application should now be live, fully deployed on GCP!s
