Skip to content

Commit a31dffa

Browse files
committed
Factor Cloud Run deploy steps into deploy.sh and add service account creation
1 parent a1f1b15 commit a31dffa

2 files changed

Lines changed: 61 additions & 32 deletions

File tree

‎gcp/cloud_run/README.md‎

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ drop the `[tool.uv.sources]` override once it ships on PyPI.
1818

1919
Prerequisites: a Temporal Cloud namespace and API key; a Google Cloud project
2020
with billing and an authenticated `gcloud` CLI; `envsubst` (`gettext` package).
21-
Worker pools bill continuously, so run scale-to-zero (step 6) after testing.
21+
Worker pools bill continuously, so scale to zero after testing (see below).
2222

2323
## Deploy
2424

25-
Run from the repository root, with your own values:
25+
Run from the repository root. Set your own values (also used by the verify step
26+
below), then run the deploy script:
2627

2728
```bash
2829
export PROJECT_ID=your-project-id REGION=us-central1
@@ -37,39 +38,17 @@ export COLLECTOR_CONFIG_SECRET=temporal-otel-collector COLLECTOR_CONFIG_SECRET_V
3738
export WORKER_IMAGE="${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/gcp-cloud-run:v1"
3839
export INSTANCE_COUNT=1
3940

40-
# Steps 1-3 are one-time setup; re-run steps 4-5 to redeploy.
41-
# 1. Enable APIs, create the Artifact Registry repo and the runtime service account.
42-
gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com \
43-
monitoring.googleapis.com run.googleapis.com secretmanager.googleapis.com \
44-
telemetry.googleapis.com --project "$PROJECT_ID"
45-
gcloud artifacts repositories create "$REPOSITORY" --location "$REGION" \
46-
--repository-format docker --project "$PROJECT_ID"
47-
48-
# 2. Grant the service account the collector's telemetry roles.
49-
for role in roles/logging.logWriter roles/monitoring.metricWriter roles/telemetry.tracesWriter; do
50-
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
51-
--member "serviceAccount:${SERVICE_ACCOUNT_EMAIL}" --role "$role"
52-
done
53-
54-
# 3. Store the API key and collector config as secrets the service account can read.
55-
gcloud secrets create "$TEMPORAL_API_KEY_SECRET" --data-file "$TEMPORAL_API_KEY_FILE" --project "$PROJECT_ID"
56-
gcloud secrets create "$COLLECTOR_CONFIG_SECRET" \
57-
--data-file gcp/cloud_run/collector-config.yaml --project "$PROJECT_ID"
58-
for secret in "$TEMPORAL_API_KEY_SECRET" "$COLLECTOR_CONFIG_SECRET"; do
59-
gcloud secrets add-iam-policy-binding "$secret" \
60-
--member "serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
61-
--role roles/secretmanager.secretAccessor --project "$PROJECT_ID"
62-
done
41+
./gcp/cloud_run/deploy.sh
42+
```
6343

64-
# 4. Build the image (build context is the sample dir, keeping credentials out).
65-
gcloud builds submit gcp/cloud_run --region "$REGION" \
66-
--tag "$WORKER_IMAGE" --project "$PROJECT_ID"
44+
`deploy.sh` enables the required APIs; creates the Artifact Registry repo, the
45+
runtime service account, and the API key and collector-config secrets; grants the
46+
telemetry and secret-access roles; then builds the image and deploys the
47+
two-container worker pool. The create steps are one-time and re-run safely.
6748

68-
# 5. Render and deploy the two-container worker pool.
69-
envsubst < gcp/cloud_run/worker-pool.yaml > /tmp/worker-pool.yaml
70-
gcloud run worker-pools replace /tmp/worker-pool.yaml --project "$PROJECT_ID"
49+
Worker pools bill continuously, so scale to zero when done testing:
7150

72-
# 6. Scale to zero when done to stop compute charges.
51+
```bash
7352
gcloud run worker-pools update "$WORKER_POOL" --instances 0 \
7453
--region "$REGION" --project "$PROJECT_ID"
7554
```

