-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart-tunnel.sh
More file actions
executable file
·181 lines (155 loc) · 5.63 KB
/
start-tunnel.sh
File metadata and controls
executable file
·181 lines (155 loc) · 5.63 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
#!/usr/bin/env bash
# GitHub Webhook Tunnel Manager for OpenTelemetry Collector (Prometheus-only)
# - Exposes local collector on port 9504 via Cloudflare Tunnel
# - Prints the public URL to configure as GitHub Webhook
# - Aligns with current collector-config.yaml: path=/events, health_path=/health
set -euo pipefail
LOG_FILE="tunnel.log"
LOCAL_URL="http://localhost:9504"
WEBHOOK_PATH="/events"
HEALTH_PATH="/health"
BOLD="\033[1m"
RESET="\033[0m"
banner() {
echo ""
echo "🚀 ${BOLD}Starting Cloudflare Tunnel for GitHub Webhooks${RESET}"
echo "=================================================="
}
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "❌ Required command '$1' not found." >&2
if [ "$1" = "cloudflared" ]; then
echo "👉 Install on macOS (Homebrew): brew install cloudflare/cloudflare/cloudflared" >&2
echo " Docs: https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/" >&2
fi
exit 127
fi
}
cleanup() {
if [ -n "${TUNNEL_PID:-}" ] && kill -0 "${TUNNEL_PID}" 2>/dev/null; then
echo ""
echo "🛑 Stopping tunnel (PID: ${TUNNEL_PID})..."
kill "${TUNNEL_PID}" 2>/dev/null || true
sleep 2
fi
echo "👋 Goodbye!"
exit 0
}
start_tunnel() {
echo "🌀 Starting tunnel..."
# Set up cleanup trap
trap cleanup INT TERM EXIT
# Start tunnel in background with nohup to prevent interruption
nohup cloudflared tunnel --url "${LOCAL_URL}" >"${LOG_FILE}" 2>&1 &
TUNNEL_PID=$!
echo "⏳ Waiting for tunnel to initialize..."
# Try reading URL from log with multiple patterns
local url=""
local last_error=""
for attempt in $(seq 1 60); do
if [ -f "${LOG_FILE}" ]; then
# Look for the URL in various formats that cloudflared outputs
url=$(grep -oE 'https://[a-zA-Z0-9_-]+\.trycloudflare\.com' "${LOG_FILE}" | head -1 || true)
# Also try to find it in connection messages
if [ -z "${url}" ]; then
url=$(grep -E '(Your quick Tunnel|INF \+)' "${LOG_FILE}" | grep -oE 'https://[a-zA-Z0-9_-]+\.trycloudflare\.com' | head -1 || true)
fi
# Check for successful connection indicator
if [ -z "${url}" ]; then
if grep -q "Connection.*registered" "${LOG_FILE}" 2>/dev/null; then
# Connection registered but URL not found yet, keep waiting
:
elif grep -q "ERR.*Serve tunnel error" "${LOG_FILE}" 2>/dev/null; then
last_error="Tunnel connection errors detected"
fi
fi
if [ -n "${url}" ]; then
break
fi
fi
# Check if process is still alive
if ! kill -0 ${TUNNEL_PID} 2>/dev/null; then
echo "❌ Tunnel process died unexpectedly" >&2
break
fi
sleep 1
if [ $((attempt % 10)) -eq 0 ]; then
echo " Attempt ${attempt}/60... (waiting for successful connection)"
fi
done
if [ -z "${url}" ]; then
echo "❌ Failed to get tunnel URL. Check ${LOG_FILE} for errors." >&2
if [ -n "${last_error}" ]; then
echo " ${last_error}"
fi
echo " Tip: Cloudflare tunnel may be experiencing issues. Try again in a moment."
echo ""
echo "📝 Full log output:"
cat "${LOG_FILE}" 2>/dev/null || echo " (log file empty or unreadable)"
kill ${TUNNEL_PID} >/dev/null 2>&1 || true
exit 1
fi
echo ""
echo "🎉 Tunnel is running!"
echo "📍 Your GitHub Webhook Payload URL:"
echo " ${url}${WEBHOOK_PATH}"
echo ""
echo "🔧 Configure your GitHub webhook at your repository settings (Settings → Webhooks → Add webhook)."
echo " - Payload URL: ${url}${WEBHOOK_PATH}"
echo " - Content type: application/json"
echo " - Secret: Use GITHUB_WEBHOOK_SECRET from your .env (do not paste here)"
echo " - Events: ✅ Workflow runs, ✅ Workflow jobs (and any others you need)"
echo ""
echo "📋 Tunnel PID: ${TUNNEL_PID}"
echo "📝 Logs: tail -f ${LOG_FILE}"
echo "⏹️ Stop: kill ${TUNNEL_PID}"
echo ""
echo "🩺 Checking local collector health (${LOCAL_URL}${HEALTH_PATH})..."
local health
health=$(curl -s --max-time 5 "${LOCAL_URL}${HEALTH_PATH}" || true)
if echo "${health}" | grep -qi healthy; then
echo "✅ Collector health endpoint reports healthy"
else
echo "⚠️ Health endpoint response: ${health}"
fi
echo "🧪 Testing webhook endpoint accessibility (${url}${WEBHOOK_PATH})..."
# Post minimal JSON; 200/400/500 are all acceptable as 'reachable' depending on receiver behavior
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 \
-X POST "${url}${WEBHOOK_PATH}" \
-H "Content-Type: application/json" \
-H "X-GitHub-Event: ping" \
-d '{"zen":"test"}' || echo "timeout")
case "${http_code}" in
200|400|401|403|422|500)
echo "✅ Webhook endpoint reachable (HTTP ${http_code})"
;;
timeout)
echo "❌ Request timed out; check connectivity/logs."
;;
*)
echo "⚠️ Webhook endpoint returned status: HTTP ${http_code}"
;;
esac
echo ""
echo "🔄 Tunnel is running in background. To stop: kill ${TUNNEL_PID}"
echo " Keep this shell open or monitor logs with: tail -f ${LOG_FILE}"
echo ""
echo "🎯 ${BOLD}WEBHOOK URL (copy this for GitHub):${RESET}"
echo " ${url}${WEBHOOK_PATH}"
echo ""
# Keep script attached, printing a heartbeat with URL
echo "⏰ Starting continuous monitoring... (URL will be logged every 15 seconds)"
while kill -0 ${TUNNEL_PID} 2>/dev/null; do
sleep 15
echo "🔗 $(date '+%H:%M:%S'): Tunnel ACTIVE → ${url}${WEBHOOK_PATH}"
done
echo "🛑 Tunnel stopped."
}
main() {
banner
need_cmd curl
need_cmd cloudflared
start_tunnel
}
main "$@"