-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap_runner.py
More file actions
98 lines (84 loc) · 4.1 KB
/
bootstrap_runner.py
File metadata and controls
98 lines (84 loc) · 4.1 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
#!/usr/bin/env python3
"""
bootstrap_runner.py – registers and installs one self-hosted runner per repo.
Assumes the host machine is macOS Apple-silicon (arm64).
Edit the OWNER, REPOS list, and PAT (or set GITHUB_PAT in your shell) before running.
"""
import json
import os
import pathlib
import subprocess
import sys
import tarfile
import tempfile
import urllib.request
# ──────────────────────────────────────────────────────────────────────────────
# CONFIGURATION – change these three lines to suit your repos / account
# ──────────────────────────────────────────────────────────────────────────────
PAT: str = os.getenv("GITHUB_PAT") or "<your-github-personal-access-token>"
OWNER: str = "<your-github-account-name>"
REPOS: list[str] = [
"<your-awesome-repo>",
"<your-other-awesome-repo>",
"<another-awesome-repo-of-yours>",
]
# Parent folder that will hold one runner sub-folder per repository
ROOT = pathlib.Path.home() / "gh-runners"
# ──────────────────────────────────────────────────────────────────────────────
def latest_runner_asset_url() -> str:
"""Return the download URL for the latest arm64 macOS runner package."""
api = "https://api.github.com/repos/actions/runner/releases/latest"
req = urllib.request.Request(api, headers={"Accept": "application/vnd.github+json"})
with urllib.request.urlopen(req) as resp:
tag = json.load(resp)["tag_name"] # e.g. "v2.325.0"
version = tag.lstrip("v") # -> "2.325.0"
return (
f"https://github.com/actions/runner/releases/download/{tag}/"
f"actions-runner-osx-arm64-{version}.tar.gz"
)
RUNNER_DL = latest_runner_asset_url()
def gh_post(path: str) -> dict:
"""GitHub REST POST helper that returns parsed JSON."""
req = urllib.request.Request(
f"https://api.github.com/{path}",
headers={
"Authorization": f"Bearer {PAT}",
"Accept": "application/vnd.github+json",
},
method="POST",
)
with urllib.request.urlopen(req) as r:
return json.load(r)
# ──────────────────────────────────────────────────────────────────────────────
# MAIN LOOP – one runner per repository
# ──────────────────────────────────────────────────────────────────────────────
for repo in REPOS:
token = gh_post(f"repos/{OWNER}/{repo}/actions/runners/registration-token")[
"token"
]
work = ROOT / repo
work.mkdir(parents=True, exist_ok=True)
# 1. Download runner binaries once per repo folder
if not (work / "config.sh").exists():
with tempfile.NamedTemporaryFile() as tmp:
print(f"⬇️ Downloading runner for {repo} …")
urllib.request.urlretrieve(RUNNER_DL, tmp.name)
with tarfile.open(tmp.name) as tar:
tar.extractall(work)
# 2. Configure the runner (unattended) and install as a service
subprocess.check_call(
[
"./config.sh",
"--url",
f"https://github.com/{OWNER}/{repo}",
"--token",
token,
"--unattended",
"--labels",
repo.replace("-", "_"), # label = repo_name with underscores
],
cwd=work,
)
subprocess.check_call(["./svc.sh", "install"], cwd=work)
print(f"✓ runner installed for {repo}")
print("\nAll requested runners are registered and installed.")