Google Cloud Run Traffic Shift
Route traffic to a specific Cloud Run revision using the Google Cloud Run Traffic Shift step.
The Google Cloud Run Traffic Shift step routes traffic to one or more Cloud Run revisions without redeploying. Use it after a Deploy step in a canary stage to gradually shift traffic to the new revision, and again to promote to 100% once validated.
What you will learn
How to configure the Metadata field to define a traffic distribution across Cloud Run revisions.
How to use
LATESTto refer to the most recently deployed revision versus a named revision.What the step outputs after a traffic shift and how to reference them in subsequent steps.
The
gcloudcommand the step runs to apply the traffic distribution.
Before you begin
A Google Cloud Run Deploy step earlier in the same stage execution, which creates the new revision the Traffic Shift targets.
A Google Cloud Run infrastructure with a GCP connector and region. Go to Google Cloud Run infrastructure.
Step YAML
Route all traffic to the latest revision:
- name: Google Cloud Run Traffic Shift
id: googleCloudRunTrafficShiftStep
template:
uses: googleCloudRunTrafficShiftStep
with:
metadata: '{"status":{"traffic":[{"revisionName":"LATEST","percent":100}]}}'Split traffic 50% to the stable revision and 50% to the new revision:
Configure the step
The following fields appear on the Traffic Shift step. The Metadata field is the only required input; infrastructure fields are resolved from the stage context.
Metadata
The Metadata field defines the target traffic distribution as a JSON object. Harness passes this directly to gcloud run services update-traffic.
Metadata
{"status":{"traffic":[{"revisionName":"LATEST","percent":100}]}}
JSON object defining the target traffic split across revisions. Use LATEST to refer to the most recently deployed revision, or specify a revision name to pin to a specific revision.
To route 20% canary and 80% stable:
Infrastructure configuration
GCP Connector
Infrastructure connector
The GCP connector for authentication. Defaults to the connector in the infrastructure definition.
GCP Region
${{infra.region}}
The Cloud Run service region.
GCP Project
${{infra.project}}
The GCP project.
Command Timeout
10m
Maximum time the step is allowed to run.
Log Level
info
Log verbosity for the step.
Step outputs
SERVICE
The Cloud Run service name.
REVISION_METADATA
The post-shift traffic state, saved as a JSON string.
INSTANCES
Instance count reported by Google Cloud Monitoring after the shift.
DEPLOYMENT_METADATA
Full deployment metadata for Harness instance tracking.
Advanced settings
Timeout duration: Maximum time the step is allowed to run before being terminated.
On failure: Define what happens if the step fails: retry, mark as success, or abort.
Strategy: Configure a looping strategy to run this step over a list of values.
Conditional execution: Run this step only when a specified condition is true.
How it runs
When the Traffic Shift step executes, the following sequence occurs:
Authenticates with GCP using the configured connector. Runs
gcloud auth activate-service-account --key-file sa_key.jsonfor Service Account auth, or identifies the active account for Inherit from Delegate.Configures the gcloud environment: sets project and region.
Saves
SERVICEto step output.Runs
gcloud run services update-traffic <service> --to-revisions=<revision>=<percent>[,<revision>=<percent>]using the distribution from the Metadata field. For a 50/50 canary shift the command is:Cloud Run confirms the result, for example:
50% my-svc-00003-abc / 50% LATEST (currently my-svc-00004-xyz).Runs
gcloud run services describe <service> --format jsonto capture the post-shift traffic state.Saves the post-shift
REVISION_METADATAto step output. For example:Performs instance sync using the Google Cloud Monitoring SDK. If data is found, waits an additional 60 seconds for a stable read.
Publishes
INSTANCESandDEPLOYMENT_METADATAto step output.
Next steps
Go to Google Cloud Run Deploy for the step that creates the revision the Traffic Shift routes to.
Go to Google Cloud Run Rollback for the rollback step that restores traffic on failure.
Go to Canary deployment for the full canary flow using Deploy and Traffic Shift steps.
Last updated
Was this helpful?