|
1 | 1 | #!/bin/bash
|
2 | 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 | 3 | DEFAULT_USER="githubrunner"
|
9 | 4 | USER_HOME="/home/$DEFAULT_USER"
|
10 | 5 | USER_PASSWORD="password"
|
@@ -73,38 +68,66 @@ download_and_extract_runner() {
|
73 | 68 | 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 | 69 | }
|
75 | 70 |
|
| 71 | +# Function to fetch GitHub Actions runner registration token |
| 72 | +fetch_runner_token() { |
| 73 | + local response |
| 74 | + response=$(curl -s -X POST -H "Authorization: token ${GITHUB_PAT}" "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/actions/runners/registration-token") |
| 75 | + echo $(echo "$response" | jq -r .token) |
| 76 | +} |
| 77 | + |
76 | 78 | # Function to configure and start the runner
|
77 | 79 | configure_and_start_runner() {
|
78 | 80 | local RUNNER_NAME="$1"
|
| 81 | + local RUNNER_TOKEN |
| 82 | + RUNNER_TOKEN=$(fetch_runner_token) || die "Failed to fetch GitHub Actions runner registration token." |
79 | 83 |
|
80 | 84 | sudo -u $DEFAULT_USER -i <<EOF
|
81 | 85 | cd "$USER_HOME/$RUNNER_NAME/actions-runner" || exit 1
|
82 | 86 |
|
83 |
| - ./config.sh --url "${CONFIG_URL}" \ |
84 |
| - --token "${CONFIG_TOKEN}" \ |
| 87 | + ./config.sh --url "https://github.com/${GITHUB_ORG}/${GITHUB_REPO}" \ |
| 88 | + --token "$RUNNER_TOKEN" \ |
85 | 89 | --name "$RUNNER_NAME" \
|
86 | 90 | --runnergroup "Default" \
|
87 | 91 | --work "_work" \
|
88 | 92 | --labels "self-hosted,Linux,X64,$RUNNER_NAME" \
|
89 | 93 | --unattended \
|
90 | 94 | --replace || { echo "Failed to configure GitHub Actions runner"; exit 1; }
|
91 | 95 |
|
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 | 96 | echo "GitHub Actions runner setup for $RUNNER_NAME completed successfully."
|
99 | 97 | echo "The runner is running in the background. Check runner.log for output."
|
100 | 98 | EOF
|
| 99 | + |
| 100 | + # Create systemd service |
| 101 | + sudo tee /etc/systemd/system/github-runner.service >/dev/null <<EOL |
| 102 | +[Unit] |
| 103 | +Description=GitHub Actions Runner |
| 104 | +After=network.target |
| 105 | +
|
| 106 | +[Service] |
| 107 | +User=$DEFAULT_USER |
| 108 | +WorkingDirectory=$USER_HOME/$RUNNER_NAME/actions-runner |
| 109 | +ExecStart=$USER_HOME/$RUNNER_NAME/actions-runner/run.sh |
| 110 | +Restart=always |
| 111 | +RestartSec=10 |
| 112 | +
|
| 113 | +[Install] |
| 114 | +WantedBy=multi-user.target |
| 115 | +EOL |
| 116 | + |
| 117 | + # Reload systemd and start the service |
| 118 | + sudo systemctl daemon-reload |
| 119 | + sudo systemctl enable github-runner |
| 120 | + sudo systemctl start github-runner |
| 121 | + |
| 122 | + echo "Systemd service for GitHub Actions runner $RUNNER_NAME created and started." |
101 | 123 | }
|
102 | 124 |
|
103 | 125 | # Main script
|
104 | 126 | main() {
|
105 |
| - local RUNNER_NAME="runner" |
| 127 | + local RUNNER_NAME="deck" |
106 | 128 | # Install required packages if not already installed
|
107 | 129 | command_exists curl || install_packages curl
|
| 130 | + install_packages curl jq |
108 | 131 |
|
109 | 132 | # Ensure default user exists and has necessary permissions (no longer creating new users)
|
110 | 133 | sudo useradd -m -s /bin/bash $DEFAULT_USER 2>/dev/null || true
|
|
0 commit comments