Repository navigation
Expand file tree
/
Copy pathdeploy_cloud_run.sh
More file actions
executable file
·131 lines (111 loc) · 4.32 KB
/
Copy pathdeploy_cloud_run.sh
File metadata and controls
executable file
·131 lines (111 loc) · 4.32 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
set -euo pipefail
# deploy_cloud_run.sh
# Build the Docker image, push to Artifact Registry, and deploy to Cloud Run.
# Usage: ./deploy_cloud_run.sh -p PROJECT_ID -s SERVICE_NAME [-r REGION] [-i IMAGE_NAME] [-a]
REGION=""
ALLOW_UNAUTH=""
IMAGE_NAME=""
PROJECT_ID=""
SERVICE_NAME=""
ENVIRONMENT="dev"
REPO_NAME="cloud-run-images" # Artifact Registry repository name
# Load defaults from an env file without overwriting flags already set.
load_env_defaults() {
local file="$1"
if [[ -f "$file" ]]; then
# read non-empty, non-comment lines
while IFS='=' read -r key val; do
key=$(echo "$key" | xargs)
val=$(echo "$val" | sed -e 's/^ *//;s/ *$//')
if [[ -z "$key" ]]; then
continue
fi
# only set variable if not already provided via flag
if [[ -z "${!key:-}" ]]; then
eval "$key=\"$val\""
fi
done < <(grep -vE '^\s*#|^\s*$' "$file")
echo "Loaded defaults from $file"
else
echo "Env file not found: $file"
fi
}
usage() {
cat <<EOF
Usage: $0 -p PROJECT_ID -s SERVICE_NAME [-r REGION] [-i IMAGE_NAME] [-a]
Options:
-p PROJECT_ID GCP project ID (required unless env preset supplies it)
-s SERVICE_NAME Cloud Run service name (required unless env preset supplies it)
-r REGION GCP region (default: us-central1)
-i IMAGE_NAME Image name (default: same as service)
-a Allow unauthenticated (public) access
-e ENV Environment preset: dev (default) or prod
-h Show this help
EOF
}
while getopts ":p:s:r:i:ae:h" opt; do
case ${opt} in
p) PROJECT_ID="$OPTARG" ;;
s) SERVICE_NAME="$OPTARG" ;;
r) REGION="$OPTARG" ;;
i) IMAGE_NAME="$OPTARG" ;;
a) ALLOW_UNAUTH=true ;;
e) ENVIRONMENT="$OPTARG" ;;
h) usage; exit 0 ;;
:) echo "Missing arg for -$OPTARG"; usage; exit 1 ;;
*) echo "Invalid option -$OPTARG"; usage; exit 1 ;;
esac
done
# Load env defaults from file corresponding to the selected environment
# Use `.env` for local/dev workflows and `.env.production` for production Cloud Run
if [[ "$ENVIRONMENT" == "dev" ]]; then
load_env_defaults ".env"
else
load_env_defaults ".env.production"
fi
# Fill defaults for any remaining unset values
REGION="${REGION:-us-central1}"
IMAGE_NAME="${IMAGE_NAME:-$SERVICE_NAME}"
ALLOW_UNAUTH="${ALLOW_UNAUTH:-false}"
REPO_NAME="${REPO_NAME:-cloud-run-images}"
if [[ -z "$PROJECT_ID" || -z "$SERVICE_NAME" ]]; then
echo "Error: PROJECT_ID and SERVICE_NAME must be set (flags, preset or environment vars)."
usage
exit 1
fi
TIMESTAMP=$(date -u +%Y%m%d%H%M%S)
# UPDATED: New Artifact Registry image naming convention
IMAGE_TAG="${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:${TIMESTAMP}"
command -v gcloud >/dev/null 2>&1 || { echo "gcloud CLI not found. Install and authenticate first."; exit 1; }
# show what we're deploying
echo "Environment: ${ENVIRONMENT}"
echo "Project: ${PROJECT_ID}"
echo "Region: ${REGION}"
echo "Repository: ${REPO_NAME}"
echo "Service: ${SERVICE_NAME}"
echo "Image: ${IMAGE_TAG}"
echo "Allow unauthenticated: ${ALLOW_UNAUTH}"
# UPDATED: Ensure the Artifact Registry repository exists before building
echo "Checking if Artifact Registry repository '${REPO_NAME}' exists..."
if ! gcloud artifacts repositories describe "${REPO_NAME}" --location="${REGION}" --project="${PROJECT_ID}" >/dev/null 2>&1; then
echo "Repository not found. Creating '${REPO_NAME}' in ${REGION}..."
gcloud artifacts repositories create "${REPO_NAME}" \
--repository-format=docker \
--location="${REGION}" \
--project="${PROJECT_ID}" \
--description="Docker repository for Cloud Run images"
fi
echo "Building container and pushing to ${IMAGE_TAG}..."
# Cloud Build seamlessly pushes to Artifact Registry URLs
gcloud builds submit --tag "${IMAGE_TAG}" --project "${PROJECT_ID}" .
echo "Deploying to Cloud Run service ${SERVICE_NAME} in ${REGION}..."
# REMOVED: Deprecated --platform managed flag (it is now the standard default behavior)
DEPLOY_CMD=(gcloud run deploy "${SERVICE_NAME}" --image "${IMAGE_TAG}" --region "${REGION}" --project "${PROJECT_ID}")
if [ "$ALLOW_UNAUTH" = true ]; then
DEPLOY_CMD+=(--allow-unauthenticated)
fi
"${DEPLOY_CMD[@]}"
echo "Deployment finished. Service URL:"
gcloud run services describe "${SERVICE_NAME}" --region "${REGION}" --project "${PROJECT_ID}" --format="value(status.url)"
echo "Done."