-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·137 lines (111 loc) · 4.25 KB
/
entrypoint.sh
File metadata and controls
executable file
·137 lines (111 loc) · 4.25 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
#!/usr/bin/env bash
set -euo pipefail
# ──────────────────────────────────────────────
# GitLab Runner Entrypoint
# Auto-registers the runner on first start and
# handles graceful shutdown with optional unregister.
# ──────────────────────────────────────────────
CONFIG_DIR="/etc/gitlab-runner"
CONFIG_FILE="${CONFIG_DIR}/config.toml"
# ── CA Certificate handling ───────────────────
setup_ca_certificate() {
local ca_file="${CA_CERTIFICATES_PATH:-}"
if [[ -z "$ca_file" ]]; then
return
fi
if [[ ! -f "$ca_file" ]]; then
echo "[entrypoint] WARNING: CA_CERTIFICATES_PATH set but file not found: ${ca_file}"
return
fi
echo "[entrypoint] Installing custom CA certificate from ${ca_file} ..."
cp "$ca_file" /usr/local/share/ca-certificates/custom-ca.crt
update-ca-certificates --fresh >/dev/null 2>&1 || true
}
# ── Registration check ────────────────────────
is_registered() {
[[ -f "$CONFIG_FILE" ]] && grep -q '\[\[runners\]\]' "$CONFIG_FILE" 2>/dev/null
}
# ── Register runner ───────────────────────────
register_runner() {
if [[ -z "${CI_SERVER_URL:-}" ]]; then
echo "[entrypoint] ERROR: CI_SERVER_URL is required but not set."
exit 1
fi
if [[ -z "${RUNNER_TOKEN:-}" ]]; then
echo "[entrypoint] ERROR: RUNNER_TOKEN is required but not set."
exit 1
fi
local executor="${RUNNER_EXECUTOR:-docker}"
echo "[entrypoint] Registering runner at ${CI_SERVER_URL} ..."
local args=(
--non-interactive
--url "$CI_SERVER_URL"
--token "$RUNNER_TOKEN"
--name "${RUNNER_NAME:-unraid-runner}"
--executor "$executor"
)
# Docker-executor specific options
if [[ "$executor" == "docker" ]]; then
args+=(--docker-image "${DOCKER_IMAGE:-alpine:latest}")
if [[ "${DOCKER_PRIVILEGED:-false}" == "true" ]]; then
args+=(--docker-privileged)
fi
local docker_volumes="${DOCKER_VOLUMES:-}"
docker_volumes="$(echo "$docker_volumes" | xargs)" # trim whitespace
if [[ -n "$docker_volumes" ]]; then
IFS=',' read -ra vols <<< "$docker_volumes"
for vol in "${vols[@]}"; do
vol="$(echo "$vol" | xargs)" # trim whitespace
[[ -n "$vol" ]] && args+=(--docker-volumes "$vol")
done
fi
fi
# Legacy token options (ignored by modern runner tokens glrt-*)
if [[ -n "${RUNNER_TAG_LIST:-}" ]]; then
args+=(--tag-list "$RUNNER_TAG_LIST")
fi
if [[ -n "${RUNNER_UNTAGGED:-}" ]]; then
args+=(--run-untagged="$RUNNER_UNTAGGED")
fi
if [[ -n "${RUNNER_LOCKED:-}" ]]; then
args+=(--locked="$RUNNER_LOCKED")
fi
gitlab-runner register "${args[@]}"
echo "[entrypoint] Runner registered successfully."
}
# ── Patch global config ───────────────────────
patch_global_config() {
local concurrent="${RUNNER_CONCURRENT:-1}"
if [[ ! -f "$CONFIG_FILE" ]]; then
return
fi
if grep -q '^concurrent' "$CONFIG_FILE"; then
sed -i "s/^concurrent *=.*/concurrent = ${concurrent}/" "$CONFIG_FILE"
else
sed -i "1i concurrent = ${concurrent}" "$CONFIG_FILE"
fi
echo "[entrypoint] Set concurrent = ${concurrent}"
}
# ── Graceful shutdown ─────────────────────────
shutdown() {
echo "[entrypoint] Received shutdown signal, stopping runner ..."
gitlab-runner stop 2>/dev/null || true
if [[ "${UNREGISTER_ON_STOP:-false}" == "true" ]]; then
echo "[entrypoint] Unregistering runner ..."
gitlab-runner unregister --all-runners 2>/dev/null || true
fi
exit 0
}
trap shutdown SIGQUIT SIGTERM SIGINT
# ── Main ──────────────────────────────────────
setup_ca_certificate
if ! is_registered; then
register_runner
patch_global_config
else
echo "[entrypoint] Runner already registered, skipping registration."
# Still patch concurrent in case the env var changed
patch_global_config
fi
echo "[entrypoint] Starting gitlab-runner ..."
exec gitlab-runner "$@"