Skip to content

Commit bf557eb

Browse files
committed
Add manual Cloud Run invocation workflow
1 parent 5128d78 commit bf557eb

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Invoke Cloud Run
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
environment:
7+
description: "GitHub Environment to invoke"
8+
required: true
9+
default: "longbridge-sg"
10+
type: choice
11+
options:
12+
- longbridge-hk
13+
- longbridge-sg
14+
path:
15+
description: "HTTP path to call"
16+
required: false
17+
default: "/"
18+
type: string
19+
20+
env:
21+
GCP_PROJECT_ID: longbridgequant
22+
GCP_WORKLOAD_IDENTITY_PROVIDER: projects/252919773759/locations/global/workloadIdentityPools/github-actions/providers/github-main
23+
GCP_WORKLOAD_IDENTITY_SERVICE_ACCOUNT: longbridge-platform-deploy@longbridgequant.iam.gserviceaccount.com
24+
25+
jobs:
26+
invoke:
27+
name: Invoke ${{ inputs.environment }} Cloud Run
28+
runs-on: ubuntu-latest
29+
permissions:
30+
contents: read
31+
id-token: write
32+
environment: ${{ inputs.environment }}
33+
env:
34+
CLOUD_RUN_REGION: ${{ vars.CLOUD_RUN_REGION }}
35+
CLOUD_RUN_SERVICE: ${{ vars.CLOUD_RUN_SERVICE }}
36+
steps:
37+
- name: Validate inputs
38+
run: |
39+
set -euo pipefail
40+
41+
case "${{ inputs.environment }}" in
42+
longbridge-hk|longbridge-sg) ;;
43+
*)
44+
echo "Unsupported environment: ${{ inputs.environment }}" >&2
45+
exit 1
46+
;;
47+
esac
48+
49+
if [ -z "${CLOUD_RUN_REGION:-}" ] || [ -z "${CLOUD_RUN_SERVICE:-}" ]; then
50+
echo "CLOUD_RUN_REGION and CLOUD_RUN_SERVICE are required on ${{ inputs.environment }}." >&2
51+
exit 1
52+
fi
53+
54+
- name: Authenticate to Google Cloud
55+
uses: google-github-actions/auth@v3
56+
with:
57+
workload_identity_provider: ${{ env.GCP_WORKLOAD_IDENTITY_PROVIDER }}
58+
service_account: ${{ env.GCP_WORKLOAD_IDENTITY_SERVICE_ACCOUNT }}
59+
60+
- name: Set up gcloud
61+
uses: google-github-actions/setup-gcloud@v3
62+
with:
63+
project_id: ${{ env.GCP_PROJECT_ID }}
64+
version: ">= 416.0.0"
65+
66+
- name: Invoke service
67+
run: |
68+
set -euo pipefail
69+
70+
raw_path="${{ inputs.path }}"
71+
if [ -z "${raw_path}" ]; then
72+
raw_path="/"
73+
fi
74+
if [[ "${raw_path}" != /* ]]; then
75+
raw_path="/${raw_path}"
76+
fi
77+
78+
service_url="$(
79+
gcloud run services describe "${CLOUD_RUN_SERVICE}" \
80+
--region "${CLOUD_RUN_REGION}" \
81+
--format='value(status.url)'
82+
)"
83+
if [ -z "${service_url}" ]; then
84+
echo "Unable to resolve Cloud Run service URL." >&2
85+
exit 1
86+
fi
87+
88+
token="$(gcloud auth print-identity-token --audiences="${service_url}")"
89+
curl --fail-with-body --show-error --silent \
90+
--request POST \
91+
--header "Authorization: Bearer ${token}" \
92+
"${service_url}${raw_path}"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
5+
workflow_file="$repo_dir/.github/workflows/invoke-cloud-run.yml"
6+
7+
grep -Fq "name: Invoke Cloud Run" "$workflow_file"
8+
grep -Fq "workflow_dispatch:" "$workflow_file"
9+
grep -Fq "environment: \${{ inputs.environment }}" "$workflow_file"
10+
grep -Fq "id-token: write" "$workflow_file"
11+
grep -Fq "google-github-actions/auth@v3" "$workflow_file"
12+
grep -Fq "google-github-actions/setup-gcloud@v3" "$workflow_file"
13+
grep -Fq "CLOUD_RUN_REGION: \${{ vars.CLOUD_RUN_REGION }}" "$workflow_file"
14+
grep -Fq "CLOUD_RUN_SERVICE: \${{ vars.CLOUD_RUN_SERVICE }}" "$workflow_file"
15+
grep -Fq "longbridge-hk|longbridge-sg" "$workflow_file"
16+
grep -Fq "gcloud run services describe \"\${CLOUD_RUN_SERVICE}\"" "$workflow_file"
17+
grep -Fq "gcloud auth print-identity-token --audiences=\"\${service_url}\"" "$workflow_file"
18+
grep -Fq "curl --fail-with-body --show-error --silent" "$workflow_file"
19+
grep -Fq -- "--request POST" "$workflow_file"

0 commit comments

Comments
 (0)