Simplifying GCP Cloud Run Deployment with GitHub Actions

Are you looking to streamline your application deployment process on Google Cloud Platform (GCP)? GitHub Actions combined with GCP services offer an efficient solution. In this guide, we’ll walk you through setting up a Cloud Run deployment pipeline using GitHub Actions, making deployment a breeze for your team. Whether you’re new to GitHub Actions or GCP, this guide will help you get started.

Introduction

GitHub Actions allow you to automate tasks directly within your GitHub repository, including CI/CD workflows. With the power of Google Cloud Platform, we can seamlessly deploy applications to Cloud Run, a fully managed serverless platform.

Getting Started

To follow along, clone the repository from GitHub.

Step 1: Understanding the YAML Files

pipeline.yaml

# Define deployment jobs for different branches
on:
  push:
    branches: [develop, stage, prod]

jobs:
  dev:
    name: Deploy to Development Environment
    # Specify deployment workflow for 'develop' branch
    if: ${{ github.ref == 'refs/heads/develop' || github.base_ref == 'develop' }}
    uses: littleworks-inc/gcp-cloud-run/.github/workflows/deployment.yml@main
    # Define environment variables
    with:
      environment: dev
      ref: ${{ github.sha }}
      PROJECT_ID: dummy_project_id
      REGION: dummy_region
      APP: gcpcli
    # Define secrets required for authentication
    secrets:
      workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
      service_account: ${{ secrets.SERVICE_ACCOUNT }}
  # Similar configuration for staging and production environments

deployment.yml

# Define workflow for building and deploying application
name: Build app and create a release in Cloud Deploy

on:
  workflow_call:
    inputs:
      environment:
        type: string
        required: true
      ref:
        type: string
        required: true
      PROJECT_ID:
        type: string
        required: true
      REGION:
        type: string
        required: true
      APP:
        type: string
        required: true
    secrets:
      workload_identity_provider:
        required: true
      service_account:
        required: true

jobs:
  deploy:
    # Define permissions for workflow
    permissions:
      contents: 'read'
      id-token: 'write'

    runs-on: ubuntu-latest
    # Define environment variables
    env:
      PROJECT_ID: ${{ inputs.PROJECT_ID }}
      REGION: ${{ inputs.REGION }}
      APP: ${{ inputs.APP }}
      workload_identity_provider: ${{ secrets.workload_identity_provider }}
      service_account: ${{ secrets.service_account }}
    steps:
      # Define workflow steps
      # Checkout, Authenticate to Google Cloud, Set up Cloud SDK, Docker Authentication, Build and Push Container Image, Render Templated Config Manifests, Create Cloud Deploy Delivery Pipeline, Create Release Name, Create Cloud Deploy Release, Report Cloud Deploy Release

Step 2: Adding Secrets

To add secrets in GitHub Actions:

  1. Go to your GitHub repository.
  2. Navigate to “Settings” > “Secrets” > “New repository secret.”
  3. Add your secret values for WORKLOAD_IDENTITY_PROVIDER and SERVICE_ACCOUNT.

Step 3: Customizing Variables

Replace the dummy variables (dummy_project_id and dummy_region) with your actual project ID and desired region.

Conclusion

With GitHub Actions and GCP, deploying applications to Cloud Run becomes effortless. By automating the deployment pipeline, you can focus on developing quality software without worrying about manual deployment processes.

Now that you’ve learned the basics, experiment with different configurations and workflows to suit your project’s needs. Happy coding!

Stay tuned for more insights on GitHub Actions, GCP, and DevOps practices on our blog.

Additional Resources

Ready to streamline your deployment process? Start automating with GitHub Actions and GCP today!