> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/3.0/use-deployments/overview-3/gcr-deploy/gcr-traffic-shift.md).

# 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.

***

## Step YAML

Route all traffic to the latest revision:

```yaml
- 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:

```yaml
- name: Google Cloud Run Traffic Shift
  id: googleCloudRunTrafficShiftStep
  template:
    uses: googleCloudRunTrafficShiftStep
    with:
      metadata: '{"status":{"traffic":[{"revisionName":"<stable-revision>","percent":50},{"revisionName":"LATEST","percent":50}]}}'
```

***

## 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](/continuous-delivery/3.0/use-deployments/overview-3/google-cloud-run-infrastructure.md).

***

## 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`.

| Field        | Default value                                                      | Description                                                                                                                                                                             |
| ------------ | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **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:

```json
{"status":{"traffic":[{"revisionName":"LATEST","percent":20},{"revisionName":"<stable-revision>","percent":80}]}}
```

### Infrastructure configuration

| Field               | Default value            | Description                                                                                       |
| ------------------- | ------------------------ | ------------------------------------------------------------------------------------------------- |
| **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

| Output key            | Description                                                         |
| --------------------- | ------------------------------------------------------------------- |
| `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:

1. Authenticates with GCP using the configured connector. Runs `gcloud auth activate-service-account --key-file sa_key.json` for Service Account auth, or identifies the active account for Inherit from Delegate.
2. Configures the gcloud environment: sets project and region.
3. Saves `SERVICE` to step output.
4. 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:

   ```
   gcloud run services update-traffic my-svc --to-revisions=LATEST=50,my-svc-00003-abc=50
   ```

   Cloud Run confirms the result, for example: `50% my-svc-00003-abc / 50% LATEST (currently my-svc-00004-xyz)`.
5. Runs `gcloud run services describe <service> --format json` to capture the post-shift traffic state.
6. Saves the post-shift `REVISION_METADATA` to step output. For example:

   ```json
   {"status":{"traffic":[{"percent":50,"revisionName":"my-svc-00003-abc"},{"percent":50,"revisionName":"my-svc-00004-xyz"}],"latestCreatedRevisionName":"my-svc-00004-xyz"}}
   ```
7. Performs instance sync using the Google Cloud Monitoring SDK. If data is found, waits an additional 60 seconds for a stable read.
8. Publishes `INSTANCES` and `DEPLOYMENT_METADATA` to step output.

{% hint style="info" %}
**No Prepare Rollback step**

There is no Prepare Rollback step. The Deploy step saves rollback data automatically. The Rollback step uses the state saved by the last successful deployment of the stage to restore traffic.
{% endhint %}

***

## Next steps

* Go to [Google Cloud Run Deploy](/continuous-delivery/3.0/use-deployments/overview-3/gcr-deploy.md) for the step that creates the revision the Traffic Shift routes to.
* Go to [Google Cloud Run Rollback](/continuous-delivery/3.0/use-deployments/overview-3/gcr-deploy/gcr-rollback.md) for the rollback step that restores traffic on failure.
* Go to [Canary deployment](/continuous-delivery/3.0/use-deployments/overview-3/basic/canary.md) for the full canary flow using Deploy and Traffic Shift steps.

{% @harness-feedback/feedback %}