‎gcp/cloud_run/deploy.sh‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
# Deploy the Temporal Cloud Run worker sample. Export the variables documented in
3+
# README.md ("Deploy") first. The create steps are one-time and skipped when the
4+
# resource already exists, so this is safe to re-run to redeploy.
5+
set -euo pipefail
6+
7+
: "${PROJECT_ID:?set PROJECT_ID}"
8+
: "${REGION:?set REGION}"
9+
: "${REPOSITORY:?set REPOSITORY}"
10+
: "${SERVICE_ACCOUNT_EMAIL:?set SERVICE_ACCOUNT_EMAIL}"
11+
: "${TEMPORAL_API_KEY_SECRET:?set TEMPORAL_API_KEY_SECRET}"
12+
: "${TEMPORAL_API_KEY_FILE:?set TEMPORAL_API_KEY_FILE}"
13+
: "${COLLECTOR_CONFIG_SECRET:?set COLLECTOR_CONFIG_SECRET}"
14+
: "${WORKER_IMAGE:?set WORKER_IMAGE}"
15+
16+
# Enable APIs, create the Artifact Registry repo and runtime service account.
17+
gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com \
18+
monitoring.googleapis.com run.googleapis.com secretmanager.googleapis.com \
19+
telemetry.googleapis.com --project "$PROJECT_ID"
20+
gcloud artifacts repositories describe "$REPOSITORY" --location "$REGION" --project "$PROJECT_ID" >/dev/null 2>&1 ||
21+
gcloud artifacts repositories create "$REPOSITORY" --location "$REGION" \
22+
--repository-format docker --project "$PROJECT_ID"
23+
gcloud iam service-accounts describe "$SERVICE_ACCOUNT_EMAIL" --project "$PROJECT_ID" >/dev/null 2>&1 ||
24+
gcloud iam service-accounts create "${SERVICE_ACCOUNT_EMAIL%%@*}" --project "$PROJECT_ID"
25+
26+
# Grant the service account the collector's telemetry roles.
27+
for role in roles/logging.logWriter roles/monitoring.metricWriter roles/telemetry.tracesWriter; do
28+
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
29+
--member "serviceAccount:${SERVICE_ACCOUNT_EMAIL}" --role "$role"
30+
done
31+
32+
# Store the API key and collector config as secrets the service account can read.
33+
gcloud secrets describe "$TEMPORAL_API_KEY_SECRET" --project "$PROJECT_ID" >/dev/null 2>&1 ||
34+
gcloud secrets create "$TEMPORAL_API_KEY_SECRET" --data-file "$TEMPORAL_API_KEY_FILE" --project "$PROJECT_ID"
35+
gcloud secrets describe "$COLLECTOR_CONFIG_SECRET" --project "$PROJECT_ID" >/dev/null 2>&1 ||
36+
gcloud secrets create "$COLLECTOR_CONFIG_SECRET" \
37+
--data-file gcp/cloud_run/collector-config.yaml --project "$PROJECT_ID"
38+
for secret in "$TEMPORAL_API_KEY_SECRET" "$COLLECTOR_CONFIG_SECRET"; do
39+
gcloud secrets add-iam-policy-binding "$secret" \
40+
--member "serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
41+
--role roles/secretmanager.secretAccessor --project "$PROJECT_ID"
42+
done
43+
44+
# Build the image (build context is the sample dir, keeping credentials out).
45+
gcloud builds submit gcp/cloud_run --region "$REGION" \
46+
--tag "$WORKER_IMAGE" --project "$PROJECT_ID"
47+
48+
# Render and deploy the two-container worker pool.
49+
envsubst < gcp/cloud_run/worker-pool.yaml > /tmp/worker-pool.yaml
50+
gcloud run worker-pools replace /tmp/worker-pool.yaml --project "$PROJECT_ID"

0 commit comments

Comments
 (0)