๐Ÿ”Cloud Run Deployment Preview Github Actions workflows

Base on official tutorial by GCP which using Cloud Build (Configuring deployment previews | Cloud Run Documentation), I "converted" these flows to Github Actions workflows.

Below is sample deployment previews for develop environment.

preview_deploy.yml
on:
  pull_request:
    branches:
      - develop

name: Build and Deploy Preview WebApp
env:
  PROJECT_ID: ${{ secrets.GCP_PROJECT }}
  SERVICE: ${{ secrets.GCP_PROJECT }}-webapp
  REGION: us-central1

jobs:
  deploy_preview:
    environment: develop
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Cloud SDK
        uses: google-github-actions/[email protected]
        with:
          project_id: ${{ env.PROJECT_ID }}
          service_account_key: ${{ secrets.GCP_SA_KEY }}
          export_default_credentials: true

      - name: Authorize Docker push
        run: gcloud auth configure-docker

      - name: Build and Push Container
        run: |-
          docker build -t gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.event.number }}-${{  github.sha }} .
          docker push gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.event.number }}-${{  github.sha }}

      - name: Deploy to Cloud Run
        id: deploy
        run: |-
          result=$(gcloud run deploy ${{ env.SERVICE }} --image gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.event.number }}-${{  github.sha }} --region ${{ env.REGION }} --tag "pr-${{ github.event.number }}" --no-traffic --ingress internal --allow-unauthenticated --format json)
          preview_url=$(echo $result | jq -r --arg pr "pr-${{ github.event.number }}" '.status.traffic[] | select(.tag == $pr) | .url')
          echo "::set-output name=preview_url::$preview_url"

      - name: Update Pull Request
        uses: actions/[email protected]
        if: github.event_name == 'pull_request'
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const output = `#### Preview URL: \<${{ steps.deploy.outputs.preview_url }}>

            *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`;
              
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: output
            })

The deploy workflow is only intended to run on merged PR, not by direct pushing to the branch. You may replace the trigger with push to suite your need.

Last updated

Was this helpful?