fix(register_gitea_runner): update instance directory handling and token retrieval #106

Open
Richie wants to merge 1 commits from bugfix/fix-gitbub-runnets into main
pull from: bugfix/fix-gitbub-runnets
@@ -1,21 +1,38 @@
#!/usr/bin/env bash
legacy_instance_dir="$STATE_DIRECTORY/$(hostname)"
instance_dir="$STATE_DIRECTORY/$1"
# Newer runner modules export INSTANCE_DIR and multiple colon-separated state paths.
instance_dir="${INSTANCE_DIR:-${STATE_DIRECTORY%%:*}/$1}"
legacy_instance_dir="$(dirname "$instance_dir")/$(hostname)"
runner_url="$2"
runner_config_file="$3"
shift 3
runner_labels="$(IFS=,; printf '%s' "$*")"
if [ ! -e "$instance_dir" ] && [ -d "$legacy_instance_dir" ]; then
mv "$legacy_instance_dir" "$instance_dir"
if [ "$legacy_instance_dir" != "$instance_dir" ] && [ -d "$legacy_instance_dir" ]; then
# systemd may have already created an empty instance directory.
if [ ! -e "$instance_dir" ] || rmdir "$instance_dir" 2>/dev/null; then
mv "$legacy_instance_dir" "$instance_dir"
fi
fi
mkdir -vp "$instance_dir"
cd "$instance_dir" || exit 1
# Newer modules use LoadCredential; older ones export TOKEN via EnvironmentFile.
runner_token="${TOKEN:-}"
if [ -n "${CREDENTIALS_DIRECTORY:-}" ]; then
runner_token="$(cat "$CREDENTIALS_DIRECTORY/token")"
# Keep accepting the legacy TOKEN= format as well as a raw token.
while IFS= read -r token_line; do
if [[ "$token_line" =~ ^[[:space:]]*TOKEN= ]]; then
runner_token="${token_line#*=}"
fi
done <<< "$runner_token"
fi
: "${runner_token:?Runner registration token is missing}"
token_hash_file="$instance_dir/.token-hash"
token_hash_current="$(printf '%s' "$TOKEN" | sha256sum | cut -d' ' -f1)"
token_hash_current="$(printf '%s' "$runner_token" | sha256sum | cut -d' ' -f1)"
token_hash_stored="$(cat "$token_hash_file" 2>/dev/null || echo "")"
labels_file="$instance_dir/.labels"
labels_wanted="$(printf '%s\n' "$@" | sort)"
@@ -26,7 +43,7 @@ if [ ! -e "$instance_dir/.runner" ] || [ "$labels_wanted" != "$labels_current" ]
gitea-runner register --no-interactive \
--instance "$runner_url" \
--token "$TOKEN" \
--token "$runner_token" \
--name "jeeves-$(hostname)" \
--labels "$runner_labels" \
--config "$runner_config_file"