Skip to content

Instantly share code, notes, and snippets.

@adammontlake
Last active March 11, 2024 14:05
Show Gist options
  • Save adammontlake/7bee567951a8d25e5a01977303fb9c8a to your computer and use it in GitHub Desktop.
Save adammontlake/7bee567951a8d25e5a01977303fb9c8a to your computer and use it in GitHub Desktop.
Github Self-hosted runner setup script
server_url="REPLACE_WITH_SERVER_URL"
server_api_url="api.$server_url"
org="REPLACE_WITH_ORG_NAME"
repo="REPLACE_WITH_REPO_NAME"
registration_token_raw=$(curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://$server_api_url/repos/$org/$repo/actions/runners/registration-token)
# Get binary downloading token from GitHub server
download_details_raw=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
https://$server_api_url/repos/$org/$repo/actions/runners/downloads)
# Regex definition for download_url, download_filename,download_token and registration_token
download_url_regex='download_url\":\ \"(https://[-[:alnum:]/_:.]+linux-x64[-[:alnum:]/.]+)\"'
download_filename_regex='filename\":\ \"([-[:alnum:].]+linux-x64[-[:alnum:].]+.gz)\"'
download_token_regex='temp_download_token\":\ \"([A-z0-9.\n\-]+)"'
registration_token_regex='token\":\ \"([A-z0-9.\-]+)\"'
# Extract data from raw response
if [[ $download_details_raw =~ $download_url_regex ]]; then download_url="${BASH_REMATCH[1]}"; else emit "no match found for: download_url_regex"; fi
if [[ $download_details_raw =~ $download_filename_regex ]]; then filename="${BASH_REMATCH[1]}"; else emit "no match found for: download_filename_regex"; fi
if [[ $download_details_raw =~ $download_token_regex ]]; then download_token="${BASH_REMATCH[1]}"; else download_token=$GH_TOKEN; fi
if [[ $registration_token_raw =~ $registration_token_regex ]]; then registration_token="${BASH_REMATCH[1]}"; else emit "no match found for: registration_token_regex"; fi
# Download agent binary
emit "Download binary"
curl -L \
-o $filename \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $download_token" \
$download_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment