Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
376 lines (340 loc) · 15.5 KB
/
Copy pathrun.py
File metadata and controls
376 lines (340 loc) · 15.5 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python3
"""
build-sequence.py
Purpose:
- Walk through all version branches in a git repo (named like v2.1, v3.0, v11.0, v30.0, ...)
- Skip blacklisted helper branches
- Order branches numerically
- For each branch:
* checkout branch
* update LABEL org.opencontainers.image.revision="manual-trigger-YYYYMMDD" in Dockerfile
* git add/commit/push
* poll GitHub Actions for the workflow run that corresponds to that branch until completion
* if success -> continue to next branch; if failure -> stop and print failure info
Usage:
./build-sequence.py --repo /path/to/clone --owner mocacinno --repo-name bitcoin_core_docker
Requires:
- git available in PATH
- Python 3.8+
- requests: pip install requests
Environment:
- GITHUB_TOKEN (optional but recommended): token with repo/actions read/write if required.
Notes:
- The script expects the Dockerfile path at the repo root named "Dockerfile". If it's different, change DOCKERFILE_PATH.
- The script uses push to 'origin' remote. Adjust as needed.
"""
import argparse
import datetime
import os
import re
import subprocess
import sys
import time
from typing import List, Tuple, Optional
try:
import requests
except Exception:
print("Missing dependency: requests. Install with: pip install requests")
sys.exit(1)
# -------------------------
# Configuration / defaults
# -------------------------
DOCKERFILE_PATH = "Dockerfile" # relative to repo root
REMOTE_NAME = "origin"
POLL_INTERVAL = 60 # seconds between checks of the GitHub Actions run
GITHUB_API_BASE = "https://api.github.com"
# -------------------------
# Helpers
# -------------------------
def run_cmd(cmd: List[str], cwd: Optional[str] = None, capture_output: bool = False):
"""Run shell command; raise on non-zero exit. Returns stdout if capture_output."""
if capture_output:
proc = subprocess.run(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if proc.returncode != 0:
raise RuntimeError(f"Command {' '.join(cmd)} failed: {proc.stderr.strip()}")
return proc.stdout.strip()
else:
proc = subprocess.run(cmd, cwd=cwd)
if proc.returncode != 0:
raise RuntimeError(f"Command {' '.join(cmd)} failed with code {proc.returncode}")
return None
def ensure_clone(local_path: str, remote_url: str, verbose: bool):
if os.path.isdir(local_path) and os.path.isdir(os.path.join(local_path, ".git")):
if verbose:
print(f"[+] Repo exists at {local_path}, fetching latest refs...")
run_cmd(["git", "fetch", REMOTE_NAME, "--prune"], cwd=local_path)
else:
if verbose:
print(f"[+] Cloning {remote_url} -> {local_path}")
run_cmd(["git", "clone", remote_url, local_path])
def list_branches(local_path: str, verbose: bool) -> List[str]:
"""Return list of remote branches (refs/remotes/origin/branch -> branch)"""
out = run_cmd(["git", "branch", "-r"], cwd=local_path, capture_output=True)
branches = []
for line in out.splitlines():
line = line.strip()
# skip HEAD pointer
if "->" in line:
continue
# Expect origin/branch
if line.startswith(f"{REMOTE_NAME}/"):
branch = line[len(REMOTE_NAME)+1:]
branches.append(branch)
if verbose:
print(f"[+] Found {len(branches)} remote branches")
return sorted(set(branches))
def filter_branches(branches: List[str], blacklist: List[str], verbose: bool) -> List[str]:
filtered = [b for b in branches if b not in blacklist]
if verbose:
removed = set(branches) - set(filtered)
print(f"[+] Blacklist removed {len(removed)} branches: {sorted(list(removed))}")
return filtered
semver_re = re.compile(r"^v([0-9]+)(?:[._]([0-9]+))?(?:[._]([0-9]+))?$")
def branch_to_sort_key(branch: str) -> Tuple:
"""
Convert branch name like v2.1 or v11.0.1 into tuple of ints for correct numeric sorting.
Unknown patterns will be pushed to the end by using large numbers.
"""
m = semver_re.match(branch)
if not m:
# nonstandard branch -> put at end, keep ascii fallback
return (9999, 9999, 9999, branch)
a = int(m.group(1) or 0)
b = int(m.group(2) or 0)
c = int(m.group(3) or 0)
return (a, b, c, branch)
def sort_branches(branches: List[str], verbose: bool) -> List[str]:
sorted_br = sorted(branches, key=branch_to_sort_key)
if verbose:
print("[+] Sorted branches:")
for b in sorted_br:
print(" ", b)
return sorted_br
def update_dockerfile_label(repo_path: str, label_date: str, verbose: bool) -> bool:
"""
Find and replace the LABEL org.opencontainers.image.revision="manual-trigger-YYYYMMDD"
Return True if changed, False if not.
"""
dfpath = os.path.join(repo_path, DOCKERFILE_PATH)
if not os.path.isfile(dfpath):
raise FileNotFoundError(f"Dockerfile not found at {dfpath}")
with open(dfpath, "r", encoding="utf-8") as f:
lines = f.readlines()
changed = False
newlines = []
label_pattern = re.compile(r'^(LABEL\s+org\.opencontainers\.image\.revision\s*=\s*")(manual-trigger-[0-9]{8})(".*)$')
replaced_any = False
for ln in lines:
m = label_pattern.match(ln.strip())
if m:
prefix, old, suffix = m.group(1), m.group(2), m.group(3)
new_label = f'{prefix}manual-trigger-{label_date}{suffix}\n'
newlines.append(new_label)
replaced_any = True
changed = (old != f"manual-trigger-{label_date}")
if verbose:
print(f"[+] Replacing label {old} -> manual-trigger-{label_date}")
else:
newlines.append(ln)
if not replaced_any:
# If the exact label line doesn't exist, append it (helpful for older Dockerfiles)
newline = f'LABEL org.opencontainers.image.revision="manual-trigger-{label_date}"\n'
newlines.append("\n" + newline)
changed = True
if verbose:
print("[+] Label line not found. Appending new LABEL line.")
if changed:
with open(dfpath, "w", encoding="utf-8") as f:
f.writelines(newlines)
return changed
def git_checkout_and_update(repo_path: str, branch: str, label_date: str, verbose: bool, dry_run: bool):
# checkout branch
if verbose:
print(f"[+] Checking out branch {branch}")
run_cmd(["git", "checkout", "--force", branch], cwd=repo_path)
# ensure branch is up-to-date with remote
run_cmd(["git", "reset", "--hard", f"{REMOTE_NAME}/{branch}"], cwd=repo_path)
changed = update_dockerfile_label(repo_path, label_date, verbose)
if not changed:
if verbose:
print("[+] Dockerfile label already up-to-date (no change).")
return False, None # no commit/push necessary
if dry_run:
print("[DRY-RUN] Would commit and push changes for", branch)
return True, None
# git add/commit/push
run_cmd(["git", "add", DOCKERFILE_PATH], cwd=repo_path)
commit_msg = f'ci: manual rebuild trigger {label_date} (automated)'
run_cmd(["git", "commit", "-m", commit_msg], cwd=repo_path)
head_sha = run_cmd(["git", "rev-parse", "HEAD"], cwd=repo_path, capture_output=True)
run_cmd(["git", "push", REMOTE_NAME, f"{branch}"], cwd=repo_path)
if verbose:
print(f"[+] Pushed commit to {branch}")
time.sleep(15)
return True, head_sha
# -------------------------
# GitHub Actions polling
# -------------------------
def get_latest_workflow_run(owner: str, repo: str, branch: str, head_sha: str, token: Optional[str], verbose: bool):
"""
Query GitHub Actions runs for the repo+branch and return the newest run for head_sha or None.
See: GET /repos/{owner}/{repo}/actions/runs
"""
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/actions/runs"
headers = {"Accept": "application/vnd.github+json"}
if token:
headers["Authorization"] = f"Bearer {token}"
params = {"branch": branch, "per_page": 5}
r = requests.get(url, headers=headers, params=params)
if r.status_code != 200:
if verbose:
print(f"[!] GitHub API returned {r.status_code}: {r.text}")
return None
j = r.json()
runs = [run for run in j.get("workflow_runs", []) if run.get("head_sha") == head_sha]
if not runs:
return None
# choose latest by created_at
runs_sorted = sorted(runs, key=lambda x: x.get("created_at", ""), reverse=True)
return runs_sorted[0] # newest
def get_workflow_run(owner: str, repo: str, run_id: int, token: Optional[str], verbose: bool):
"""Return one workflow run by ID, or None when the API request fails."""
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/actions/runs/{run_id}"
headers = {"Accept": "application/vnd.github+json"}
if token:
headers["Authorization"] = f"Bearer {token}"
r = requests.get(url, headers=headers)
if r.status_code != 200:
if verbose:
print(f"[!] GitHub API returned {r.status_code}: {r.text}")
return None
return r.json()
def poll_workflow_until_done(owner: str, repo: str, branch: str, head_sha: str, token: Optional[str], poll_interval: int, verbose: bool):
"""
Find the workflow for head_sha, then poll that exact run until it completes.
Returns dict of run if completed, or raises on failure/timeout.
"""
if verbose:
print(f"[+] Polling GitHub Actions for branch {branch} ...")
run_id = None
while True:
if run_id is None:
run = get_latest_workflow_run(owner, repo, branch, head_sha, token, verbose)
else:
run = get_workflow_run(owner, repo, run_id, token, verbose)
if run is None:
if verbose:
print("[...] Workflow run not available yet; sleeping.")
time.sleep(poll_interval)
continue
current_run_id = run.get("id")
status = run.get("status") # queued, in_progress, completed
conclusion = run.get("conclusion") # success, failure, cancelled, timed_out, etc
html_url = run.get("html_url")
head_branch = run.get("head_branch")
created_at = run.get("created_at")
if run_id is None:
if verbose:
print(f"[+] Detected run id={current_run_id} branch={head_branch} created_at={created_at} status={status} conclusion={conclusion} url={html_url}")
run_id = current_run_id
if status in ("queued", "in_progress"):
if verbose:
print(f"[+] Run {current_run_id} status={status}. Waiting {poll_interval} sec...")
time.sleep(poll_interval)
continue
elif status == "completed":
if conclusion == "success":
if verbose:
print(f"[+] Run {current_run_id} completed successfully: {html_url}")
return run
else:
# failure/cancelled/time_out/etc -> raise to stop the sequence
raise RuntimeError(f"Workflow run {current_run_id} finished with conclusion={conclusion}. See {html_url}")
else:
# unknown status -> wait
if verbose:
print(f"[+] Run {current_run_id} unknown status={status}, sleeping.")
time.sleep(poll_interval)
# -------------------------
# CLI and main loop
# -------------------------
def main():
p = argparse.ArgumentParser(description="Automate sequential rebuild triggers for branches via Dockerfile LABEL update + GitHub Actions polling.")
p.add_argument("--repo", "-r", required=True, help="Path to local clone (will clone if missing).")
p.add_argument("--remote-url", required=True, help="Repo remote URL (e.g. https://github.com/mocacinno/bitcoin_core_docker.git)")
p.add_argument("--owner", required=True, help="GitHub owner/organization (e.g. mocacinno)")
p.add_argument("--repo-name", required=True, help="GitHub repository name (e.g. bitcoin_core_docker)")
p.add_argument("--blacklist", nargs="*", default=["main", "master", "develop", "helpers", "ci", "workflows", "v2.0_SLES16", "v2.1_SLES16", "v2.2_SLES16", "v2.3_SLES16", "v2.4_SLES16", "v2.5_SLES16", "v2.6_SLES16", "v2.7_SLES16", "v2.8_SLES16", "v2.9_SLES16", "v2.7-gui_SLES16"], help="Branches to ignore.")
p.add_argument("--start-from", default=None, help="Start from this branch name (inclusive). Useful to resume.")
p.add_argument("--dry-run", action="store_true", help="Don't commit/push; only show what would happen.")
p.add_argument("--once", action="store_true", help="Do only one branch then exit (useful for debugging).")
p.add_argument("--poll-interval", type=int, default=POLL_INTERVAL, help="Seconds between polling GitHub Actions.")
p.add_argument("--verbose", "-v", action="store_true", help="Verbose output.")
args = p.parse_args()
repo_path = os.path.abspath(args.repo)
remote_url = args.remote_url
owner = args.owner
repo_name = args.repo_name
blacklist = args.blacklist
dry_run = args.dry_run
verbose = args.verbose
poll_interval = args.poll_interval
start_from = args.start_from
token = os.environ.get("GITHUB_TOKEN") # optional but recommended
# Step 0: ensure clone present and fetch
ensure_clone(repo_path, remote_url, verbose)
# Step 1: list branches
branches = list_branches(repo_path, verbose)
branches = filter_branches(branches, blacklist, verbose)
branches = sort_branches(branches, verbose)
# If start_from provided, trim earlier branches
if start_from:
if start_from not in branches:
print(f"[!] start-from branch '{start_from}' not found in branch list. Exiting.")
sys.exit(2)
idx = branches.index(start_from)
branches = branches[idx:]
if not branches:
print("[!] No branches to process after filtering. Exiting.")
sys.exit(0)
# Today's date for label
label_date = datetime.date.today().strftime("%Y%m%d")
# main loop
for branch in branches:
print("\n" + "="*60)
print(f"Processing branch: {branch}")
try:
changed, head_sha = git_checkout_and_update(repo_path, branch, label_date, verbose, dry_run)
except Exception as e:
print(f"[ERROR] Git operation failed for branch {branch}: {e}")
sys.exit(1)
if not changed:
print("[+] No changes to commit (label already up-to-date). Skipping push/poll for this branch.")
if args.once:
print("[+] --once set: exiting after one branch.")
break
else:
# continue to next branch
continue
# After push, poll GitHub Actions for run status
if dry_run:
print("[DRY-RUN] Skipping GitHub polling because --dry-run set.")
if args.once:
break
continue
try:
run = poll_workflow_until_done(owner, repo_name, branch, head_sha, token, poll_interval, verbose)
# If returned, it finished successfully -> continue
except Exception as e:
print(f"[!] Workflow failed for branch {branch}: {e}")
# Optionally, you could create a GitHub issue here if token is provided.
# For now: stop processing and notify via stdout
print("[!] Stopping sequence due to failure. Fix the issue then re-run with --start-from", branch)
sys.exit(3)
print(f"[+] Branch {branch} finished SUCCESS — proceeding to next.")
if args.once:
print("[+] --once set: exiting after one branch.")
break
print("\nAll done (or reached end of branch list).")
if __name__ == "__main__":
main()