Skip to content

Instantly share code, notes, and snippets.

@davi020
Last active June 21, 2024 03:19
Show Gist options
  • Save davi020/a7d77cea1f8e575e6d3595a10d0812ed to your computer and use it in GitHub Desktop.
Save davi020/a7d77cea1f8e575e6d3595a10d0812ed to your computer and use it in GitHub Desktop.
Shell script to setup multiple selfhosted github runner service in an AL2 server
#!/bin/bash
# Get runner version dynamically from GitHub API
runner_version=$(curl --silent "https://api.github.com/repos/actions/runner/releases/latest" | jq -r '.tag_name[1:]')
echo "GitHub runner version: $runner_version"
# Get user and group input
read -p "Enter the action process username: " action_process_user
read -sp "Enter your GitHub PAT (masked): " PAT
echo
read -p "Enter the repository URL: " repo_url
read -p "Enter the number of GitHub runner services to create: " count
# Base directory for GitHub runner services
base_dir="/apps"
working_dir_base="/apps/actions-runners/action-runner-"
# Extract GitHub username and repository name from the repository URL
github_user_name=$(echo "$repo_url" | cut -d'/' -f4)
your_repo_name=$(echo "$repo_url" | cut -d'/' -f5)
# Function to generate GitHub personal access token via API
generate_access_token() {
echo "Generating GitHub actions-runners-registration token..."
# Create a registration token for a repository
access_token=$(curl -s -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $PAT" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$github_user_name/$your_repo_name/actions/runners/registration-token" \
| jq -r .token)
if [ -z "$access_token" ]; then
echo "Failed to generate actions-runners-registration token."
exit 1
fi
echo "Actions-runners-registration token generated successfully."
}
# Generate GitHub personal access token
generate_access_token
# Function to get the hostname of the machine
get_hostname() {
hostname=$(hostname)
if [ -z "$hostname" ]; then
echo "Failed to retrieve hostname."
exit 1
fi
echo "$hostname"
}
# Get the hostname
hostname=$(get_hostname)
# Create working directories if not present and set ownership permissions
for ((i=1; i<=$count; i++)); do
working_dir="${working_dir_base}${i}"
if [ ! -d "$working_dir" ]; then
sudo mkdir -p "$working_dir"
fi
sudo chown -R "$action_process_user":"$action_process_user" "$working_dir"
done
# Install and configure GitHub runner for each working directory
for ((i=1; i<=$count; i++)); do
working_dir="${working_dir_base}${i}"
cd "$working_dir" || exit
if [ ! -d "actions-runner-$i" ]; then
# Download GitHub runner
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"
rm -f "./actions-runner-linux-x64-${runner_version}.tar.gz"
# Configure GitHub runner with the generated access token and hostname label along with default labels
./config.sh --url "$repo_url" --token "$access_token" --name "actions-runner-$i" --unattended --replace --labels "$hostname"
# Start the GitHub runner service
sudo ./svc.sh install
sudo ./svc.sh start
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment