|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Run one CI step's `run:` block locally, verbatim. |
| 3 | +
|
| 4 | +Verbatim is the point. Every CI failure this project has had came from checking |
| 5 | +a hand-typed variant of a step rather than the step itself: a flag passed |
| 6 | +locally but not in CI, a value assumed to be set by an earlier step, a pipeline |
| 7 | +that behaves differently under `pipefail`. |
| 8 | +
|
| 9 | + .github/scripts/run-step.py # list the steps |
| 10 | + .github/scripts/run-step.py "the forge command works" |
| 11 | + .github/scripts/run-step.py -x "<name>" # trace each command |
| 12 | +
|
| 13 | +Needs the stack running (`forge start`). Steps are not independent: some rely on |
| 14 | +fixtures an earlier step creates, so run those first when a step fails on |
| 15 | +missing files. See the Contributing section of the README. |
| 16 | +""" |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | + |
| 20 | +try: |
| 21 | + import yaml |
| 22 | +except ImportError: |
| 23 | + raise SystemExit( |
| 24 | + "This needs PyYAML to read the workflow file.\n" |
| 25 | + " Debian/Ubuntu: sudo apt install python3-yaml\n" |
| 26 | + " Arch: sudo pacman -S python-yaml\n" |
| 27 | + " Any: pip install --user pyyaml" |
| 28 | + ) |
| 29 | + |
| 30 | +WORKFLOW = ".github/workflows/ci.yml" |
| 31 | + |
| 32 | + |
| 33 | +def steps(): |
| 34 | + with open(WORKFLOW, encoding="utf-8") as fh: |
| 35 | + doc = yaml.safe_load(fh) |
| 36 | + for job_name, job in doc["jobs"].items(): |
| 37 | + for step in job.get("steps", []): |
| 38 | + if step.get("name") and step.get("run"): |
| 39 | + yield job_name, step["name"], step["run"] |
| 40 | + |
| 41 | + |
| 42 | +def main(): |
| 43 | + args = [a for a in sys.argv[1:]] |
| 44 | + trace = False |
| 45 | + if args and args[0] in ("-x", "--trace"): |
| 46 | + trace = True |
| 47 | + args = args[1:] |
| 48 | + |
| 49 | + found = list(steps()) |
| 50 | + if not args: |
| 51 | + print("Steps that can be run:\n") |
| 52 | + current = None |
| 53 | + for job, name, _ in found: |
| 54 | + if job != current: |
| 55 | + print(" [%s]" % job) |
| 56 | + current = job |
| 57 | + print(" %s" % name) |
| 58 | + print("\nRun one with: %s \"<name>\"" % sys.argv[0]) |
| 59 | + return 0 |
| 60 | + |
| 61 | + want = args[0] |
| 62 | + for _, name, body in found: |
| 63 | + if name == want: |
| 64 | + print("--- running %r verbatim ---\n" % name, flush=True) |
| 65 | + # GitHub runs each step as `bash -e {0}`. |
| 66 | + flags = "-ex" if trace else "-e" |
| 67 | + result = subprocess.run(["bash", flags, "-c", body]) |
| 68 | + print("\n--- exit %d ---" % result.returncode) |
| 69 | + return result.returncode |
| 70 | + |
| 71 | + print("No step named %r. Run without arguments to list them." % want, file=sys.stderr) |
| 72 | + return 2 |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + sys.exit(main()) |
0 commit comments