GitHub Actions Workflow for Deploying Applications to Google Cloud Run

I need a GitHub Actions workflow that automatically deploys my application to Google Cloud Run whenever I push changes to the main branch. Can you provide a template?

1 Answers

āœ“ Best Answer
```html

šŸš€ Automating Google Cloud Run Deployments with GitHub Actions

Here's a comprehensive guide and a sample workflow to automate deployments to Google Cloud Run using GitHub Actions. This setup assumes you have a containerized application ready for deployment.

Prerequisites

  • āœ… A Google Cloud Project with Cloud Run enabled.
  • šŸ”‘ A Service Account with the necessary permissions (roles/run.developer and roles/iam.serviceAccountUser).
  • šŸ“¦ Your application containerized and pushed to a container registry (e.g., Google Container Registry, Docker Hub).
  • ā˜ļø Google Cloud SDK (gcloud) configured in your environment.

Steps to Create the Workflow

  1. Create a GitHub Repository: If you don't have one already.
  2. Set up Google Cloud Credentials: Store your Service Account key as a GitHub Secret. Go to your repository's Settings -> Secrets -> Actions and add a new secret named GCLOUD_CREDENTIALS. The value should be the contents of your Service Account key JSON file.
  3. Create the GitHub Actions Workflow file: Create a new file in your repository at .github/workflows/deploy-to-cloud-run.yml.

Sample Workflow File (.github/workflows/deploy-to-cloud-run.yml)

name: Deploy to Google Cloud Run

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

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

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

    - name: Deploy to Cloud Run
      run: |
        gcloud run deploy your-service-name \
          --image gcr.io/your-project-id/your-image:latest \
          --region your-region \
          --platform managed \
          --allow-unauthenticated

Explanation

  • name: Deploy to Google Cloud Run: The name of the workflow.
  • on: push: Triggers the workflow on pushes to the main branch.
  • jobs: deploy: Defines the deployment job.
  • uses: actions/checkout@v3: Checks out your repository code.
  • uses: google-github-actions/auth@v1: Authenticates to Google Cloud using the provided credentials.
  • uses: google-github-actions/setup-gcloud@v1: Sets up the Google Cloud SDK.
  • gcloud run deploy ...: Deploys the application to Cloud Run. Replace your-service-name, gcr.io/your-project-id/your-image:latest, and your-region with your actual service name, image URL, and region.

Important Considerations

  • Service Account Permissions: Ensure your service account has the correct permissions.
  • Image Tagging: Consider using more specific image tags (e.g., commit SHA) instead of latest for better version control.
  • Environment Variables: If your application requires environment variables, you can set them using the --set-env-vars flag in the gcloud run deploy command.
  • Custom Domains: After deployment, you can map a custom domain to your Cloud Run service.

Example with Environment Variables

name: Deploy to Google Cloud Run with Env Vars

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

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

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

    - name: Deploy to Cloud Run
      run: |
        gcloud run deploy your-service-name \
          --image gcr.io/your-project-id/your-image:latest \
          --region your-region \
          --platform managed \
          --allow-unauthenticated \
          --set-env-vars API_KEY=your-api-key,DATABASE_URL=your-database-url

Remember to replace the placeholder values with your actual information. This workflow provides a basic setup, and you can customize it further to fit your specific needs.

Know the answer? Login to help.