google-github-actions / deploy-cloudrun

A GitHub Action for deploying services to Google Cloud Run.
https://cloud.google.com/run
Apache License 2.0
467 stars 115 forks source link

`revision_traffic: 'LATEST=100'` does not deploy the image #533

Closed yamcodes closed 3 months ago

yamcodes commented 3 months ago

TL;DR

If traffic is not already at "latest", there is no way to deploy an image and set traffic to it in the same step.

Expected behavior

The image is deployed and traffic is set to the latest revision (the image we'd just deployed).

Observed behavior

When setting revision_traffic: 'LATEST=100' (as suggested in the docs), along with image, the image is not deployed.

Action YAML

name: "Deploy to Cloud Run"
description: "pnpm-based Build and deploy a service to Google Cloud Run"

inputs:
  workspace-directory:
    description: "The <package_selector> to specify after the pnpm --filter command"
    required: true
  service-name:
    description: "Name of the service"
    required: true
  project-id:
    description: "Google Cloud project ID"
    required: true
  google-application-credentials:
    description: "Google application credentials JSON"
    required: true
  github-token:
    description: "GitHub token"
    required: true
  env-vars:
    description: 'Environment variables to set (comma-separated KEY=VALUE or KEY="VALUE" pairs)'
    required: false
  personal-access-token:
    description: "Personal access token"
    required: true
  build-script:
    description: "The root script to run to build the service. Defaults to 'build'"
    required: false
    default: "build"

runs:
  using: "composite"
  steps:
    - uses: actions/checkout@v4

    - uses: pnpm/action-setup@v4

    - uses: actions/setup-node@v4
      with:
        node-version: "lts/*"
        registry-url: "https://npm.pkg.github.com"
        scope: "@acme"
        cache: "pnpm"

    - name: Configure pnpm Auth for private registry
      if: ${{ inputs.personal-access-token }}
      shell: bash
      run: pnpm config set //npm.pkg.github.com/:_authToken=${{ inputs.personal-access-token }}

    - name: Install dependencies
      shell: bash
      run: pnpm install

    - name: Cache for Turbo
      uses: rharkor/caching-for-turbo@v1.3

    - name: Build
      shell: bash
      run: pnpm run ${{ inputs.build-script }} --filter ./${{ inputs.workspace-directory }}

    - name: Authenticate with Google Cloud
      uses: google-github-actions/auth@v2
      with:
        credentials_json: ${{ inputs.google-application-credentials }}
        project_id: ${{ inputs.project-id }}

    - name: Setup Cloud SDK
      uses: google-github-actions/setup-gcloud@v2
      with:
        project_id: ${{ inputs.project-id }}

    - name: Configure Docker With gcloud
      shell: bash
      run: |
        gcloud auth configure-docker \
          us-central1-docker.pkg.dev

    - name: Build and Push Docker image with gcloud
      shell: bash
      run: |
        gcloud builds submit --tag us-central1-docker.pkg.dev/${{ inputs.project-id }}/${{ inputs.service-name }}/${{ github.ref_name }}:${{ github.sha }} ./${{ inputs.workspace-directory }}

    - name: Deploy to Cloud Run (traffic promoted)
      id: deploy
      uses: google-github-actions/deploy-cloudrun@v2
      with:
        service: ${{ inputs.service-name }}
        image: us-central1-docker.pkg.dev/${{ inputs.project-id }}/${{ inputs.service-name }}/${{ github.ref_name }}:${{ github.sha }}
        project_id: ${{ inputs.project-id }}
        env_vars: ${{ inputs.env-vars }}
        revision_traffic: "LATEST=100"

    - name: Output Cloud Run URL
      shell: bash
      run: |
        echo "Service URL: ${{ steps.deploy.outputs.url }}"

outputs:
  url:
    description: "The URL of the deployed Cloud Run service"
    value: ${{ steps.deploy.outputs.url }}

Log output

Successfully authenticated
Running: gcloud run services update-traffic home --to-revisions LATEST=100 --format json --region us-central1 --project my-project

Additional information

There is an easy-to-miss section in the doc that says:

When using tag_traffic or revision_traffic, the subcommand is gcloud run services update-traffic. For all other values, the subcommand is gcloud run deploy.

However, this is not found in the same paragraph as the revision_traffic documentation, and it's also not what I'd expect in the first place. I would expect an action called deploy-cloud-run to always deploy to Cloud Run, even when the traffic is set to "latest". We should strive to avoid such large side-effects.

Internally, if the command is different, the action should always perform the "deploy" one, and additionally perform the "traffic promotion" one.