Last active
June 26, 2024 03:34
-
-
Save whonion/cbbbaa38ba4690c5aa450a7556193fa9 to your computer and use it in GitHub Desktop.
github-runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Prompt for GitHub username | |
read -p "Enter GitHub username: " USER_NAME | |
# Prompt for repository name | |
read -p "Enter repository name: " RUNNER_FOLDER | |
# Prompt for GitHub Runner token | |
read -p "Enter GITHUB_RUNNER_TOKEN: " GITHUB_RUNNER_TOKEN | |
# Form the service name from 'runner-' and the repository name | |
SERVICE_NAME="runner-$RUNNER_FOLDER" | |
# Form the repository URL | |
REPO_URL="https://github.com/${USER_NAME}/${RUNNER_FOLDER}" | |
# Get the latest version of github-runner | |
latest_version=$(curl -s https://api.github.com/repos/actions/runner/releases/latest | grep "tag_name" | cut -d'"' -f4) | |
clean_version=${latest_version#v} | |
# Check if user 'github' exists, if not, create it | |
if id "github" &>/dev/null; then | |
echo "User github already exists" | |
else | |
sudo adduser --disabled-password --gecos "" github | |
fi | |
# Create the task folder in the home directory of the 'github' user | |
RUNNER_PATH="/home/github/$RUNNER_FOLDER" | |
sudo mkdir -p "$RUNNER_PATH" | |
sudo chown -R github:github "$RUNNER_PATH" | |
cd "$RUNNER_PATH" | |
# Download the latest version of github-runner as the 'github' user | |
ARCHIVE_NAME="actions-runner-linux-x64-${clean_version}.tar.gz" | |
DOWNLOAD_URL="https://github.com/actions/runner/releases/download/${latest_version}/actions-runner-linux-x64-${clean_version}.tar.gz" | |
echo "Downloading from: $DOWNLOAD_URL" | |
sudo -u github curl -o $ARCHIVE_NAME -L $DOWNLOAD_URL | |
# Check if the download was successful and the file exists | |
if [[ $? -ne 0 || ! -f $ARCHIVE_NAME ]]; then | |
echo "Error: failed to download file $ARCHIVE_NAME" | |
exit 1 | |
fi | |
# Check if the downloaded file is a gzip archive | |
if ! sudo -u github file $ARCHIVE_NAME | grep -q 'gzip compressed data'; then | |
echo "Error: downloaded file is not a gzip archive" | |
exit 1 | |
fi | |
# Extract the archive in the current directory as the 'github' user | |
if ! sudo -u github tar -xzf $ARCHIVE_NAME --strip-components=1; then | |
echo "Error: failed to extract file $ARCHIVE_NAME" | |
exit 1 | |
fi | |
# Register the GitHub Runner as the 'github' user | |
if [[ -f ./config.sh ]]; then | |
sudo -u github ./config.sh --url "$REPO_URL" --token "$GITHUB_RUNNER_TOKEN" | |
else | |
echo "Error: config.sh file not found" | |
exit 1 | |
fi | |
# Create a systemd service | |
SERVICE_PATH="/etc/systemd/system/$SERVICE_NAME.service" | |
sudo bash -c "cat > $SERVICE_PATH" <<EOL | |
[Unit] | |
Description=GitHub Actions Runner | |
After=network.target | |
[Service] | |
ExecStart=$RUNNER_PATH/run.sh | |
User=github | |
WorkingDirectory=$RUNNER_PATH | |
Restart=always | |
[Install] | |
WantedBy=default.target | |
EOL | |
# Reload systemd and enable the service | |
sudo systemctl daemon-reload | |
sudo systemctl enable $SERVICE_NAME | |
sudo systemctl start $SERVICE_NAME | |
echo "GitHub Runner installed and started as service $SERVICE_NAME." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment