-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathdeploy_check.sh
More file actions
executable file
·118 lines (97 loc) · 3.44 KB
/
Copy pathdeploy_check.sh
File metadata and controls
executable file
·118 lines (97 loc) · 3.44 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
#!/usr/bin/env bash
# Compare the latest Railway deployment commit against origin/main HEAD
# to detect when Railway's GitHub integration silently disconnects.
#
# Usage:
# ./scripts/deploy_check.sh # check automem (default)
# ./scripts/deploy_check.sh mcp-automem # check a specific service
# DEPLOY_CHECK_QUIET=1 ./scripts/deploy_check.sh # exit code only (for CI)
set -euo pipefail
SERVICE="${1:-automem}"
QUIET="${DEPLOY_CHECK_QUIET:-0}"
MAX_AGE_HOURS="${DEPLOY_CHECK_MAX_AGE:-24}"
log() { [[ "$QUIET" == "1" ]] || echo "$@"; }
err() { echo "❌ $*" >&2; }
if ! command -v railway &>/dev/null; then
err "railway CLI not found. Install: https://docs.railway.app/guides/cli"
exit 2
fi
if ! command -v gh &>/dev/null; then
err "gh CLI not found. Install: https://cli.github.com"
exit 2
fi
log "🔍 Checking deploy freshness for service: $SERVICE"
deploy_json=$(railway deployment list --json --service "$SERVICE" 2>/dev/null) || {
err "Failed to list deployments for $SERVICE. Is the Railway CLI linked?"
exit 2
}
deploy_commit=$(echo "$deploy_json" | python3 -c "
import json, sys
data = json.load(sys.stdin)
for d in data:
if d.get('status') == 'SUCCESS':
print(d.get('meta', {}).get('commitHash', ''))
break
" 2>/dev/null)
deploy_ts=$(echo "$deploy_json" | python3 -c "
import json, sys
data = json.load(sys.stdin)
for d in data:
if d.get('status') == 'SUCCESS':
print(d.get('createdAt', ''))
break
" 2>/dev/null)
if [[ -z "$deploy_commit" ]]; then
err "No successful deployment found for $SERVICE"
exit 1
fi
repo_slug=$(gh repo view --json nameWithOwner -q .nameWithOwner 2>/dev/null) || {
err "Failed to detect GitHub repo. Run from within the repo directory."
exit 2
}
main_sha=$(gh api "repos/$repo_slug/commits/main" --jq .sha 2>/dev/null) || {
err "Failed to fetch HEAD of main from GitHub."
exit 2
}
deploy_short="${deploy_commit:0:7}"
main_short="${main_sha:0:7}"
if [[ "$main_sha" == "$deploy_commit"* ]] || [[ "$deploy_commit" == "$main_sha"* ]]; then
log "✅ $SERVICE is up to date (deployed: $deploy_short, main: $main_short)"
exit 0
fi
git fetch origin main --quiet 2>/dev/null || true
is_ancestor=0
if git merge-base --is-ancestor "$deploy_commit" origin/main 2>/dev/null; then
is_ancestor=1
fi
behind_count=""
if [[ "$is_ancestor" == "1" ]]; then
behind_count=$(git rev-list --count "$deploy_commit..origin/main" 2>/dev/null || echo "?")
fi
age_hours=""
if [[ -n "$deploy_ts" ]]; then
deploy_epoch=$(python3 -c "
from datetime import datetime, timezone
ts = '$deploy_ts'.replace('Z', '+00:00')
print(int(datetime.fromisoformat(ts).timestamp()))
" 2>/dev/null || echo "")
if [[ -n "$deploy_epoch" ]]; then
now_epoch=$(date +%s)
age_secs=$((now_epoch - deploy_epoch))
age_hours=$((age_secs / 3600))
fi
fi
log ""
log "⚠️ $SERVICE is BEHIND origin/main"
log " Deployed commit: $deploy_short ($deploy_ts)"
log " main HEAD: $main_short"
[[ -n "$behind_count" ]] && log " Commits behind: $behind_count"
[[ -n "$age_hours" ]] && log " Deploy age: ${age_hours}h"
log ""
if [[ -n "$age_hours" ]] && (( age_hours > MAX_AGE_HOURS )); then
log "🚨 Deploy is ${age_hours}h old (threshold: ${MAX_AGE_HOURS}h)"
log " Railway's GitHub integration may have disconnected."
log " Fix: Railway dashboard → $SERVICE → Settings → Source → Reconnect repo"
log ""
fi
exit 1