-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·195 lines (170 loc) · 5.68 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·195 lines (170 loc) · 5.68 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$SCRIPT_DIR"
if [[ ! -f "$ROOT_DIR/docker-compose.prod.yml" ]]; then
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
fi
REMOTE="${DEPLOY_REMOTE:-origin}"
BRANCH="${DEPLOY_BRANCH:-main}"
COMPOSE_FILE="${DEPLOY_COMPOSE_FILE:-docker-compose.prod.yml}"
SERVICE_TIMEOUT_SECONDS="${DEPLOY_SERVICE_TIMEOUT_SECONDS:-240}"
API_TIMEOUT_SECONDS="${DEPLOY_API_TIMEOUT_SECONDS:-240}"
SKIP_PULL=0
usage() {
cat <<'EOF'
Usage: ./deploy.sh [--skip-pull] [--help]
Options:
--skip-pull Skip git fetch/pull and deploy current local checkout.
--help Show this help text.
Environment overrides:
DEPLOY_REMOTE=origin
DEPLOY_BRANCH=main
DEPLOY_COMPOSE_FILE=docker-compose.prod.yml
DEPLOY_SERVICE_TIMEOUT_SECONDS=240
DEPLOY_API_TIMEOUT_SECONDS=240
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--skip-pull)
SKIP_PULL=1
shift
;;
--help|-h)
usage
exit 0
;;
*)
echo "[deploy] Unknown option: $1" >&2
usage >&2
exit 2
;;
esac
done
log() {
echo "[deploy] $*"
}
die() {
echo "[deploy] ERROR: $*" >&2
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
}
read_env_value() {
local key="$1"
local value
value="$(
awk -F= -v key="$key" '
$0 !~ /^[[:space:]]*#/ && $1 == key {
print substr($0, index($0, "=") + 1)
}
' .env | tail -n 1
)"
value="$(printf '%s' "$value" | tr -d '\r' | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')"
value="${value#\"}"
value="${value%\"}"
value="${value#\'}"
value="${value%\'}"
printf '%s' "$value"
}
ensure_clean_worktree() {
if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then
die "Working tree is not clean. Commit/stash changes, or run with --skip-pull."
fi
}
wait_for_service() {
local service="$1"
local timeout="$2"
local elapsed=0
while (( elapsed < timeout )); do
local cid state health summary
cid="$(docker compose -f "$COMPOSE_FILE" ps -q "$service" 2>/dev/null || true)"
if [[ -n "$cid" ]]; then
summary="$(docker inspect --format '{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$cid" 2>/dev/null || true)"
state="${summary%% *}"
health="${summary##* }"
if [[ "$state" == "running" && ( "$health" == "healthy" || "$health" == "none" ) ]]; then
log "Service '$service' is running (health=$health)."
return 0
fi
if [[ "$state" == "exited" || "$state" == "dead" ]]; then
log "Service '$service' entered state=$state (health=$health)."
docker compose -f "$COMPOSE_FILE" logs --tail=120 "$service" || true
return 1
fi
fi
sleep 2
elapsed=$((elapsed + 2))
done
log "Timed out waiting for service '$service' after ${timeout}s."
docker compose -f "$COMPOSE_FILE" logs --tail=120 "$service" || true
return 1
}
wait_for_api_ready() {
local timeout="$1"
local elapsed=0
while (( elapsed < timeout )); do
if docker compose -f "$COMPOSE_FILE" exec -T server \
node -e "require('http').get('http://127.0.0.1:3001/health/ready',r=>process.exit(r.statusCode===200?0:1)).on('error',()=>process.exit(1));" \
>/dev/null 2>&1; then
log "API readiness check passed (/health/ready=200)."
return 0
fi
sleep 2
elapsed=$((elapsed + 2))
done
log "API readiness check failed after ${timeout}s."
docker compose -f "$COMPOSE_FILE" logs --tail=120 server || true
return 1
}
cd "$ROOT_DIR"
require_cmd git
require_cmd docker
[[ -d ".git" ]] || die "Not a git repository: $ROOT_DIR"
[[ -f "$COMPOSE_FILE" ]] || die "Compose file not found: $COMPOSE_FILE"
[[ -f ".env" ]] || die "Missing .env in $ROOT_DIR. Create it from env.example before deployment."
ingest_mode="$(read_env_value "INGEST_MODE")"
normalized_ingest_mode="$(printf '%s' "$ingest_mode" | tr '[:upper:]' '[:lower:]')"
if [[ -z "$normalized_ingest_mode" ]]; then
log "WARNING: INGEST_MODE is unset. Runtime default is public (unsigned submissions accepted)."
elif [[ "$normalized_ingest_mode" == "public" ]]; then
log "WARNING: INGEST_MODE=public allows unsigned submissions."
fi
if [[ "$normalized_ingest_mode" == "signed" ]]; then
ingest_secret="$(read_env_value "INGEST_HMAC_SECRET")"
[[ -n "$ingest_secret" ]] || die "INGEST_MODE=signed requires INGEST_HMAC_SECRET in .env."
fi
if [[ "$SKIP_PULL" -eq 0 ]]; then
ensure_clean_worktree
log "Fetching latest '$BRANCH' from '$REMOTE'..."
git fetch --prune "$REMOTE" "$BRANCH"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
if [[ "$current_branch" != "$BRANCH" ]]; then
if git show-ref --verify --quiet "refs/heads/$BRANCH"; then
log "Checking out local branch '$BRANCH'..."
git checkout "$BRANCH"
else
log "Creating local branch '$BRANCH' tracking '$REMOTE/$BRANCH'..."
git checkout -b "$BRANCH" --track "$REMOTE/$BRANCH"
fi
fi
log "Pulling latest changes (fast-forward only)..."
git pull --ff-only "$REMOTE" "$BRANCH"
fi
log "Validating compose configuration..."
docker compose -f "$COMPOSE_FILE" config -q
log "Building and starting production stack..."
docker compose -f "$COMPOSE_FILE" up -d --build --remove-orphans
log "Waiting for services to become healthy..."
services="$(docker compose -f "$COMPOSE_FILE" config --services)"
for service in $services; do
wait_for_service "$service" "$SERVICE_TIMEOUT_SECONDS" || die "Service readiness failed: $service"
done
if echo "$services" | grep -qx "server"; then
wait_for_api_ready "$API_TIMEOUT_SECONDS" || die "API readiness failed."
fi
commit="$(git rev-parse --short HEAD)"
log "Deployment complete at commit $commit."
docker compose -f "$COMPOSE_FILE" ps