-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-deploy-staging.sh
More file actions
161 lines (131 loc) · 4.86 KB
/
github-deploy-staging.sh
File metadata and controls
161 lines (131 loc) · 4.86 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
set -euo pipefail
repo_dir="${DEPLOY_PATH:-/home/som/MedusaStore}"
branch="${DEPLOY_BRANCH:-main}"
compose_file="${COMPOSE_FILE:-docker-compose.prod.yml}"
project_name="${COMPOSE_PROJECT_NAME:-medusastore}"
cd "$repo_dir"
run_with_heartbeat() {
local label="$1"
shift
local heartbeat_pid=""
(
while true; do
sleep 60
echo "${label} still running at $(date -Is)..."
done
) &
heartbeat_pid="$!"
set +e
"$@"
local status="$?"
set -e
kill "$heartbeat_pid" 2>/dev/null || true
wait "$heartbeat_pid" 2>/dev/null || true
return "$status"
}
env_file_value() {
local key="$1"
local value=""
value="$(grep -E "^${key}=" .env | tail -1 | cut -d= -f2- || true)"
value="${value%$'\r'}"
if [[ ${#value} -ge 2 && "$value" == \"*\" && "$value" == *\" ]]; then
value="${value:1:${#value}-2}"
value="${value//\\\"/\"}"
value="${value//\\\\/\\}"
elif [[ ${#value} -ge 2 && "$value" == \'*\' && "$value" == *\' ]]; then
value="${value:1:${#value}-2}"
fi
printf '%s' "$value"
}
sibling_origin() {
local label="$1"
local deploy_domain="$2"
if [[ "$deploy_domain" == *.*.* ]]; then
printf 'https://%s.%s' "$label" "${deploy_domain#*.}"
else
printf 'https://%s.%s' "$label" "$deploy_domain"
fi
}
echo "Fetching ${branch}..."
git fetch origin "$branch"
git checkout "$branch"
git reset --hard "origin/${branch}"
if [[ ! -f .env ]]; then
echo "Missing ${repo_dir}/.env" >&2
exit 1
fi
compose_profiles=()
if grep -Eq '^AI_ASSISTANT_ENABLED=true$' .env; then
echo "AI Assistant enabled; including ai-assistant production profile."
compose_profiles+=(--profile ai-assistant)
else
echo "AI Assistant disabled; skipping ai-assistant profile."
fi
build_services=(medusa-backend payload-cms storefront)
if [[ ${#compose_profiles[@]} -gt 0 ]]; then
build_services+=(ai-assistant)
fi
echo "Building production images..."
run_with_heartbeat "Docker image build" \
docker compose -p "$project_name" -f "$compose_file" --env-file .env "${compose_profiles[@]}" build "${build_services[@]}"
echo "Starting database and redis..."
docker compose -p "$project_name" -f "$compose_file" --env-file .env up -d medusa-db medusa-redis
run_payload_migrations="$(grep -E '^RUN_PAYLOAD_MIGRATIONS=' .env | tail -1 | cut -d= -f2- || true)"
run_payload_migrations="${RUN_PAYLOAD_MIGRATIONS:-${run_payload_migrations:-false}}"
if [[ "$run_payload_migrations" == "true" ]]; then
echo "Running Payload migrations as one-off job..."
docker compose -p "$project_name" -f "$compose_file" --env-file .env run --rm \
-e RUN_PAYLOAD_MIGRATIONS=true \
-e RUN_PAYLOAD_SEED=false \
--entrypoint payload-entrypoint \
payload-cms true
else
echo "Skipping Payload migrations; set RUN_PAYLOAD_MIGRATIONS=true in remote .env to enable them."
fi
run_payload_seed="$(grep -E '^RUN_PAYLOAD_SEED=' .env | tail -1 | cut -d= -f2- || true)"
run_payload_seed="${RUN_PAYLOAD_SEED:-${run_payload_seed:-false}}"
if [[ "$run_payload_seed" == "true" ]]; then
echo "Running Payload seed as one-off job..."
docker compose -p "$project_name" -f "$compose_file" --env-file .env run --rm \
-e RUN_PAYLOAD_MIGRATIONS=false \
-e RUN_PAYLOAD_SEED=true \
--entrypoint payload-entrypoint \
payload-cms true
fi
echo "Starting application containers..."
app_services=(medusa-backend payload-cms storefront caddy)
if [[ ${#compose_profiles[@]} -gt 0 ]]; then
app_services=(ai-assistant "${app_services[@]}")
fi
# Force recreation keeps staging deploy idempotent after interrupted or partially
# completed compose recreates, which can leave replace-labeled containers behind.
run_with_heartbeat "Application containers up" \
docker compose -p "$project_name" -f "$compose_file" --env-file .env "${compose_profiles[@]}" up -d --force-recreate --remove-orphans "${app_services[@]}"
echo "Pruning dangling Docker images..."
docker image prune -f >/dev/null || true
echo "Running production smoke checks..."
smoke_base_url="$(env_file_value SMOKE_BASE_URL)"
smoke_backend_url="$(env_file_value SMOKE_BACKEND_URL)"
smoke_payload_url="$(env_file_value SMOKE_PAYLOAD_URL)"
deploy_domain="${DEPLOY_DOMAIN:-}"
if [[ -z "$deploy_domain" ]]; then
deploy_domain="$(env_file_value DEPLOY_DOMAIN)"
fi
if [[ -z "$deploy_domain" ]]; then
deploy_domain="studio.slavx.ru"
fi
public_base_url="https://${deploy_domain}"
admin_base_url="$(sibling_origin admin "$deploy_domain")"
payload_base_url="$(sibling_origin cms "$deploy_domain")"
if [[ -z "${SMOKE_BASE_URL:-}" ]]; then
export SMOKE_BASE_URL="${smoke_base_url:-$public_base_url}"
fi
if [[ -z "${SMOKE_BACKEND_URL:-}" ]]; then
export SMOKE_BACKEND_URL="${smoke_backend_url:-$admin_base_url/app}"
fi
if [[ -z "${SMOKE_PAYLOAD_URL:-}" ]]; then
export SMOKE_PAYLOAD_URL="${smoke_payload_url:-$payload_base_url/api/pages}"
fi
run_with_heartbeat "Smoke checks" bash ./scripts/staging-container-smoke.sh
echo "Deployment complete."