iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💭

Canary Deployment of Cloud Run via Cloud Deploy and GitHub Actions

に公開

Goal

In this article, I will summarize the configuration for performing canary deployments of Cloud Run via Cloud Deploy using GitHub Actions.

The resulting artifacts are stored in the following repository.
https://github.com/tetsuya28/samples/tree/main/.github/workflows
https://github.com/tetsuya28/samples/tree/main/gcp/cloud-run

Cloud Deploy

Creating the Cloud Run Manifest

For this guide, we will use a simple manifest that just deploys Nginx.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: sample
  labels:
    cloud.googleapis.com/location: asia-northeast1
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/maxScale: '1'
        run.googleapis.com/startup-cpu-boost: 'true'
    spec:
      containerConcurrency: 80
      containers:
      - name: nginx
        image: nginx
        ports:
        - name: http1
          containerPort: 80
        resources:
          limits:
            cpu: 1000m
            memory: 512Mi
        startupProbe:
          timeoutSeconds: 240
          periodSeconds: 240
          failureThreshold: 1
          tcpSocket:
            port: 80

Defining Skaffold

Overview

scaffold.yaml
apiVersion: skaffold/v4beta7
kind: Config
metadata:
  name: sample
deploy:
  cloudrun: {}
profiles:
  - name: dev
    manifests:
      rawYaml:
        - service.yaml

Details

The following configuration is added to use it as a Cloud Run manifest.

deploy:
  cloudrun: {}

Since there is only one environment this time, I have created a profile named dev.
The dev profile is configured to render the target Cloud Run manifest.

Defining the Cloud Deploy Pipeline

Overview

cloud-deploy.yaml
apiVersion: deploy.cloud.google.com/v1
kind: DeliveryPipeline
metadata:
  name: cloud-run-cloud-deploy
description: Cloud Run with Cloud Deploy
serialPipeline:
  stages:
    - targetId: sample
      profiles: [dev]
      strategy:
        canary:
          runtimeConfig:
            cloudRun:
              automaticTrafficControl: true
          canaryDeployment:
            percentages: [5, 90]
            verify: false
---
apiVersion: deploy.cloud.google.com/v1
kind: Target
metadata:
  name: sample
run:
  location: projects/tetsuya28-playground/locations/asia-northeast1
requireApproval: true # Requires approval for deployment

Details

Create a stage that uses the dev profile created with Skaffold.
In this stage, configuration for performing canary releases when updating Cloud Run revisions is added.
In the following settings, traffic will automatically shift to the new revision in the sequence of 5% → 90% → 100%.

serialPipeline:
  stages:
    - targetId: sample
      profiles: [dev]
      strategy:
        canary:
          runtimeConfig:
            cloudRun:
              automaticTrafficControl: true
          canaryDeployment:
            percentages: [5, 90]
            verify: false

Specify the Cloud Run deployment destination as the asia-northeast1 (Tokyo) region of the tetsuya28-playground project.

apiVersion: deploy.cloud.google.com/v1
kind: Target
metadata:
  name: sample
run:
  location: projects/tetsuya28-playground/locations/asia-northeast1

Additionally, the following setting is added to require approval at the start of the pipeline.

requireApproval: true # Requires approval for deployment

GitHub Actions

Since the Cloud Deploy configuration is now complete, I will organize the structure for executing these via GitHub Actions.

Pipeline

Update the pipeline using GitHub Actions when the Cloud Deploy pipeline itself is modified.
This assumes that authentication from GitHub Actions to Google Cloud is already completed.
The authentication mechanism using OIDC is introduced on the official Google Cloud blog.
https://cloud.google.com/blog/ja/products/identity-security/enabling-keyless-authentication-from-github-actions

- uses: google-github-actions/setup-gcloud@v2
- run: |
    gcloud deploy apply --file gcp/cloud-run/cloud-deploy.yaml --region asia-northeast1

The gcloud command is used directly to update the pipeline.

Deploy

When creating a release for the configured pipeline, use the action officially provided by Google Cloud.
https://github.com/google-github-actions/create-cloud-deploy-release

Since the release name must be unique and is limited to 63 characters, we will use the short git commit SHA.

- name: Set short git commit SHA
  run: |
    calculatedSha=$(git rev-parse --short ${{ github.sha }})
    echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV

Specify the pipeline name, region, and Skaffold file to be used.
Image updates should also be handled here, but in this case, we are providing a setting to use the nginx image for the nginx container without any specific updates.

- uses: google-github-actions/create-cloud-deploy-release@v1
  with:
    name: ${{ inputs.called_by }}-${{ env.COMMIT_SHORT_SHA }}
    delivery_pipeline: cloud-run-cloud-deploy
    region: asia-northeast1
    skaffold_file: gcp/cloud-run/scaffold.yaml
    images: |
      nginx=nginx

Running GitHub Actions in this state will create a release on Cloud Deploy.

By executing "Approve", the release will actually start.
The following shows the state where the initial 5% of the canary release is being executed.

In Cloud Run, you can confirm that 5% of the traffic is actually flowing to the latest revision.

Conclusion

I tried using Cloud Deploy for deployments to Cloud Run, and it was relatively intuitive to set up, making it a service that seems very useful even for small development teams.
It is also great that Cloud Deploy visualizes Four Keys[1] metrics by default.

Moving forward, I plan to build progressive deployments—further evolving from canary deployments—using Cloud Deploy and work on introducing them into actual services.

脚注
  1. https://cloud.google.com/blog/ja/products/gcp/using-the-four-keys-to-measure-your-devops-performance ↩︎

Discussion