Skip to content

Instantly share code, notes, and snippets.

@madebycm
Last active July 28, 2025 20:49
Show Gist options
  • Select an option

  • Save madebycm/37dbf4853f97eec3f0142a65cd8967f1 to your computer and use it in GitHub Desktop.

Select an option

Save madebycm/37dbf4853f97eec3f0142a65cd8967f1 to your computer and use it in GitHub Desktop.
GCLOUD.md

Google Cloud Deployment Guide

Prerequisites

  1. Install Google Cloud CLI: brew install google-cloud-sdk
  2. Authenticate: gcloud auth login
  3. Set project: gcloud config set project YOUR_PROJECT_ID
  4. Enable APIs:
    gcloud services enable run.googleapis.com
    gcloud services enable containerregistry.googleapis.com
  5. Configure Docker auth: gcloud auth configure-docker

Example Dockerfile

FROM node:18-alpine AS base

# Install dependencies
FROM base AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

# Build application
FROM base AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production image
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/build ./build
COPY package*.json ./

# Important: Cloud Run uses PORT=8080
EXPOSE 8080
ENV PORT=8080

CMD ["npm", "start"]

Docker Build & Deploy

1. Build and Push Image

# Set variables
PROJECT_ID="your-project-id"
SERVICE_NAME="your-service-name"
REGION="your-region"
IMAGE_NAME="gcr.io/$PROJECT_ID/$SERVICE_NAME"

# Build locally (use --platform for Apple Silicon compatibility)
docker build --platform linux/amd64 -t $IMAGE_NAME .

# Push to GCR
docker push $IMAGE_NAME

2. Deploy to Cloud Run

gcloud run deploy $SERVICE_NAME \
  --image=$IMAGE_NAME \
  --project=$PROJECT_ID \
  --region=$REGION \
  --allow-unauthenticated \
  --memory=1Gi \
  --cpu=1 \
  --max-instances=10 \
  --min-instances=0 \
  --concurrency=100

3. Map Custom Domain

# Create domain mapping
gcloud run domain-mappings create \
  --service=$SERVICE_NAME \
  --domain=your-domain.com \
  --region=$REGION \
  --project=$PROJECT_ID

# Check status
gcloud run domain-mappings describe \
  --domain=your-domain.com \
  --region=$REGION \
  --project=$PROJECT_ID

4. Update DNS

Point your domain's DNS to the Cloud Run service:

  • Add CNAME record: your-domain.comghs.googlehosted.com

Common Regions

  • us-central1 (Iowa)
  • europe-west1 (Belgium)
  • asia-east1 (Taiwan)
  • europe-north1 (Finland)

Example Deploy Script

#!/bin/bash
# @author madebycm (2025)
# Deploy script for Google Cloud Run

# Configuration
PROJECT_ID="my-project-id"
SERVICE_NAME="my-app"
REGION="us-central1"
IMAGE_NAME="gcr.io/$PROJECT_ID/$SERVICE_NAME"
DOMAIN="app.example.com"

set -e

echo "Building Docker image..."
docker build --platform linux/amd64 -t $IMAGE_NAME .

echo "Pushing to Google Container Registry..."
docker push $IMAGE_NAME

echo "Deploying to Cloud Run..."
gcloud run deploy $SERVICE_NAME \
  --image=$IMAGE_NAME \
  --project=$PROJECT_ID \
  --region=$REGION \
  --allow-unauthenticated \
  --memory=1Gi \
  --cpu=1 \
  --max-instances=10 \
  --min-instances=0 \
  --concurrency=100 \
  --set-env-vars="NODE_ENV=production"

echo "Mapping custom domain..."
gcloud run domain-mappings create \
  --service=$SERVICE_NAME \
  --domain=$DOMAIN \
  --region=$REGION \
  --project=$PROJECT_ID || echo "Domain mapping may already exist"

echo "Deployment complete!"
echo "Service URL: https://$DOMAIN"

# Check domain mapping status
gcloud run domain-mappings describe \
  --domain=$DOMAIN \
  --region=$REGION \
  --project=$PROJECT_ID

Useful Commands

# List services
gcloud run services list

# View logs
gcloud run logs tail $SERVICE_NAME --region=$REGION

# Delete service
gcloud run services delete $SERVICE_NAME --region=$REGION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment