Last active 1 month ago

forgejo runner install script

Revision c891acf10803814cb655aafed6bcfb6e73cd6a65

forgejo-actions-guide-docker.txt Raw
1#!/bin/bash
2# =============================================================================
3# FORGEJO RUNNER - FULL SYSTEM DEPLOYMENT WITH SYSTEMD
4# Supports both DIRECT and DOCKER modes
5# =============================================================================
6
7# --- CONFIGURATION VARIABLES ---
8FORGEJO_URL="http://192.168.2.76:3006" # CHANGE THIS
9REGISTRATION_TOKEN="your-registration-token" # CHANGE THIS
10RUNNER_NAME="selfhosted-runner-01"
11RUNNER_VERSION="latest" # Can be "latest" or specific like "v12.13.2"
12RUNNER_MODE="direct" # "direct" or "docker"
13
14# Direct mode variables - using current user
15RUNNER_HOME="/opt/forgejo-runner"
16
17# Docker mode variables
18COMPOSE_DIR="$HOME/forgejo-runner"
19DIND_IMAGE="docker:dind"
20RUNNER_IMAGE="data.forgejo.org/forgejo/runner"
21# =============================================================================
22
23set -e
24
25echo "============================================================"
26echo "FORGEJO RUNNER DEPLOYMENT"
27echo "============================================================"
28echo "Mode: $RUNNER_MODE"
29echo "Version: $RUNNER_VERSION"
30echo "Instance: $FORGEJO_URL"
31echo ""
32
33# --- STEP 1: Detect system architecture ---
34ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/;s/armv7l/armv7/')
35echo "Detected architecture: $ARCH"
36
37# --- STEP 2: Get actual version if "latest" ---
38if [ "$RUNNER_VERSION" = "latest" ]; then
39 echo "Fetching latest Forgejo Runner version..."
40
41 # Check if jq is installed
42 if ! command -v jq &> /dev/null; then
43 echo "jq not found. Installing jq..."
44 apt-get update && apt-get install -y jq
45 fi
46
47 # Get version using jq (your method)
48 RUNNER_VERSION=$(curl -s https://code.forgejo.org/api/v1/repos/forgejo/runner/releases/latest | jq -r .tag_name)
49
50 # Verify we got a valid version
51 if [ -z "$RUNNER_VERSION" ] || [ "$RUNNER_VERSION" = "null" ]; then
52 echo "Failed to get version from API, trying fallback..."
53 RUNNER_VERSION=$(curl -s https://code.forgejo.org/forgejo/runner/releases | \
54 grep -o 'v[0-9]\+\.[0-9]\+\.[0-9]\+' | head -1)
55 fi
56
57 if [ -z "$RUNNER_VERSION" ]; then
58 echo "ERROR: Failed to fetch latest version"
59 echo "Using fallback version v12.13.2"
60 RUNNER_VERSION="v12.13.2"
61 fi
62
63 echo "Latest version: $RUNNER_VERSION"
64fi
65
66# Remove 'v' prefix for version number
67VERSION_NUM=${RUNNER_VERSION#v}
68echo "Using version: $RUNNER_VERSION (number: $VERSION_NUM)"
69echo ""
70
71# --- STEP 3: Download runner binary (direct mode) ---
72if [ "$RUNNER_MODE" = "direct" ]; then
73 echo "=== DIRECT MODE INSTALLATION ==="
74 echo "Installing runner directly on the system (no Docker required)"
75 echo "Using current user: $USER"
76 echo ""
77
78 # --- Create directory ---
79 echo "Creating runner directory..."
80 sudo mkdir -p "$RUNNER_HOME/bin"
81 sudo chown -R "$USER":"$USER" "$RUNNER_HOME"
82
83 # --- Download runner binary ---
84 echo "Downloading Forgejo runner binary..."
85 DOWNLOAD_URL="https://code.forgejo.org/forgejo/runner/releases/download/$RUNNER_VERSION/forgejo-runner-${VERSION_NUM}-linux-$ARCH"
86
87 echo "Downloading from: $DOWNLOAD_URL"
88
89 # Download with timeout and retry
90 if ! curl -L -m 60 --retry 3 "$DOWNLOAD_URL" -o "$RUNNER_HOME/bin/forgejo-runner"; then
91 echo "ERROR: Failed to download runner binary"
92 echo "Try manual download:"
93 echo " wget $DOWNLOAD_URL"
94 exit 1
95 fi
96
97 chmod +x "$RUNNER_HOME/bin/forgejo-runner"
98 chown -R "$USER":"$USER" "$RUNNER_HOME"
99
100 # Verify binary
101 echo "Verifying binary..."
102 "$RUNNER_HOME/bin/forgejo-runner" --version || echo "Warning: Version check failed"
103
104 # --- Register runner ---
105 echo "Registering runner with Forgejo..."
106 "$RUNNER_HOME/bin/forgejo-runner" register \
107 --token "$REGISTRATION_TOKEN" \
108 --name "$RUNNER_NAME" \
109 --instance "$FORGEJO_URL" \
110 --labels "$RUNNER_NAME:host" \
111 --no-interactive
112
113 # --- Create config.yaml ---
114 echo "Creating configuration..."
115 cat > "$RUNNER_HOME/config.yaml" << EOF
116instance: $FORGEJO_URL
117token: "$REGISTRATION_TOKEN"
118
119log:
120 level: info
121
122runner:
123 file: .runner
124 capacity: 1
125 timeout: 30m
126 shutdown_timeout: 30m
127 insecure: true
128 fetch_timeout: 30s
129 fetch_interval: 60s
130 report_interval: 10s
131 labels:
132 - "$RUNNER_NAME:host"
133
134cache:
135 enabled: true
136 dir: ""
137
138host:
139 workdir_parent: ""
140EOF
141
142 # --- Create systemd service ---
143 echo "Creating systemd service..."
144 sudo tee /etc/systemd/system/forgejo-runner.service > /dev/null << EOF
145[Unit]
146Description=Forgejo Actions Runner
147After=network.target
148
149[Service]
150Type=simple
151User=$USER
152Group=$USER
153WorkingDirectory=$RUNNER_HOME
154ExecStart=$RUNNER_HOME/bin/forgejo-runner daemon --config $RUNNER_HOME/config.yaml
155Restart=always
156RestartSec=10
157StartLimitBurst=3
158StartLimitInterval=60s
159LimitNOFILE=65536
160LimitNPROC=65536
161
162[Install]
163WantedBy=multi-user.target
164EOF
165
166 # --- Enable and start service ---
167 echo "Starting service..."
168 sudo systemctl daemon-reload
169 sudo systemctl enable forgejo-runner.service
170 sudo systemctl start forgejo-runner.service
171
172 echo ""
173 echo "=== DIRECT MODE DEPLOYMENT COMPLETE ==="
174 echo ""
175 echo "Service status:"
176 sudo systemctl status forgejo-runner.service --no-pager
177 echo ""
178 echo "To view logs:"
179 echo " sudo journalctl -u forgejo-runner -f"
180 echo ""
181 echo "Runner location: $RUNNER_HOME"
182
183# =============================================================================
184# DOCKER MODE INSTALLATION
185# =============================================================================
186elif [ "$RUNNER_MODE" = "docker" ]; then
187 echo "=== DOCKER MODE INSTALLATION ==="
188 echo "Installing runner with Docker Compose"
189 echo ""
190
191 # --- Check and install Docker if needed ---
192 echo "Checking Docker..."
193 if ! command -v docker &> /dev/null; then
194 echo "Docker not found. Installing Docker..."
195 curl -fsSL https://get.docker.com -o get-docker.sh
196 sudo sh get-docker.sh
197 sudo usermod -aG docker $USER
198 sudo systemctl enable docker --now
199 rm get-docker.sh
200 echo "Docker installed successfully"
201 else
202 echo "Docker is already installed"
203 fi
204
205 if ! systemctl is-active --quiet docker; then
206 echo "Starting Docker service..."
207 sudo systemctl start docker
208 fi
209 echo "Docker is running"
210
211 # --- Check and install Docker Compose if needed ---
212 echo "Checking Docker Compose..."
213 if ! command -v docker compose &> /dev/null; then
214 echo "Docker Compose not found. Installing..."
215 apt-get update && apt-get install -y docker-compose-plugin
216 else
217 echo "Docker Compose is already installed"
218 fi
219 echo ""
220
221 # --- Create working directory ---
222 echo "Creating working directory..."
223 mkdir -p "$COMPOSE_DIR"/{data,cache}
224 cd "$COMPOSE_DIR"
225
226 # --- Register runner using Docker ---
227 echo "Registering runner with Forgejo..."
228 docker run -it --rm \
229 -v "$(pwd)/data:/data" \
230 -w /data \
231 --user root \
232 "${RUNNER_IMAGE}:${RUNNER_VERSION#v}" \
233 forgejo-runner register \
234 --no-interactive \
235 --token "$REGISTRATION_TOKEN" \
236 --name "$RUNNER_NAME" \
237 --instance "$FORGEJO_URL" \
238 --labels "$RUNNER_NAME:host"
239
240 # --- Generate config.yaml ---
241 echo "Generating configuration..."
242 cat > data/config.yaml << EOF
243instance: $FORGEJO_URL
244token: "$REGISTRATION_TOKEN"
245
246log:
247 level: info
248
249runner:
250 file: .runner
251 capacity: 1
252 timeout: 30m
253 shutdown_timeout: 30m
254 insecure: true
255 fetch_timeout: 30s
256 fetch_interval: 60s
257 report_interval: 10s
258 labels:
259 - "$RUNNER_NAME:host"
260
261cache:
262 enabled: true
263 dir: ""
264
265container:
266 privileged: false
267 valid_volumes:
268 - '**'
269 docker_host: ""
270 force_pull: false
271
272host:
273 workdir_parent: ""
274EOF
275
276 # --- Create docker-compose.yml ---
277 echo "Creating docker-compose.yml..."
278 cat > docker-compose.yml << EOF
279version: '3.8'
280
281services:
282 docker-in-docker:
283 image: $DIND_IMAGE
284 container_name: ${RUNNER_NAME}_dind
285 privileged: true
286 command: ['dockerd', '-H', 'tcp://0.0.0.0:2375', '--tls=false']
287 restart: unless-stopped
288 volumes:
289 - dind-storage:/var/lib/docker
290
291 runner:
292 image: ${RUNNER_IMAGE}:${RUNNER_VERSION#v}
293 container_name: ${RUNNER_NAME}
294 depends_on:
295 docker-in-docker:
296 condition: service_started
297 environment:
298 DOCKER_HOST: tcp://docker-in-docker:2375
299 user: 0:0
300 volumes:
301 - ./data:/data
302 - ./cache:/cache
303 restart: unless-stopped
304 command: /bin/sh -c "
305 apk update && apk add --no-cache nodejs npm &&
306 sleep 5 &&
307 forgejo-runner daemon --config /data/config.yaml
308 "
309
310volumes:
311 dind-storage:
312EOF
313
314 # --- Create systemd service for docker-compose ---
315 echo "Creating systemd service..."
316 sudo tee /etc/systemd/system/forgejo-runner-docker.service > /dev/null << EOF
317[Unit]
318Description=Forgejo Runner (Docker Compose)
319After=docker.service
320Requires=docker.service
321
322[Service]
323Type=oneshot
324RemainAfterExit=yes
325WorkingDirectory=$COMPOSE_DIR
326ExecStart=/usr/bin/docker compose up -d
327ExecStop=/usr/bin/docker compose down
328ExecReload=/usr/bin/docker compose restart
329StandardOutput=journal
330User=$USER
331Group=$USER
332
333[Install]
334WantedBy=multi-user.target
335EOF
336
337 # --- Enable and start service ---
338 echo "Starting service..."
339 sudo systemctl daemon-reload
340 sudo systemctl enable forgejo-runner-docker.service
341 sudo systemctl start forgejo-runner-docker.service
342
343 echo ""
344 echo "=== DOCKER MODE DEPLOYMENT COMPLETE ==="
345 echo ""
346 echo "Service status:"
347 sudo systemctl status forgejo-runner-docker.service --no-pager
348 echo ""
349 echo "To view logs:"
350 echo " docker compose -f $COMPOSE_DIR/docker-compose.yml logs -f runner"
351 echo ""
352 echo "To manage:"
353 echo " cd $COMPOSE_DIR"
354 echo " docker compose up -d # start"
355 echo " docker compose down # stop"
356 echo " docker compose restart # restart"
357 echo ""
358 echo "Runner location: $COMPOSE_DIR"
359
360# =============================================================================
361# INVALID MODE
362# =============================================================================
363else
364 echo "ERROR: Invalid RUNNER_MODE. Use 'direct' or 'docker'"
365 exit 1
366fi
367
368echo ""
369echo "============================================================"
370echo "DEPLOYMENT COMPLETE"
371echo "============================================================"
372echo ""
373echo "Runner should now appear online in Forgejo Admin -> Actions -> Runners"
374echo "URL: $FORGEJO_URL/admin/actions/runners"
375echo ""
376if [ "$RUNNER_MODE" = "direct" ]; then
377 echo "To check runner logs: sudo journalctl -u forgejo-runner -f"
378else
379 echo "To check runner logs: docker compose -f $COMPOSE_DIR/docker-compose.yml logs -f runner"
380fi
381echo "============================================================"