-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
76 lines (66 loc) · 2.9 KB
/
start.sh
File metadata and controls
76 lines (66 loc) · 2.9 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
#!/usr/bin/env bash
set -euo pipefail
# ── Required env ─────────────────────────────────────────────────────
: "${GITHUB_PAT:?GITHUB_PAT must be set}"
: "${GITHUB_ORG:?GITHUB_ORG must be set}"
RUNNER_NAME="${RUNNER_NAME:-$(hostname)}"
RUNNER_LABELS="${RUNNER_LABELS:-self-hosted,linux,x64,nullrabbit}"
RUNNER_GROUP="${RUNNER_GROUP:-default}"
RUNNER_WORKDIR="${RUNNER_WORKDIR:-_work}"
# ── Mint a fresh registration token ─────────────────────────────────
echo "Requesting registration token for org: ${GITHUB_ORG}..."
REG_TOKEN=$(curl -sX POST \
-H "Authorization: token ${GITHUB_PAT}" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/orgs/${GITHUB_ORG}/actions/runners/registration-token" \
| jq -r '.token')
if [ -z "${REG_TOKEN}" ] || [ "${REG_TOKEN}" = "null" ]; then
echo "ERROR: Failed to obtain registration token. Check GITHUB_PAT permissions." >&2
exit 1
fi
# ── Wait for DinD sidecar ────────────────────────────────────────────
if [ -n "${DOCKER_HOST:-}" ]; then
echo "Waiting for Docker daemon at ${DOCKER_HOST}..."
for i in $(seq 1 30); do
if docker info >/dev/null 2>&1; then
echo "Docker daemon is ready."
break
fi
if [ "$i" -eq 30 ]; then
echo "ERROR: Docker daemon not available after 30s." >&2
exit 1
fi
sleep 1
done
fi
# ── Cleanup function ────────────────────────────────────────────────
cleanup() {
echo "Caught signal — removing runner..."
REMOVE_TOKEN=$(curl -sX POST \
-H "Authorization: token ${GITHUB_PAT}" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/orgs/${GITHUB_ORG}/actions/runners/remove-token" \
| jq -r '.token')
./config.sh remove --token "${REMOVE_TOKEN}" 2>/dev/null || true
exit 0
}
trap cleanup SIGTERM SIGINT
# ── Configure runner ─────────────────────────────────────────────────
echo "Configuring runner: ${RUNNER_NAME}..."
./config.sh \
--url "https://github.com/${GITHUB_ORG}" \
--token "${REG_TOKEN}" \
--name "${RUNNER_NAME}" \
--labels "${RUNNER_LABELS}" \
--runnergroup "${RUNNER_GROUP}" \
--work "${RUNNER_WORKDIR}" \
--ephemeral \
--replace \
--disableupdate \
--unattended
# ── Run (background + wait for signal handling) ──────────────────────
echo "Starting runner..."
./run.sh &
wait $!
# ── Ephemeral mode: runner exits after one job — exit cleanly ────────
echo "Runner has finished (ephemeral). Exiting."