Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,33 @@ install_actions_runner_if_configured() {

# Runner-provided dependency installer; must run as root.
if [[ -x "$dir/bin/installdependencies.sh" ]]; then
run_cmd "$dir/bin/installdependencies.sh"
# Runner scripts often assume they are executed from within the runner dir.
run_cmd bash -c "cd \"\$1\" && exec ./bin/installdependencies.sh" bash "$dir"
fi

# Ensure the runner files are owned by the dedicated user.
run_cmd chown -R "$user:$user" "$dir"
}

validate_actions_runner_registration_inputs() {
local github_url="$1"
local reg_token="$2"

# Users occasionally paste API endpoints; the runner expects the *web* URL.
if [[ "$github_url" == *"api.github.com"* || "$github_url" == */api/v3* ]]; then
cover_path "install:actions-runner-config-invalid-url"
die "RUNNER_GITHUB_URL must be the web URL for the org/repo (e.g. https://github.com/ORG or https://github.com/ORG/REPO), not an API URL: $github_url"
fi

# GitHub returns 404 for invalid auth; a common cause is a PAT instead of a registration token.
if [[ "$reg_token" == ghp_* || "$reg_token" == github_pat_* || "$reg_token" == gho_* || "$reg_token" == ghu_* || "$reg_token" == ghs_* || "$reg_token" == ghr_* ]]; then
cover_path "install:actions-runner-config-pat"
die "RUNNER_REGISTRATION_TOKEN looks like a GitHub token (PAT/app). Use the short-lived *runner registration token* from Settings → Actions → Runners → New self-hosted runner. A wrong/expired token commonly shows up as HTTP 404 during registration."
fi

cover_path "install:actions-runner-config-inputs-ok"
}

configure_actions_runner_if_configured() {
# Optional feature:
# If the user provides GitHub URL + registration token, configure the runner.
Expand All @@ -146,6 +166,8 @@ configure_actions_runner_if_configured() {
fi
cover_path "install:actions-runner-config-vars-present"

validate_actions_runner_registration_inputs "$github_url" "$reg_token"

local dir
dir="$(runner_dir)"
if [[ ! -x "$dir/config.sh" ]]; then
Expand All @@ -164,12 +186,15 @@ configure_actions_runner_if_configured() {

local name="${RUNNER_NAME:-$(hostname)}"

local -a cmd=("$dir/config.sh" --unattended --url "$github_url" --token "$reg_token" --name "$name")
local -a cmd=("./config.sh" --unattended --url "$github_url" --token "$reg_token" --name "$name")
if command -v runuser > /dev/null 2>&1; then
run_cmd runuser -u "$user" -- "${cmd[@]}"
# Run from within the runner directory so runner scripts can use ./bin/ paths.
run_cmd runuser -u "$user" -- bash -c "cd \"\$1\"; shift; exec \"\$@\"" bash "$dir" "${cmd[@]}"
else
# Fallback for environments without runuser.
run_cmd su -s /bin/bash -c "\"$dir/config.sh\" --unattended --url \"$github_url\" --token \"$reg_token\" --name \"$name\"" "$user"
local su_cmd
su_cmd="$(printf 'cd %q && exec %q --unattended --url %q --token %q --name %q' "$dir" "./config.sh" "$github_url" "$reg_token" "$name")"
run_cmd su -s /bin/bash -c "$su_cmd" "$user"
fi
}

Expand Down
18 changes: 18 additions & 0 deletions tests/bin/kcov-line-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,24 @@ export RUNNER_REGISTRATION_TOKEN="token"
export RUNNER_NAME="test"
( set +e; configure_actions_runner_if_configured; exit 0 ) >/dev/null 2>&1 || true

# Configure: reject API URLs (common misconfiguration).
export RUNNER_GITHUB_URL="https://api.github.com/example/repo"
export RUNNER_REGISTRATION_TOKEN="token"
( set +e; configure_actions_runner_if_configured; exit 0 ) >/dev/null 2>&1 || true

# Reset to valid inputs for subsequent coverage.
export RUNNER_GITHUB_URL="https://github.com/example/repo"
export RUNNER_REGISTRATION_TOKEN="token"

# Configure: reject PAT-like tokens (404 during registration is a common symptom).
export RUNNER_GITHUB_URL="https://github.com/example/repo"
export RUNNER_REGISTRATION_TOKEN="ghp_examplepat"
( set +e; configure_actions_runner_if_configured; exit 0 ) >/dev/null 2>&1 || true

# Reset to valid inputs for subsequent coverage.
export RUNNER_GITHUB_URL="https://github.com/example/repo"
export RUNNER_REGISTRATION_TOKEN="token"

# Configure: present + config.sh, cover both runuser and fallback branches.
dir="\$(runner_dir)"
mkdir -p "\$dir"
Expand Down