Featured image of post Automated Deployment to Google Cloud Platform with GitHub Actions

Automated Deployment to Google Cloud Platform with GitHub Actions

A complete GitHub Actions CI/CD workflow example for building, testing, and automatically deploying applications to GKE and Cloud Run.

Local Testing

Workflow Configuration

name: 'Build and Deploy to GKE/Cloud Run'

on:
  push:
    branches:
      - 'main'
      - 'dev'

env:
  PROJECT_ID: 'random-cloud-project-98765'
  GCLOUD_DOCKER_REGISTRY: 'asia-northeast1-docker.pkg.dev'
  GCLOUD_DOCKER_IMAGE: random-cloud-project-98765/registry/awesome-app-service-${{ github.ref_name == 'main' && github.ref_name || 'staging' }}
  CLOUD_RUN_SERVICE: 'awesome-app-service-staging'
  GKE_NAMESPACE: 'production'
  GKE_DEPLOYMENT_NAME: 'awesome-app'
  GKE_CLUSTER: 'main-cluster'
  GKE_LOCATION: 'europe-west1'

jobs:
  test:
    name: 'Unit Test Placeholder'
    runs-on: 'ubuntu-latest'
    steps:
      - name: 'Unit Test Placeholder'
        id: build
        run: |
          # Run unit tests here
          echo "Run tests..."          

  build:
    name: 'Build and Push Image'
    runs-on: 'ubuntu-latest'
    needs: test
    # Select environment based on branch
    environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }}
    outputs:
      docker_tag: ${{ steps.build.outputs.docker_tag }}

    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
      - name: 'Checkout code'
        uses: 'actions/checkout@v4'
      - id: 'auth'
        name: 'Authenticate to Google Cloud'
        uses: 'google-github-actions/auth@v3'
        with:
          project_id: '${{ env.PROJECT_ID }}'
          credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
      - name: 'Set up Cloud SDK'

        uses: 'google-github-actions/setup-gcloud@v3'
        with:
          version: '>= 363.0.0'
      - name: 'Build image'
        id: build
        run: |
          # Generate tag with date and short SHA
          DATE=$(date +'%Y%m%d')
          SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
          IMAGE_TAG=${{ env.GCLOUD_DOCKER_REGISTRY }}/${{ env.GCLOUD_DOCKER_IMAGE }}:${DATE}-${SHORT_SHA}
          gcloud auth configure-docker ${{ env.GCLOUD_DOCKER_REGISTRY }}
          docker build -t ${IMAGE_TAG} .
          docker push ${IMAGE_TAG}
          # Export image tag for downstream jobs
          echo "docker_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT          
  # Deploy to Cloud Run (dev branch)
  deploy-dev-cloudrun:
    name: 'Deploy to Cloud Run'
    runs-on: 'ubuntu-latest'
    if: github.ref == 'refs/heads/dev'
    needs: build
    environment: 'development'
    permissions:
      contents: 'read'
      id-token: 'write'
    steps:
      - name: 'Checkout code'
        uses: 'actions/checkout@v4'
      - id: 'auth'
        name: 'Authenticate to Google Cloud'
        uses: 'google-github-actions/auth@v3'
        with:
          project_id: '${{ env.PROJECT_ID }}'
          credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
      - id: 'deploy'
        name: 'Deploy to Cloud Run'
        uses: 'google-github-actions/deploy-cloudrun@v3'
        with:
          service: '${{ env.CLOUD_RUN_SERVICE }}'
          image: '${{ needs.build.outputs.docker_tag }}'
      - name: 'Use output'
        run: 'curl "${{ steps.deploy.outputs.url }}"'
  # Deploy to GKE (main branch)
  deploy-gke:
    name: 'Deploy to GKE'
    runs-on: 'ubuntu-latest'
    needs: build
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: 'read'
      id-token: 'write'
    steps:
      - name: 'Checkout code'
        uses: 'actions/checkout@v4'
      - name: Install kubectl
        uses: azure/setup-kubectl@v3
        with:
          version: 'latest'
      - id: 'auth'
        name: 'Authenticate to Google Cloud'
        uses: 'google-github-actions/auth@v3'
        with:
          project_id: '${{ env.PROJECT_ID }}'
          credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
      - uses: google-github-actions/get-gke-credentials@v3
        with:
          cluster_name: ${{ env.GKE_CLUSTER }}
          project_id: '${{ env.PROJECT_ID }}'
          location: ${{ env.GKE_LOCATION }}
      - name: Update deployment image
        run: |
                    kubectl set image deployment/${{ env.GKE_DEPLOYMENT_NAME }} ${{ env.GKE_DEPLOYMENT_NAME }}=${{ needs.build.outputs.docker_tag }} -n ${{ env.GKE_NAMESPACE }}