> 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/google-cloud-functions/google-cloud-functions-deployment-strategies/canary.md).

# Canary deployment

Progressively shift traffic to a new Google Cloud Function revision before completing the cutover.

Canary deployment deploys a new function revision without traffic, shifts an initial percentage of traffic to it, then shifts the remaining traffic to complete the cutover. Use this strategy when you want to validate a new revision against live traffic before committing fully.

***

### What you will learn from this topic <a href="#what-you-will-learn-from-this-topic" id="what-you-will-learn-from-this-topic"></a>

* Understand how Harness progressively shifts traffic to a new function revision before completing the cutover.
* See how the deploy and traffic shift steps pass data to each other, and how Harness handles it if the deployment fails.

***

### Before you begin <a href="#before-you-begin" id="before-you-begin"></a>

* **A Google Cloud Functions service:** Go to [Google Cloud Functions services](https://developer.harness.io/continuous-delivery/3.0/use-deployments/google-cloud-functions/google-cloud-functions-services.md) to set up a service with a manifest and artifact source.
* **A Google Cloud Functions infrastructure definition:** Go to [Google Cloud Functions infrastructure](https://developer.harness.io/continuous-delivery/3.0/use-deployments/google-cloud-functions/google-cloud-functions-infrastructure.md) to connect a Google Cloud account and configure authentication.

***

### Canary strategy step sequence <a href="#canary-strategy-step-sequence" id="canary-strategy-step-sequence"></a>

When you select **Canary**, Harness adds the following steps to your stage execution:

1. **Google Cloud Function Deploy:** Deploys the new function revision with **Skip Traffic** set to `true`, so the revision does not receive traffic yet.
2. **Google Cloud Function Traffic Shift Canary:** Shifts an initial percentage of traffic, for example `10`, to the newly deployed revision, using the function name, revision, and first-deployment flag from the deploy step's output.
3. **Google Cloud Function Traffic Shift Final:** Shifts the remaining traffic to the new revision, completing the cutover to `100`.

On rollback, Harness runs the **Google Cloud Function Rollback** step inside a **Google Cloud Function Canary Rollback** step group.

***

### Stage YAML <a href="#stage-yaml" id="stage-yaml"></a>

A minimal canary stage looks like this.

{% tabs %}
{% tab title="Run on Delegate" %}
This example runs on a delegate and deploys to every infrastructure definition in the environment:

```yaml
- name: Deploy Google Cloud Function
  id: Deploy_Google_Cloud_Function
  service:
    type: google-cloud-functions
    items:
    - service_google_cloud_function
  environment:
    items:
    - id: environment_env
      all-infra: true
      deploy-to: all
  runtime: delegate
  steps:
  - name: Google Cloud Function Deploy
    id: googleCloudFunctionDeployStep
    template:
      uses: googleCloudFunctionDeployStep
      with:
        skip_traffic: true
  - name: Google Cloud Function Traffic Shift Canary
    id: googleCloudFunctionTrafficShiftStepCanary
    template:
      uses: googleCloudFunctionTrafficShiftStep
      with:
        function: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.functionName}}
        is_first_deployment: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.isFirstDeployment}}
        target_revision: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.revision}}
        target_traffic_percent: "10"
  - name: Google Cloud Function Traffic Shift Final
    id: googleCloudFunctionTrafficShiftStepFinal
    template:
      uses: googleCloudFunctionTrafficShiftStep
      with:
        function: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.functionName}}
        is_first_deployment: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.isFirstDeployment}}
        target_revision: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.revision}}
        target_traffic_percent: "100"
  rollback:
  - group:
      steps:
      - name: Google Cloud Function Rollback
        id: googleCloudFunctionRollbackStep
        template:
          uses: googleCloudFunctionRollbackStep
          with:
            function_name: ${{rollback.data.FUNCTION_NAME}}
            active_cloud_run_revisions: ${{rollback.data.ACTIVE_CLOUD_RUN_REVISIONS}}
    id: googleCloudFunctionCanaryRollback
    name: Google Cloud Function Canary Rollback
  on-failure:
    errors: all
    action: stage-rollback
```

{% endtab %}

{% tab title="Run on Harness Cloud" %}
This example runs on Harness Cloud and deploys to a single, specific infrastructure definition:

```yaml
- name: s1
  id: s1
  service:
    items:
    - gcf_nodejs_service
  environment:
    items:
    - id: environment_env
      deploy-to: infrastructure_gcf
  runtime: cloud
  steps:
  - name: Google Cloud Function Deploy
    id: googleCloudFunctionDeployStep
    template:
      uses: googleCloudFunctionDeployStep
      with:
        skip_traffic: true
  - name: Google Cloud Function Traffic Shift Canary
    id: googleCloudFunctionTrafficShiftStepCanary
    template:
      uses: googleCloudFunctionTrafficShiftStep
      with:
        function: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.functionName}}
        is_first_deployment: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.isFirstDeployment}}
        target_revision: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.revision}}
        target_traffic_percent: "10"
  - name: Google Cloud Function Traffic Shift Final
    id: googleCloudFunctionTrafficShiftStepFinal
    template:
      uses: googleCloudFunctionTrafficShiftStep
      with:
        function: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.functionName}}
        is_first_deployment: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.isFirstDeployment}}
        target_revision: ${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.revision}}
        target_traffic_percent: "100"
  rollback:
  - group:
      steps:
      - name: Google Cloud Function Rollback
        id: googleCloudFunctionRollbackStep
        template:
          uses: googleCloudFunctionRollbackStep
          with:
            function_name: ${{rollback.data.FUNCTION_NAME}}
            active_cloud_run_revisions: ${{rollback.data.ACTIVE_CLOUD_RUN_REVISIONS}}
    id: googleCloudFunctionCanaryRollback
    name: Google Cloud Function Canary Rollback
  on-failure:
    errors: all
    action: stage-rollback
```

{% endtab %}
{% endtabs %}

***

### Configure the Google Cloud Function Deploy step <a href="#configure-the-google-cloud-function-deploy-step" id="configure-the-google-cloud-function-deploy-step"></a>

For the canary strategy, the deploy step sets **Skip Traffic** to `true` so the new revision does not receive traffic until the traffic shift steps run.

| Field               | Default | Description                                                                                                                       |
| ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------- |
| **Skip Traffic**    | `false` | When `true`, the deployed revision does not receive traffic until a separate traffic shift step runs. Canary sets this to `true`. |
| **Command Timeout** | `10m`   | Maximum time for the step to complete.                                                                                            |
| **Log Level**       | `info`  | Log verbosity for the step execution.                                                                                             |

***

### Configure the Google Cloud Function Traffic Shift Canary step <a href="#configure-the-google-cloud-function-traffic-shift-canary-step" id="configure-the-google-cloud-function-traffic-shift-canary-step"></a>

The canary traffic shift step reads the function name, revision, and first-deployment flag from the deploy step's output, and shifts an initial percentage of traffic to that revision for validation.

| Field                      | Default                                                                                   | Description                                                                                                                                              |
| -------------------------- | ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Cloud Function Name**    | `${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.functionName}}`      | The function to shift traffic for. Automatically resolved from the deploy step output.                                                                   |
| **First Deployment**       | `${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.isFirstDeployment}}` | Whether this is the first deployment of the function. When `true`, the step skips the traffic shift. Automatically resolved from the deploy step output. |
| **Target Revision**        | `${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.revision}}`          | The revision to shift traffic to. Automatically resolved from the deploy step output.                                                                    |
| **Target Traffic Percent** | —                                                                                         | The percentage of traffic to shift to the target revision. The canary step sets this to an initial value, for example `10`.                              |
| **Command Timeout**        | `10m`                                                                                     | Maximum time for the step to complete.                                                                                                                   |
| **Log Level**              | `info`                                                                                    | Log verbosity for the step execution.                                                                                                                    |

***

### Configure the Google Cloud Function Traffic Shift Final step <a href="#configure-the-google-cloud-function-traffic-shift-final-step" id="configure-the-google-cloud-function-traffic-shift-final-step"></a>

The final traffic shift step uses the same inputs as the canary traffic shift step, but shifts the remaining traffic to complete the cutover.

| Field                      | Default                                                                                   | Description                                                                                                                                              |
| -------------------------- | ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Cloud Function Name**    | `${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.functionName}}`      | The function to shift traffic for. Automatically resolved from the deploy step output.                                                                   |
| **First Deployment**       | `${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.isFirstDeployment}}` | Whether this is the first deployment of the function. When `true`, the step skips the traffic shift. Automatically resolved from the deploy step output. |
| **Target Revision**        | `${{stage.steps.googleCloudFunctionDeployStep.output.outputVariables.revision}}`          | The revision to shift traffic to. Automatically resolved from the deploy step output.                                                                    |
| **Target Traffic Percent** | —                                                                                         | The percentage of traffic to shift to the target revision. The final step sets this to `100` to complete the cutover.                                    |
| **Command Timeout**        | `10m`                                                                                     | Maximum time for the step to complete.                                                                                                                   |
| **Log Level**              | `info`                                                                                    | Log verbosity for the step execution.                                                                                                                    |

***

### Rollback behavior <a href="#rollback-behavior" id="rollback-behavior"></a>

When the stage failure strategy triggers rollback, the **Google Cloud Function Rollback** step inside the **Google Cloud Function Canary Rollback** step group runs automatically.

The rollback step receives the following inputs:

| Field                      | Value                                           | Description                                                                                    |
| -------------------------- | ----------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| **Cloud Function Name**    | `${{rollback.data.FUNCTION_NAME}}`              | The function to roll back.                                                                     |
| **Previous Traffic Split** | `${{rollback.data.ACTIVE_CLOUD_RUN_REVISIONS}}` | The revisions that were active before the deployment. Used to restore traffic during rollback. |

{% hint style="info" %}
**Gen 2 functions run on a backing Cloud Run service**

Google Cloud Functions Gen 2 deploys each function as a Cloud Run service. The deploy and traffic shift steps read and modify this backing service directly, which is why rollback data stores Cloud Run revisions.
{% endhint %}

Rollback behavior differs by deployment history:

* **Subsequent deployments:** Harness restores the previous serving traffic split on the backing Cloud Run service. **Previous Traffic Split** holds the revision and traffic percentage that were active before the deployment, and the rollback step applies that split back to the service.
* **First deployment:** There is no previous revision to restore traffic to, so **Previous Traffic Split** has nothing to roll back to.

{% hint style="warning" %}
**First-deployment rollback behavior is unconfirmed**

The exact outcome of a rollback on a function's first deployment, such as whether Harness leaves the new revision in place, routes it to zero traffic, or deletes it, has not been verified against a real execution log. Verify this against your own environment before relying on it.
{% endhint %}

***

### Next steps <a href="#next-steps" id="next-steps"></a>

* Go to [Basic](https://developer.harness.io/continuous-delivery/3.0/use-deployments/google-cloud-functions/google-cloud-functions-deployment-strategies/basic.md) for a single-phase deployment without progressive traffic shifting.
* Go to [Blue-green](https://developer.harness.io/continuous-delivery/3.0/use-deployments/google-cloud-functions/google-cloud-functions-deployment-strategies/blue-green.md) for a zero-downtime traffic cutover deployment.
* Go to [Blank canvas](https://developer.harness.io/continuous-delivery/3.0/use-deployments/google-cloud-functions/google-cloud-functions-deployment-strategies/blank-canvas.md) to compose steps manually.
