Skip to content

Commit 0cdc55f

Browse files
feat: add reusable workflow to deploy images to Cloud Run with auto rollback (#307)
Co-authored-by: CloudDrove CI <84795582+clouddrove-ci@users.noreply.github.com>
1 parent 521761c commit 0cdc55f

4 files changed

Lines changed: 232 additions & 0 deletions

File tree

‎.github/actions/cloudrun-rollback‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
########################################################################################
2+
# Rollback Workflow (Cloud Run)
3+
#---------------------------------------------------------------------------------------
4+
# Purpose:
5+
# This job is automatically triggered when the `deploy-backend` job fails.
6+
# It safely rolls back the specified Google Cloud Run service to the
7+
# previously deployed revision by shifting 100% traffic back to it.
8+
#
9+
# Use case:
10+
# - Prevents broken deployments from impacting production
11+
# - Ensures high availability by restoring last known good revision
12+
#
13+
# Trigger condition:
14+
# - Runs only when a dependent job fails (`if: failure()`)
15+
########################################################################################
16+
17+
rollback-all:
18+
needs:
19+
- deploy-backend
20+
if: ${{ failure() }}
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Authenticate to GCP
25+
uses: google-github-actions/auth@v2
26+
with:
27+
credentials_json: # GCP Service Account key (JSON)
28+
29+
- name: Setup gcloud
30+
uses: google-github-actions/setup-gcloud@v2
31+
with:
32+
project_id: # GCP Project ID
33+
34+
- name: Rollback Cloud Run services (if needed)
35+
run: |
36+
echo "🚨 Rollback triggered because a deployment failed"
37+
38+
REGION= # GCP region
39+
PROJECT= # GCP Project ID
40+
41+
rollback_if_needed () {
42+
SERVICE_NAME=$1
43+
PREV_REV=$2
44+
45+
if [ -z "$PREV_REV" ]; then
46+
echo "ℹ️ $SERVICE_NAME was not deployed, skipping rollback"
47+
return
48+
fi
49+
50+
CURRENT_REV=$(gcloud run services describe "$SERVICE_NAME" \
51+
--region "$REGION" \
52+
--project "$PROJECT" \
53+
--format="value(status.traffic[0].revisionName)")
54+
55+
if [ "$CURRENT_REV" = "$PREV_REV" ]; then
56+
echo "✅ $SERVICE_NAME rollback not required — traffic already on previous revision ($PREV_REV)"
57+
else
58+
echo "🔄 Rolling back $SERVICE_NAME to revision: $PREV_REV"
59+
gcloud run services update-traffic "$SERVICE_NAME" \
60+
--to-revisions="$PREV_REV=100" \
61+
--region "$REGION" \
62+
--project "$PROJECT" \
63+
&& echo "✅ $SERVICE_NAME rollback successful" \
64+
|| echo "⚠️ $SERVICE_NAME rollback failed — traffic may already be correct"
65+
fi
66+
}
67+
68+
rollback_if_needed "# Cloud Run service name" "${{ needs.deploy-backend.outputs.revision }}"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
name: Cloud Run Deploy with Auto Rollback
3+
4+
on:
5+
workflow_call:
6+
inputs:
7+
gcp_registry_host:
8+
required: true
9+
type: string
10+
IMAGE_NAME:
11+
required: true
12+
type: string
13+
IMAGE_TAG:
14+
required: true
15+
type: string
16+
GCP_REPOSITORY:
17+
required: true
18+
type: string
19+
SERVICE_NAME:
20+
required: true
21+
type: string
22+
REGION:
23+
required: true
24+
type: string
25+
26+
secrets:
27+
GCP_PROJECT_ID:
28+
required: true
29+
GCP_SA_KEY:
30+
required: true
31+
32+
outputs:
33+
revision:
34+
description: "Previous revision"
35+
value: ${{ jobs.deploy.outputs.revision }}
36+
service_name:
37+
description: "Service name"
38+
value: ${{ inputs.SERVICE_NAME }}
39+
40+
jobs:
41+
deploy:
42+
runs-on: ubuntu-latest
43+
outputs:
44+
revision: ${{ steps.prev.outputs.revision }}
45+
46+
steps:
47+
- name: Authenticate to GCP
48+
uses: google-github-actions/auth@v2
49+
with:
50+
credentials_json: ${{ secrets.GCP_SA_KEY }}
51+
52+
- name: Setup gcloud
53+
uses: google-github-actions/setup-gcloud@v2
54+
55+
- name: Get current revision
56+
id: prev
57+
run: |
58+
REVISION=$(gcloud run services describe ${{ inputs.SERVICE_NAME }} \
59+
--region ${{ inputs.REGION }} \
60+
--format="value(status.traffic[0].revisionName)")
61+
echo "Current revision: $REVISION"
62+
# Set for parent workflow (Environment file)
63+
echo "revision=$REVISION" >> $GITHUB_OUTPUT
64+
65+
# 2️⃣ Deploy new image
66+
- name: Deploy new image
67+
id: deploy
68+
run: |
69+
IMAGE_URI="${{ inputs.gcp_registry_host }}/${{ secrets.GCP_PROJECT_ID }}/${{ inputs.GCP_REPOSITORY }}/${{ inputs.IMAGE_NAME }}:${{ inputs.IMAGE_TAG }}"
70+
echo "🚀 Deploying $IMAGE_URI"
71+
gcloud run deploy ${{ inputs.SERVICE_NAME }} \
72+
--image "$IMAGE_URI" \
73+
--region ${{ inputs.REGION }} \
74+
--platform managed \
75+
--quiet
76+
...

‎docs/cloudrun-rollback.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## [Cloud Run Deploy with Auto Rollback reusable workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/.github/workflows/shared-cloudrun-rollback.yml)
2+
3+
4+
5+
### Overview
6+
7+
The Cloud Run Deploy with Auto Rollback reusable workflow is designed to make Cloud Run deployments safer and more reliable.
8+
9+
It deploys a new Docker image to a Cloud Run service while preserving the currently active revision.
10+
If the deployment fails, parent workflows can automatically roll back traffic to the last stable revision.
11+
12+
This approach helps reduce production risk and ensures quick recovery from failed deployments.
13+
14+
### Features
15+
16+
* Deploys a Docker image to Google Cloud Run
17+
18+
* Captures the currently active revision before deployment
19+
20+
* Exposes the previous revision as a workflow output
21+
22+
* Enables automatic rollback on deployment failure
23+
24+
* Uses secure GCP authentication via Service Account
25+
26+
* Designed as a reusable workflow for multiple services
27+
28+
### Usage
29+
30+
### Workflow Location
31+
```
32+
.github/workflows/cloudrun-rollback.yml
33+
```
34+
#### Example
35+
```yaml
36+
name: Deploy to Cloud Run
37+
38+
on:
39+
workflow_dispatch:
40+
41+
jobs:
42+
deploy-backend:
43+
uses: clouddrove/github-shared-workflows/.github/workflows/cloudrun-rollback.yml@master
44+
with:
45+
gcp_registry_host: # GCP Artifact Registry host
46+
IMAGE_NAME: # Docker image name
47+
IMAGE_TAG: # Image tag to deploy
48+
SERVICE_NAME: # Cloud Run service name
49+
REGION: # GCP region
50+
GCP_REPOSITORY: # Artifact Registry repository
51+
secrets:
52+
GCP_PROJECT_ID: # GCP Project ID
53+
GCP_SA_KEY: # GCP Service Account key (JSON)
54+
```
55+
56+
#### Reference workflow:
57+
The rollback logic used by this workflow is implemented in
58+
```
59+
actions/cloudrun-rollback
60+
```

‎docs/tfdrift.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## [terraform drifts Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/.github/workflows/tfdrifts.yml)
2+
3+
This workflow automates Terraform configuration drift detection by running terraform init/plan against your live infrastructure and signaling when resources have changed outside of code. The reusable workflow is stored at `.github/workflows/tfdrifts.yml` in the shared repo.
4+
5+
**Key capabilities**:
6+
- Detect drift via terraform plan.
7+
- Works with AWS, Azure, or GCP (select with provider).
8+
9+
#### Example
10+
```yaml
11+
name: TF-Drift
12+
on:
13+
push:
14+
branches: [ master ]
15+
pull_request:
16+
workflow_dispatch:
17+
jobs:
18+
tf-lint:
19+
uses: clouddrove/github-shared-workflows/.github/workflows/tfdrift.yml@master
20+
with:
21+
working_directory: #'./_example/complete/'
22+
provider: #aws
23+
aws_region: # AWS region
24+
secrets:
25+
AWS_ACCESS_KEY_ID: # Specify AWS Access key ID
26+
AWS_SECRET_ACCESS_KEY: # Specify AWS Secret Access key ID
27+
AWS_SESSION_TOKEN: # Specify Session ID
28+
```

0 commit comments

Comments
 (0)