Skip to content

Commit 2bcaee9

Browse files
Copilotarii
andcommitted
Add resource limits and automated systemd service installation
- Add RUNNER_MEMORY_LIMIT and RUNNER_CPU_LIMIT to .env.runner.example - Update deploy-runner.sh to apply resource limits when configured - Create install-service.sh for automated systemd service installation - Script auto-detects repository root and updates service paths - Update README with resource limits configuration and automated install - Address feedback: automate path replacement and add resource limits Co-authored-by: arii <342438+arii@users.noreply.github.com>
1 parent 42bc995 commit 2bcaee9

4 files changed

Lines changed: 127 additions & 10 deletions

File tree

deploy/runner/.env.runner.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ RUNNER_TOKEN=
1717
# Must have 'repo' scope (or 'public_repo' for public repositories)
1818
# Generate at: https://github.com/settings/tokens
1919
GITHUB_PAT=
20+
21+
# Optional: Resource limits for the Docker container
22+
# Recommended for production deployments to prevent resource exhaustion
23+
# Memory limit (e.g., 2g for 2 gigabytes, 512m for 512 megabytes)
24+
RUNNER_MEMORY_LIMIT=
25+
26+
# CPU limit (e.g., 2 for 2 CPUs, 0.5 for half a CPU)
27+
RUNNER_CPU_LIMIT=

deploy/runner/README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Edit `.env.runner` and set:
3333
- `REPO_URL`: The GitHub repository URL (required, default: `https://github.com/arii/hrm`)
3434
- `RUNNER_TOKEN`: Runner registration token (optional - auto-generated if not provided)
3535
- `GITHUB_PAT`: GitHub Personal Access Token (optional - only needed if `gh` CLI is not authenticated)
36+
- `RUNNER_MEMORY_LIMIT`: Memory limit for container (optional, e.g., `2g` for 2GB, recommended for production)
37+
- `RUNNER_CPU_LIMIT`: CPU limit for container (optional, e.g., `2` for 2 CPUs, recommended for production)
3638

3739
#### Automatic Token Generation
3840

@@ -89,7 +91,30 @@ Verify in GitHub:
8991

9092
For automatic startup on system boot and better management, set up the systemd service.
9193

92-
### Installation
94+
### Automated Installation (Recommended)
95+
96+
Use the provided installation script to automatically configure paths:
97+
98+
```bash
99+
sudo ./install-service.sh
100+
```
101+
102+
This script will:
103+
1. Detect the repository root path automatically
104+
2. Update the service file with correct paths
105+
3. Install the service to `/etc/systemd/system/`
106+
4. Reload systemd daemon
107+
108+
After installation:
109+
110+
```bash
111+
sudo systemctl enable hrm-runner.service
112+
sudo systemctl start hrm-runner.service
113+
```
114+
115+
### Manual Installation
116+
117+
If you prefer manual installation:
93118

94119
1. Edit `hrm-runner.service` and replace `/path/to/hrm` with the actual path to your repository:
95120

@@ -288,7 +313,7 @@ cd /home/runner/actions-runner
288313
### Important Security Limitations
289314

290315
- **No Docker-in-Docker by default**: The container does not mount the Docker socket, so jobs that need to build Docker images will fail. If needed, add `-v /var/run/docker.sock:/var/run/docker.sock` to the docker run command, but be aware this grants significant privileges.
291-
- **Resource limits**: Consider adding `--memory` and `--cpus` flags to the docker run command to prevent resource exhaustion.
316+
- **Resource limits**: Configure `RUNNER_MEMORY_LIMIT` and `RUNNER_CPU_LIMIT` in `.env.runner` to prevent resource exhaustion. Recommended for production deployments (e.g., `RUNNER_MEMORY_LIMIT=2g` and `RUNNER_CPU_LIMIT=2`).
292317
- **Sudo access**: The runner user has sudo access (with password requirement). For maximum security, you may want to remove sudo access entirely if not needed by your workflows.
293318
- **GitHub PAT Storage**: If using `GITHUB_PAT` for automatic token generation, ensure `.env.runner` file permissions are restrictive (`chmod 600 .env.runner`).
294319

@@ -325,9 +350,10 @@ cd /home/runner/actions-runner
325350
| ------------------------ | ------------------------------------------------- |
326351
| `Dockerfile.runner` | Docker image definition for the runner |
327352
| `entrypoint.sh` | Container entrypoint - configures and starts runner |
328-
| `deploy-runner.sh` | Deployment script - builds and runs the container, auto-generates token if needed |
329-
| `.env.runner.example` | Configuration template with REPO_URL, RUNNER_TOKEN, and GITHUB_PAT |
330-
| `hrm-runner.service` | Systemd service definition |
353+
| `deploy-runner.sh` | Deployment script - builds and runs the container, auto-generates token if needed, applies resource limits |
354+
| `install-service.sh` | Automated systemd service installation with path auto-detection |
355+
| `.env.runner.example` | Configuration template with REPO_URL, RUNNER_TOKEN, GITHUB_PAT, and resource limits |
356+
| `hrm-runner.service` | Systemd service definition (template) |
331357
| `README.md` | This documentation |
332358

333359
## Related Documentation

deploy/runner/deploy-runner.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,27 @@ fi
142142

143143
# Run the container
144144
echo "Starting GitHub Actions Runner container..."
145-
docker run -d --restart unless-stopped \
146-
--name hrm-runner \
147-
-e REPO_URL="$REPO_URL" \
148-
-e RUNNER_TOKEN="$RUNNER_TOKEN" \
149-
hrm-actions-runner
145+
146+
# Build docker run command with optional resource limits
147+
DOCKER_RUN_CMD="docker run -d --restart unless-stopped --name hrm-runner"
148+
149+
# Add memory limit if specified
150+
if [ -n "$RUNNER_MEMORY_LIMIT" ]; then
151+
echo "Setting memory limit: $RUNNER_MEMORY_LIMIT"
152+
DOCKER_RUN_CMD="$DOCKER_RUN_CMD --memory=$RUNNER_MEMORY_LIMIT"
153+
fi
154+
155+
# Add CPU limit if specified
156+
if [ -n "$RUNNER_CPU_LIMIT" ]; then
157+
echo "Setting CPU limit: $RUNNER_CPU_LIMIT"
158+
DOCKER_RUN_CMD="$DOCKER_RUN_CMD --cpus=$RUNNER_CPU_LIMIT"
159+
fi
160+
161+
# Add environment variables and image
162+
DOCKER_RUN_CMD="$DOCKER_RUN_CMD -e REPO_URL=\"$REPO_URL\" -e RUNNER_TOKEN=\"$RUNNER_TOKEN\" hrm-actions-runner"
163+
164+
# Execute the command
165+
eval $DOCKER_RUN_CMD
150166

151167
echo ""
152168
echo "✓ GitHub Actions Runner deployed successfully!"

deploy/runner/install-service.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
3+
# HRM GitHub Actions Runner - Systemd Service Installation Script
4+
# This script installs the hrm-runner.service systemd unit
5+
6+
set -e
7+
8+
# Change to the directory where this script is located
9+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
cd "$SCRIPT_DIR"
11+
12+
# Detect the repository root (parent of deploy/runner)
13+
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
14+
15+
echo "HRM GitHub Actions Runner - Systemd Service Installation"
16+
echo "========================================================="
17+
echo ""
18+
echo "Detected repository root: $REPO_ROOT"
19+
echo "Service working directory: $SCRIPT_DIR"
20+
echo ""
21+
22+
# Check if running with appropriate permissions
23+
if [ "$EUID" -ne 0 ]; then
24+
echo "This script requires root privileges to install systemd services."
25+
echo "Please run with sudo:"
26+
echo " sudo ./install-service.sh"
27+
exit 1
28+
fi
29+
30+
# Create a temporary service file with updated paths
31+
SERVICE_FILE="/tmp/hrm-runner.service.tmp"
32+
cp hrm-runner.service "$SERVICE_FILE"
33+
34+
# Replace placeholder paths with actual paths
35+
sed -i "s|/path/to/hrm|$REPO_ROOT|g" "$SERVICE_FILE"
36+
37+
echo "Generated service file:"
38+
echo "----------------------"
39+
cat "$SERVICE_FILE"
40+
echo "----------------------"
41+
echo ""
42+
43+
# Confirm installation
44+
read -p "Install this service to /etc/systemd/system/hrm-runner.service? [y/N] " -n 1 -r
45+
echo
46+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
47+
echo "Installation cancelled."
48+
rm "$SERVICE_FILE"
49+
exit 0
50+
fi
51+
52+
# Copy to systemd directory
53+
cp "$SERVICE_FILE" /etc/systemd/system/hrm-runner.service
54+
rm "$SERVICE_FILE"
55+
56+
# Reload systemd
57+
echo "Reloading systemd daemon..."
58+
systemctl daemon-reload
59+
60+
echo ""
61+
echo "✓ Service installed successfully!"
62+
echo ""
63+
echo "Next steps:"
64+
echo " 1. Enable service: sudo systemctl enable hrm-runner.service"
65+
echo " 2. Start service: sudo systemctl start hrm-runner.service"
66+
echo " 3. Check status: sudo systemctl status hrm-runner.service"
67+
echo " 4. View logs: sudo journalctl -u hrm-runner.service -f"

0 commit comments

Comments
 (0)