|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# ────────────────────────────────────────────── |
| 5 | +# GitLab Runner Entrypoint |
| 6 | +# Auto-registers the runner on first start and |
| 7 | +# handles graceful shutdown with optional unregister. |
| 8 | +# ────────────────────────────────────────────── |
| 9 | + |
| 10 | +CONFIG_DIR="/etc/gitlab-runner" |
| 11 | +CONFIG_FILE="${CONFIG_DIR}/config.toml" |
| 12 | + |
| 13 | +# ── CA Certificate handling ─────────────────── |
| 14 | +setup_ca_certificate() { |
| 15 | + local ca_file="${CA_CERTIFICATES_PATH:-}" |
| 16 | + if [[ -z "$ca_file" ]]; then |
| 17 | + return |
| 18 | + fi |
| 19 | + |
| 20 | + if [[ ! -f "$ca_file" ]]; then |
| 21 | + echo "[entrypoint] WARNING: CA_CERTIFICATES_PATH set but file not found: ${ca_file}" |
| 22 | + return |
| 23 | + fi |
| 24 | + |
| 25 | + echo "[entrypoint] Installing custom CA certificate from ${ca_file} ..." |
| 26 | + cp "$ca_file" /usr/local/share/ca-certificates/custom-ca.crt |
| 27 | + update-ca-certificates --fresh >/dev/null 2>&1 || true |
| 28 | +} |
| 29 | + |
| 30 | +# ── Registration check ──────────────────────── |
| 31 | +is_registered() { |
| 32 | + [[ -f "$CONFIG_FILE" ]] && grep -q '\[\[runners\]\]' "$CONFIG_FILE" 2>/dev/null |
| 33 | +} |
| 34 | + |
| 35 | +# ── Register runner ─────────────────────────── |
| 36 | +register_runner() { |
| 37 | + if [[ -z "${CI_SERVER_URL:-}" ]]; then |
| 38 | + echo "[entrypoint] ERROR: CI_SERVER_URL is required but not set." |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + if [[ -z "${RUNNER_TOKEN:-}" ]]; then |
| 42 | + echo "[entrypoint] ERROR: RUNNER_TOKEN is required but not set." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + |
| 46 | + local executor="${RUNNER_EXECUTOR:-docker}" |
| 47 | + |
| 48 | + echo "[entrypoint] Registering runner at ${CI_SERVER_URL} ..." |
| 49 | + |
| 50 | + local args=( |
| 51 | + --non-interactive |
| 52 | + --url "$CI_SERVER_URL" |
| 53 | + --token "$RUNNER_TOKEN" |
| 54 | + --name "${RUNNER_NAME:-unraid-runner}" |
| 55 | + --executor "$executor" |
| 56 | + ) |
| 57 | + |
| 58 | + # Docker-executor specific options |
| 59 | + if [[ "$executor" == "docker" ]]; then |
| 60 | + args+=(--docker-image "${DOCKER_IMAGE:-alpine:latest}") |
| 61 | + |
| 62 | + if [[ "${DOCKER_PRIVILEGED:-false}" == "true" ]]; then |
| 63 | + args+=(--docker-privileged) |
| 64 | + fi |
| 65 | + |
| 66 | + if [[ -n "${DOCKER_VOLUMES:-}" ]]; then |
| 67 | + IFS=',' read -ra vols <<< "$DOCKER_VOLUMES" |
| 68 | + for vol in "${vols[@]}"; do |
| 69 | + vol="$(echo "$vol" | xargs)" # trim whitespace |
| 70 | + [[ -n "$vol" ]] && args+=(--docker-volumes "$vol") |
| 71 | + done |
| 72 | + fi |
| 73 | + fi |
| 74 | + |
| 75 | + # Legacy token options (ignored by modern runner tokens glrt-*) |
| 76 | + if [[ -n "${RUNNER_TAG_LIST:-}" ]]; then |
| 77 | + args+=(--tag-list "$RUNNER_TAG_LIST") |
| 78 | + fi |
| 79 | + if [[ -n "${RUNNER_UNTAGGED:-}" ]]; then |
| 80 | + args+=(--run-untagged="$RUNNER_UNTAGGED") |
| 81 | + fi |
| 82 | + if [[ -n "${RUNNER_LOCKED:-}" ]]; then |
| 83 | + args+=(--locked="$RUNNER_LOCKED") |
| 84 | + fi |
| 85 | + |
| 86 | + gitlab-runner register "${args[@]}" |
| 87 | + echo "[entrypoint] Runner registered successfully." |
| 88 | +} |
| 89 | + |
| 90 | +# ── Patch global config ─────────────────────── |
| 91 | +patch_global_config() { |
| 92 | + local concurrent="${RUNNER_CONCURRENT:-1}" |
| 93 | + |
| 94 | + if [[ ! -f "$CONFIG_FILE" ]]; then |
| 95 | + return |
| 96 | + fi |
| 97 | + |
| 98 | + if grep -q '^concurrent' "$CONFIG_FILE"; then |
| 99 | + sed -i "s/^concurrent *=.*/concurrent = ${concurrent}/" "$CONFIG_FILE" |
| 100 | + else |
| 101 | + sed -i "1i concurrent = ${concurrent}" "$CONFIG_FILE" |
| 102 | + fi |
| 103 | + |
| 104 | + echo "[entrypoint] Set concurrent = ${concurrent}" |
| 105 | +} |
| 106 | + |
| 107 | +# ── Graceful shutdown ───────────────────────── |
| 108 | +shutdown() { |
| 109 | + echo "[entrypoint] Received shutdown signal, stopping runner ..." |
| 110 | + gitlab-runner stop 2>/dev/null || true |
| 111 | + |
| 112 | + if [[ "${UNREGISTER_ON_STOP:-false}" == "true" ]]; then |
| 113 | + echo "[entrypoint] Unregistering runner ..." |
| 114 | + gitlab-runner unregister --all-runners 2>/dev/null || true |
| 115 | + fi |
| 116 | + |
| 117 | + exit 0 |
| 118 | +} |
| 119 | + |
| 120 | +trap shutdown SIGQUIT SIGTERM SIGINT |
| 121 | + |
| 122 | +# ── Main ────────────────────────────────────── |
| 123 | +setup_ca_certificate |
| 124 | + |
| 125 | +if ! is_registered; then |
| 126 | + register_runner |
| 127 | + patch_global_config |
| 128 | +else |
| 129 | + echo "[entrypoint] Runner already registered, skipping registration." |
| 130 | + # Still patch concurrent in case the env var changed |
| 131 | + patch_global_config |
| 132 | +fi |
| 133 | + |
| 134 | +echo "[entrypoint] Starting gitlab-runner ..." |
| 135 | +exec gitlab-runner "$@" |
0 commit comments