83 lines
3.3 KiB
Python
Executable File
83 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Sync step of the `Release` workflow: merge production into develop.
|
|
|
|
Runs after a release, so `develop` always contains what is live. Features are made from develop, so
|
|
it must be on the latest production before the next release is started.
|
|
`develop` takes direct pushes (only `production` and the release branches are protected),
|
|
so this is a plain merge and push: fast-forward when possible, otherwise a
|
|
merge commit `chore: sync production into develop`.
|
|
|
|
A conflict fails the step on purpose. The release itself is done by then; the
|
|
red build is the signal that `develop` holds something production does not
|
|
and needs a merge by hand.
|
|
|
|
Environment: GT_URL, GT_OWNER, GT_REPO, GT_TOKEN (an access token
|
|
that may push to develop), COMMIT_HASH (the commit this job released; defaults
|
|
to the tip of production). SYNC_REMOTE overrides the clone URL (local testing).
|
|
|
|
Standard library + git only.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import urllib.request
|
|
|
|
URL = os.environ.get("GT_URL", "").rstrip("/")
|
|
OWNER = os.environ.get("GT_OWNER", "")
|
|
REPO = os.environ.get("GT_REPO", "")
|
|
TOKEN = os.environ.get("GT_TOKEN", "")
|
|
REMOTE = os.environ.get("SYNC_REMOTE", "")
|
|
MESSAGE = "chore: sync production into develop"
|
|
|
|
|
|
def git(repo, *args, check=True):
|
|
result = subprocess.run(["git", "-C", repo, *args], capture_output=True, text=True)
|
|
if check and result.returncode != 0:
|
|
raise SystemExit(f"git {' '.join(args[:2])} failed: {result.stderr.strip()}")
|
|
return result
|
|
|
|
|
|
def main():
|
|
user = os.environ.get("GT_BOT_USER") or "sync"
|
|
remote = REMOTE
|
|
if not remote:
|
|
if not (URL and OWNER and REPO and TOKEN):
|
|
raise SystemExit("GT_URL, GT_OWNER, GT_REPO and GT_TOKEN are required")
|
|
remote = f"https://{user}:{TOKEN}@{URL.split('://', 1)[1]}/{OWNER}/{REPO}.git"
|
|
|
|
repo = os.path.join(tempfile.mkdtemp(), "repo")
|
|
result = subprocess.run(["git", "clone", "--quiet", "--branch", "develop", remote, repo],
|
|
capture_output=True, text=True)
|
|
if result.returncode != 0:
|
|
raise SystemExit(f"Could not clone develop: {result.stderr.strip()}")
|
|
git(repo, "config", "user.name", user)
|
|
git(repo, "config", "user.email", f"{user}@iriks-it.nl")
|
|
|
|
commit = os.environ.get("COMMIT_HASH", "").strip()
|
|
source = commit if re.fullmatch(r"[0-9a-f]{40}", commit) else "origin/production"
|
|
short = git(repo, "rev-parse", "--short", source).stdout.strip()
|
|
|
|
if git(repo, "merge-base", "--is-ancestor", source, "HEAD", check=False).returncode == 0:
|
|
print(f"develop already contains production ({short}) — nothing to do.")
|
|
return
|
|
|
|
merge = git(repo, "merge", "--no-edit", "-m", MESSAGE, source, check=False)
|
|
if merge.returncode != 0:
|
|
conflicts = git(repo, "diff", "--name-only", "--diff-filter=U", check=False).stdout.strip()
|
|
raise SystemExit(
|
|
f"production ({short}) does not merge cleanly into develop — merge it by hand.\n"
|
|
f"Conflicting files:\n{conflicts or merge.stdout.strip() or merge.stderr.strip()}"
|
|
)
|
|
|
|
push = git(repo, "push", "--quiet", "origin", "HEAD:refs/heads/develop", check=False)
|
|
if push.returncode != 0:
|
|
raise SystemExit(f"Could not push develop: {push.stderr.strip()}")
|
|
print(f"develop now contains production ({short}).")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|