-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudbuild-preview.yaml
More file actions
117 lines (99 loc) · 4.14 KB
/
cloudbuild-preview.yaml
File metadata and controls
117 lines (99 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Minimal Cloud Build configuration for preview deployments
# This file only handles deployment of pre-built Docker images from GitHub Actions
substitutions:
_DEPLOY_REGION: us-central1
_PLATFORM: managed
_SERVICE_NAME_PREFIX: analytics-api-preview
_PROJECT_ID: mento-prod
_BRANCH_NAME: ${_BRANCH_NAME}
_BRANCH_TAG: ${_BRANCH_TAG}
_SHORT_SHA: ${_SHORT_SHA}
_COMMIT_SHA: ${_COMMIT_SHA}
_IMAGE_TAG: ${_IMAGE_TAG}
options:
logging: CLOUD_LOGGING_ONLY
substitutionOption: ALLOW_LOOSE
steps:
# Step 1: Generate a safe cloud run service name from the git branch name
- name: gcr.io/cloud-builders/gcloud
id: generate-service-name
entrypoint: bash
args:
- -c
- |
# Convert branch name to a safe service name (lowercase, alphanumeric, hyphens only)
# Cloud Run service name limit is 63 characters
# Prefix 'analytics-api-preview-' is 22 chars, leaving 41 chars for branch name
SAFE_BRANCH_NAME=$$(echo "${_BRANCH_NAME}" | \
sed 's/[^a-zA-Z0-9-]/-/g' | \
tr '[:upper:]' '[:lower:]' | \
sed 's/^-//;s/-$//' | \
sed 's/--*/-/g' | \
cut -c1-41)
# Create the full service name
SERVICE_NAME="${_SERVICE_NAME_PREFIX}-$${SAFE_BRANCH_NAME}"
# Save for later steps
echo "$$SERVICE_NAME" > /workspace/service_name.txt
echo "Preview service name: $$SERVICE_NAME"
# Step 2: Deploy to Cloud Run using pre-built image
- name: gcr.io/google.com/cloudsdktool/cloud-sdk:slim
id: deploy
entrypoint: bash
args:
- -c
- |
SERVICE_NAME=$$(cat /workspace/service_name.txt)
echo "Deploying pre-built image: ${_IMAGE_TAG}"
echo "Service name: $$SERVICE_NAME"
# The source files are already in /workspace from Cloud Build
# Source the centralized environment configuration
cd /workspace && source ./scripts/env-config.sh
# Enable cache warming for preview deployments
export CACHE_WARMING_ENABLED="true"
# Build the environment variables string
ENV_VARS=$$(build_env_vars_string "${_BRANCH_NAME}" "${_BRANCH_TAG}" "${_SHORT_SHA}")
# Debug: Print the environment variables
echo "=== Environment variables being set ==="
echo "$$ENV_VARS" | tr ',' '\n'
echo "=== End of environment variables ==="
# Check if service exists
if gcloud run services describe "$$SERVICE_NAME" \
--platform=${_PLATFORM} \
--region=${_DEPLOY_REGION} \
--project=${_PROJECT_ID} >/dev/null 2>&1; then
echo "Updating existing preview service: $$SERVICE_NAME"
else
echo "Creating new preview service: $$SERVICE_NAME"
fi
# Deploy the service (this command handles both create and update)
# Use preview-specific secrets that are shared across all preview deployments
gcloud run deploy "$$SERVICE_NAME" \
--platform=${_PLATFORM} \
--image="${_IMAGE_TAG}" \
--labels=managed-by=preview-deployments,commit-sha=${_COMMIT_SHA},branch=${_BRANCH_TAG} \
--region=${_DEPLOY_REGION} \
--project=${_PROJECT_ID} \
--allow-unauthenticated \
--timeout=300 \
--set-env-vars="$$ENV_VARS" \
--set-secrets="EXCHANGE_RATES_API_KEY=exchange-rates-api-key-preview:latest,COINMARKETCAP_API_KEY=coinmarketcap-api-key-preview:latest,CELO_RPC_URL=celo-rpc-url-preview:latest,ETH_RPC_URL=eth-rpc-url-preview:latest,MONAD_RPC_URL=monad-rpc-url-preview:latest"
# Step 3: Get the service URL and save it
- name: gcr.io/google.com/cloudsdktool/cloud-sdk:slim
id: get-url
entrypoint: bash
args:
- -c
- |
SERVICE_NAME=$$(cat /workspace/service_name.txt)
# Get the service URL
SERVICE_URL=$$(gcloud run services describe "$$SERVICE_NAME" \
--platform=${_PLATFORM} \
--region=${_DEPLOY_REGION} \
--project=${_PROJECT_ID} \
--format='value(status.url)')
echo "Preview URL: $$SERVICE_URL"
echo "$$SERVICE_URL" > /workspace/preview_url.txt
# Tags for this build
tags:
- preview-deployment
timeout: 300s