Deploy Docker on Google Cloud with GitHub Actions

Docker • Google Cloud Platform • GitHub Actions

Configuration Files

name: Deploy to Google Cloud Run

on:
  push:
    branches:
      - main

env:
  PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
  SERVICE_NAME: my-app
  REGION: us-central1

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Authenticate to Google Cloud
        uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}

      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v2

      - name: Configure Docker
        run: gcloud auth configure-docker

      - name: Build Docker image
        run: |
          docker build -t gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA .
          docker tag gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA gcr.io/$PROJECT_ID/$SERVICE_NAME:latest

      - name: Push to Container Registry
        run: |
          docker push gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA
          docker push gcr.io/$PROJECT_ID/$SERVICE_NAME:latest

      - 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 \
            --memory 512Mi \
            --cpu 1 \
            --max-instances 10

# Dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package*.json ./
EXPOSE 8080
CMD ["npm", "start"]

Prerequisites

  • Google Cloud account with billing enabled
  • GitHub repository
  • Docker installed locally for testing
  • GCP project created
  • Cloud Run API enabled

Deployment Steps

  • Enable Cloud Run API in GCP Console
  • Create service account with Cloud Run Admin role
  • Generate JSON key for service account
  • Add GCP_SA_KEY and GCP_PROJECT_ID to GitHub Secrets
  • Create Dockerfile in project root
  • Create .github/workflows/deploy.yml
  • Push to main to deploy
  • Access app at generated Cloud Run URL
  • Optional: Add custom domain in Cloud Run console

📝 Additional Notes

  • ⚡ Serverless containers with auto-scaling
  • 🌍 Global load balancing available
  • 💰 Pay only for requests (free tier: 2M requests/month)
  • 🚀 Scale to zero when idle
  • 📦 Container Registry included
  • 🔒 Automatic HTTPS
  • ⚙️ CPU allocated only during requests
  • 📊 Built-in monitoring and logging
  • ⚠️ Cold starts can be 1-3 seconds
  • 💡 Use --min-instances=1 to avoid cold starts (costs more)