|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Define multiple runner configurations |
| 4 | +# declare -A RUNNERS=( |
| 5 | +# ["runner-1"]="https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-linux-x64-2.317.0.tar.gz" |
| 6 | +# ) |
| 7 | + |
| 8 | +DEFAULT_USER="githubrunner" |
| 9 | +USER_HOME="/home/$DEFAULT_USER" |
| 10 | +USER_PASSWORD="password" |
| 11 | +RUNNER_VERSION="2.317.0" |
| 12 | +RUNNER_PACKAGE="actions-runner-linux-x64-$RUNNER_VERSION.tar.gz" |
| 13 | + |
| 14 | +# Function to display error message and exit |
| 15 | +die() { |
| 16 | + echo >&2 "Error: $@" |
| 17 | + exit 1 |
| 18 | +} |
| 19 | + |
| 20 | +# Function to install required packages |
| 21 | +install_packages() { |
| 22 | + local packages="$@" |
| 23 | + if [ -x "$(command -v apt-get)" ]; then |
| 24 | + sudo DEBIAN_FRONTEND=noninteractive apt-get -y update |
| 25 | + sudo DEBIAN_FRONTEND=noninteractive apt-get -y install $packages || die "Failed to install $packages. Aborting." |
| 26 | + elif [ -x "$(command -v yum)" ]; then |
| 27 | + sudo yum -y install $packages || die "Failed to install $packages. Aborting." |
| 28 | + elif [ -x "$(command -v dnf)" ]; then |
| 29 | + sudo dnf -y install $packages || die "Failed to install $packages. Aborting." |
| 30 | + elif [ -x "$(command -v pacman)" ]; then |
| 31 | + sudo pacman -Sy --noconfirm $packages || die "Failed to install $packages. Aborting." |
| 32 | + else |
| 33 | + die "Unsupported package manager. Please install $packages manually." |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +# Function to check if a command exists |
| 38 | +command_exists() { |
| 39 | + command -v "$1" >/dev/null 2>&1 |
| 40 | +} |
| 41 | + |
| 42 | +# Function to set up runner directory and permissions |
| 43 | +setup_runner_directory() { |
| 44 | + local RUNNER_NAME="$1" |
| 45 | + local RUNNER_DIR="$USER_HOME/$RUNNER_NAME/actions-runner" |
| 46 | + |
| 47 | + sudo mkdir -p "$RUNNER_DIR" || die "Failed to create $RUNNER_DIR directory." |
| 48 | + sudo chown -R $DEFAULT_USER:$DEFAULT_USER "$RUNNER_DIR" || die "Failed to set ownership for $RUNNER_DIR." |
| 49 | +} |
| 50 | + |
| 51 | +# Function to download and extract GitHub Actions runner package |
| 52 | +download_and_extract_runner() { |
| 53 | + local RUNNER_NAME="$1" |
| 54 | + local EXPECTED_CHECKSUM="9e883d210df8c6028aff475475a457d380353f9d01877d51cc01a17b2a91161d" |
| 55 | + local RUNNER="https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-linux-x64-2.317.0.tar.gz" |
| 56 | + |
| 57 | + # Ensure directory exists and has correct ownership |
| 58 | + sudo mkdir -p "$USER_HOME/$RUNNER_NAME/actions-runner" || die "Failed to create $USER_HOME/$RUNNER_NAME/actions-runner directory." |
| 59 | + sudo chown -R $DEFAULT_USER:$DEFAULT_USER "$USER_HOME/$RUNNER_NAME/actions-runner" || die "Failed to set ownership for $USER_HOME/$RUNNER_NAME/actions-runner." |
| 60 | + |
| 61 | + # Download and verify checksum |
| 62 | + sudo -u $DEFAULT_USER curl -o "$USER_HOME/$RUNNER_NAME/actions-runner/$RUNNER_PACKAGE" -L "$RUNNER" || die "Failed to download $RUNNER_PACKAGE." |
| 63 | + sudo chown $DEFAULT_USER:$DEFAULT_USER "$USER_HOME/$RUNNER_NAME/actions-runner/$RUNNER_PACKAGE" || die "Failed to set ownership for $USER_HOME/$RUNNER_NAME/actions-runner/$RUNNER_PACKAGE." |
| 64 | + |
| 65 | + # Verify SHA256 checksum |
| 66 | + actual_checksum=$(sudo -u $DEFAULT_USER sha256sum "$USER_HOME/$RUNNER_NAME/actions-runner/$RUNNER_PACKAGE" | awk '{print $1}') |
| 67 | + if [ "$EXPECTED_CHECKSUM" != "$actual_checksum" ]; then |
| 68 | + die "Checksum verification failed for $USER_HOME/$RUNNER_NAME/actions-runner/$RUNNER_PACKAGE. Aborting." |
| 69 | + fi |
| 70 | + |
| 71 | + # Extract the runner package |
| 72 | + sudo -u $DEFAULT_USER tar xzf "$USER_HOME/$RUNNER_NAME/actions-runner/$RUNNER_PACKAGE" -C "$USER_HOME/$RUNNER_NAME/actions-runner" || die "Failed to extract $USER_HOME/$RUNNER_NAME/actions-runner/$RUNNER_PACKAGE." |
| 73 | + sudo chown -R $DEFAULT_USER:$DEFAULT_USER "$USER_HOME/$RUNNER_NAME/actions-runner" || die "Failed to set ownership for $USER_HOME/$RUNNER_NAME/actions-runner." |
| 74 | +} |
| 75 | + |
| 76 | +# Function to configure and start the runner |
| 77 | +configure_and_start_runner() { |
| 78 | + local RUNNER_NAME="$1" |
| 79 | + |
| 80 | + sudo -u $DEFAULT_USER -i <<EOF |
| 81 | + cd "$USER_HOME/$RUNNER_NAME/actions-runner" || exit 1 |
| 82 | +
|
| 83 | + ./config.sh --url "${CONFIG_URL}" \ |
| 84 | + --token "${CONFIG_TOKEN}" \ |
| 85 | + --name "$RUNNER_NAME" \ |
| 86 | + --runnergroup "Default" \ |
| 87 | + --work "_work" \ |
| 88 | + --labels "self-hosted,Linux,X64,$RUNNER_NAME" \ |
| 89 | + --unattended \ |
| 90 | + --replace || { echo "Failed to configure GitHub Actions runner"; exit 1; } |
| 91 | +
|
| 92 | + nohup ./run.sh > runner.log 2>&1 & |
| 93 | + if [ \$? -ne 0 ]; then |
| 94 | + echo "Failed to start GitHub Actions runner $RUNNER_NAME" |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | +
|
| 98 | + echo "GitHub Actions runner setup for $RUNNER_NAME completed successfully." |
| 99 | + echo "The runner is running in the background. Check runner.log for output." |
| 100 | +EOF |
| 101 | +} |
| 102 | + |
| 103 | +# Main script |
| 104 | +main() { |
| 105 | + local RUNNER_NAME="runner" |
| 106 | + # Install required packages if not already installed |
| 107 | + command_exists curl || install_packages curl |
| 108 | + |
| 109 | + # Ensure default user exists and has necessary permissions (no longer creating new users) |
| 110 | + sudo useradd -m -s /bin/bash $DEFAULT_USER 2>/dev/null || true |
| 111 | + echo "$DEFAULT_USER:$USER_PASSWORD" | sudo chpasswd || die "Failed to set password for $DEFAULT_USER. Aborting." |
| 112 | + echo "$DEFAULT_USER ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$DEFAULT_USER >/dev/null |
| 113 | + sudo chmod 0440 /etc/sudoers.d/$DEFAULT_USER |
| 114 | + |
| 115 | + setup_runner_directory "$RUNNER_NAME" |
| 116 | + download_and_extract_runner "$RUNNER_NAME" |
| 117 | + configure_and_start_runner "$RUNNER_NAME" |
| 118 | + |
| 119 | + echo "All GitHub Actions runners setup completed successfully." |
| 120 | +} |
| 121 | + |
| 122 | +# Execute main script |
| 123 | +main |
0 commit comments