Skip to content

Instantly share code, notes, and snippets.

@comaniac
Created February 10, 2022 01:24
Show Gist options
  • Save comaniac/2bf3c294282c1e6c09ac1e85396ef2b7 to your computer and use it in GitHub Desktop.
Save comaniac/2bf3c294282c1e6c09ac1e85396ef2b7 to your computer and use it in GitHub Desktop.
Register and launch a Github Action Runner for org, and remove it when exit.
#!/usr/bin/env bash
set -e
RUNNER_VERSION="2.287.1"
# The path to the new runner.
RUNNER_PATH=$1
# The target Github org.
GITHUB_ORG=$2
# Optional runner label.
LABEL=$3
if [ $# -lt 2 ]; then
echo "Usage: <runner-path> <github-org-name> [<label>]"
exit 1
fi
if [ ! -z $LABEL ]; then
LABEL="--labels $LABEL"
fi
if [ -z $GITHUB_TOKEN ]; then
echo "GITHUB_TOKEN is unset"
exit 1
fi
# Apply for a runner registration token.
TOKEN=$(curl -sX POST -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/orgs/${GITHUB_ORG}/actions/runners/registration-token | jq .token --raw-output)
if [ -z $TOKEN ]; then
echo "Failed to get the token"
exit 1
fi
echo "TOKEN: $TOKEN"
# Initialize
rm -rf $RUNNER_PATH
mkdir $RUNNER_PATH
cd $RUNNER_PATH
curl -O -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz
tar xzf ./actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz
# Configure
./config.sh --url https://github.com/${GITHUB_ORG} --token $TOKEN --name $RUNNER_PATH $LABEL --unattended
cleanup() {
echo "Removing runner $RUNNER_PATH"
./config.sh remove --unattended --token ${TOKEN}
}
trap "cleanup; exit 130" INT
trap "cleanup; exit 143" TERM
./run.sh & wait $!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment