forked from oNaiPs/proxmox-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlxc_create_github_actions_runner.sh
More file actions
104 lines (86 loc) · 3.27 KB
/
lxc_create_github_actions_runner.sh
File metadata and controls
104 lines (86 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
# This script automates the creation and registration of a Github self-hosted runner within a Proxmox LXC (Linux Container).
# The runner is based on Ubuntu 23.04. Before running the script, ensure you have your GITHUB_TOKEN
# and the OWNERREPO (github owner/repository) available.
set -e
# Variables
GITHUB_RUNNER_URL="https://github.com/actions/runner/releases/download/v2.319.1/actions-runner-linux-x64-2.319.1.tar.gz"
TEMPL_URL="http://download.proxmox.com/images/system/ubuntu-24.04-standard_24.04-1_amd64.tar.zst"
PCTSIZE="20G"
PCT_ARCH="amd64"
PCT_CORES="4"
PCT_MEMORY="4096"
PCT_SWAP="4096"
PCT_STORAGE="local-lvm"
DEFAULT_IP_ADDR="192.168.0.123/24"
DEFAULT_GATEWAY="192.168.0.1"
# Ask for GitHub token and owner/repo if they're not set
if [ -z "$GITHUB_TOKEN" ]; then
read -r -p "Enter github token: " GITHUB_TOKEN
echo
fi
if [ -z "$OWNERREPO" ]; then
read -r -p "Enter github owner/repo: " OWNERREPO
echo
fi
# log function prints text in yellow
log() {
local text="$1"
echo -e "\033[33m$text\033[0m"
}
# Prompt for network details
read -r -e -p "Container Address IP (CIDR format) [$DEFAULT_IP_ADDR]: " input_ip_addr
IP_ADDR=${input_ip_addr:-$DEFAULT_IP_ADDR}
read -r -e -p "Container Gateway IP [$DEFAULT_GATEWAY]: " input_gateway
GATEWAY=${input_gateway:-$DEFAULT_GATEWAY}
# Get filename from the URLs
TEMPL_FILE=$(basename $TEMPL_URL)
GITHUB_RUNNER_FILE=$(basename $GITHUB_RUNNER_URL)
# Get the next available ID from Proxmox
PCTID=$(pvesh get /cluster/nextid)
# Download Ubuntu template
log "-- Downloading $TEMPL_FILE template..."
curl -q -C - -o "$TEMPL_FILE" $TEMPL_URL
# Create LXC container
log "-- Creating LXC container with ID:$PCTID"
pct create "$PCTID" "$TEMPL_FILE" \
-arch $PCT_ARCH \
-ostype ubuntu \
-hostname github-runner-proxmox-$(openssl rand -hex 3) \
-cores $PCT_CORES \
-memory $PCT_MEMORY \
-swap $PCT_SWAP \
-storage $PCT_STORAGE \
-features nesting=1,keyctl=1 \
-net0 name=eth0,bridge=vmbr0,gw="$GATEWAY",ip="$IP_ADDR",type=veth
# Resize the container
log "-- Resizing container to $PCTSIZE"
pct resize "$PCTID" rootfs $PCTSIZE
# Start the container & run updates inside it
log "-- Starting container"
pct start "$PCTID"
sleep 10
log "-- Running updates"
pct exec "$PCTID" -- bash -c "apt update -y && apt install -y git curl zip && passwd -d root"
# Install Docker inside the container
log "-- Installing docker"
pct exec "$PCTID" -- bash -c "curl -qfsSL https://get.docker.com | sh"
# Get runner installation token
log "-- Getting runner installation token"
RES=$(curl -q -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$OWNERREPO/actions/runners/registration-token)
RUNNER_TOKEN=$(echo $RES | grep -o '"token": "[^"]*' | grep -o '[^"]*$')
# Install and start the runner
log "-- Installing runner"
pct exec "$PCTID" -- bash -c "mkdir actions-runner && cd actions-runner &&\
curl -o $GITHUB_RUNNER_FILE -L $GITHUB_RUNNER_URL &&\
tar xzf $GITHUB_RUNNER_FILE &&\
RUNNER_ALLOW_RUNASROOT=1 ./config.sh --unattended --url https://github.com/$OWNERREPO --token $RUNNER_TOKEN &&\
./svc.sh install root &&\
./svc.sh start"
# Delete the downloaded Ubuntu template
rm "$TEMPL_FILE"