|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Tag and push a GitHub release from the version in package.json. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python scripts/release.py # dry-run (shows what would happen) |
| 7 | + python scripts/release.py --push # create tag and push to trigger release workflow |
| 8 | +
|
| 9 | +What it does: |
| 10 | + 1. Reads the version from package.json |
| 11 | + 2. Checks that the git tag doesn't already exist on GitHub |
| 12 | + 3. Creates a git tag v{version} and pushes it to origin |
| 13 | + 4. Updates the floating major tag (e.g. v1) to point to the new release |
| 14 | +
|
| 15 | +Requires: |
| 16 | + - python3 (no external dependencies) |
| 17 | + - git on PATH |
| 18 | +""" |
| 19 | + |
| 20 | +import json |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 26 | +PACKAGE_JSON = REPO_ROOT / "package.json" |
| 27 | + |
| 28 | + |
| 29 | +def get_version() -> str: |
| 30 | + data = json.loads(PACKAGE_JSON.read_text()) |
| 31 | + version = data.get("version") |
| 32 | + if not version: |
| 33 | + print("ERROR: No version found in package.json", file=sys.stderr) |
| 34 | + sys.exit(1) |
| 35 | + return version |
| 36 | + |
| 37 | + |
| 38 | +def git(*args: str) -> str: |
| 39 | + result = subprocess.run( |
| 40 | + ["git", *args], |
| 41 | + capture_output=True, |
| 42 | + text=True, |
| 43 | + cwd=REPO_ROOT, |
| 44 | + ) |
| 45 | + if result.returncode != 0: |
| 46 | + print(f"ERROR: git {' '.join(args)} failed:\n{result.stderr.strip()}", file=sys.stderr) |
| 47 | + sys.exit(1) |
| 48 | + return result.stdout.strip() |
| 49 | + |
| 50 | + |
| 51 | +def local_tag_exists(tag: str) -> bool: |
| 52 | + result = subprocess.run( |
| 53 | + ["git", "tag", "-l", tag], |
| 54 | + capture_output=True, |
| 55 | + text=True, |
| 56 | + cwd=REPO_ROOT, |
| 57 | + ) |
| 58 | + return bool(result.stdout.strip()) |
| 59 | + |
| 60 | + |
| 61 | +def remote_tag_exists(tag: str) -> bool: |
| 62 | + result = subprocess.run( |
| 63 | + ["git", "ls-remote", "--tags", "origin", f"refs/tags/{tag}"], |
| 64 | + capture_output=True, |
| 65 | + text=True, |
| 66 | + cwd=REPO_ROOT, |
| 67 | + ) |
| 68 | + return bool(result.stdout.strip()) |
| 69 | + |
| 70 | + |
| 71 | +def delete_local_tag(tag: str) -> None: |
| 72 | + subprocess.run( |
| 73 | + ["git", "tag", "-d", tag], |
| 74 | + capture_output=True, |
| 75 | + cwd=REPO_ROOT, |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def main() -> None: |
| 80 | + push = "--push" in sys.argv |
| 81 | + |
| 82 | + version = get_version() |
| 83 | + tag = f"v{version}" |
| 84 | + major_tag = tag.split(".")[0] # e.g. "v1" |
| 85 | + print(f"package.json version: {version}") |
| 86 | + print(f"Git tag: {tag}") |
| 87 | + print(f"Floating major tag: {major_tag}") |
| 88 | + |
| 89 | + # GitHub is the source of truth for tags. |
| 90 | + if remote_tag_exists(tag): |
| 91 | + print(f"\nERROR: Git tag {tag} already exists on origin.", file=sys.stderr) |
| 92 | + print("Bump the version in package.json before releasing.", file=sys.stderr) |
| 93 | + sys.exit(1) |
| 94 | + |
| 95 | + if local_tag_exists(tag): |
| 96 | + print(f"Local tag {tag} exists but not on origin — deleting stale local tag.") |
| 97 | + delete_local_tag(tag) |
| 98 | + |
| 99 | + # Check we're on a clean working tree |
| 100 | + status = git("status", "--porcelain") |
| 101 | + if status: |
| 102 | + print(f"\nERROR: Working tree is not clean:\n{status}", file=sys.stderr) |
| 103 | + print("Commit or stash changes before releasing.", file=sys.stderr) |
| 104 | + sys.exit(1) |
| 105 | + |
| 106 | + if not push: |
| 107 | + print(f"\nDry run: would create and push tag {tag}") |
| 108 | + print(f" would update floating tag {major_tag} -> {tag}") |
| 109 | + print("Run with --push to execute.") |
| 110 | + return |
| 111 | + |
| 112 | + # Create and push the version tag |
| 113 | + print(f"\nCreating tag {tag}...", flush=True) |
| 114 | + result = subprocess.run( |
| 115 | + ["git", "tag", "-a", tag, "-m", f"release: {version}"], |
| 116 | + cwd=REPO_ROOT, |
| 117 | + ) |
| 118 | + if result.returncode != 0: |
| 119 | + print(f"\nERROR: git tag failed (exit {result.returncode})", file=sys.stderr) |
| 120 | + sys.exit(1) |
| 121 | + |
| 122 | + print(f"Pushing tag {tag} to origin...", flush=True) |
| 123 | + result = subprocess.run( |
| 124 | + ["git", "push", "origin", tag], |
| 125 | + cwd=REPO_ROOT, |
| 126 | + ) |
| 127 | + if result.returncode != 0: |
| 128 | + print(f"\nERROR: git push failed (exit {result.returncode})", file=sys.stderr) |
| 129 | + sys.exit(1) |
| 130 | + |
| 131 | + # Update floating major tag (e.g. v1 -> v1.0.4) |
| 132 | + print(f"Updating floating tag {major_tag} -> {tag}...", flush=True) |
| 133 | + subprocess.run(["git", "tag", "-f", major_tag, tag], cwd=REPO_ROOT) |
| 134 | + result = subprocess.run( |
| 135 | + ["git", "push", "origin", major_tag, "--force"], |
| 136 | + cwd=REPO_ROOT, |
| 137 | + ) |
| 138 | + if result.returncode != 0: |
| 139 | + print(f"\nWARNING: Failed to push floating tag {major_tag}", file=sys.stderr) |
| 140 | + |
| 141 | + print(f"\nDone. Release workflow will run at:") |
| 142 | + print(f" https://github.com/auths-dev/auths-verify-github-action/actions") |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments