From 8e3f2ee9f51e26b1817dd67b34488a31eae1f654 Mon Sep 17 00:00:00 2001 From: Andre Beging Date: Tue, 7 Oct 2025 13:48:11 +0200 Subject: [PATCH] feat: Add Docker image build and publish script --- .gitignore | 2 +- publish.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 publish.py diff --git a/.gitignore b/.gitignore index 39a29ae..1eae406 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ __pycache__/ pytestcache/ .coverage htmlcov/ -data/hooks.json +data/* \ No newline at end of file diff --git a/publish.py b/publish.py new file mode 100644 index 0000000..9a6fb8b --- /dev/null +++ b/publish.py @@ -0,0 +1,72 @@ +"""Build and push container images for Telegram Message Hook. + +This script builds the repository Docker image with two tags: +- ``latest`` +- a date-based tag ``YYYYMMDD`` + +Both tags are pushed to ``git.beging.de/troogs/{image_name}``. +""" + +from __future__ import annotations + +import argparse +import datetime as dt +import subprocess +import sys +from pathlib import Path +from typing import Sequence + + +def run(command: Sequence[str]) -> None: + """Execute a shell command and raise on failure.""" + print("→", " ".join(command)) + try: + subprocess.run(command, check=True) + except subprocess.CalledProcessError as exc: # pragma: no cover - pass-through + raise SystemExit(exc.returncode) from exc + + +def parse_args(argv: Sequence[str]) -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Build and publish Docker images") + parser.add_argument( + "image_name", + nargs="?", + default=Path.cwd().name.lower().replace(" ", "-"), + help="Base image name (default: current directory name)", + ) + parser.add_argument( + "--context", + default=".", + help="Docker build context directory (default: current directory)", + ) + parser.add_argument( + "--dockerfile", + default=None, + help="Optional path to Dockerfile relative to context.", + ) + return parser.parse_args(argv) + + +def main(argv: Sequence[str] | None = None) -> int: + args = parse_args(argv or sys.argv[1:]) + + date_tag = dt.datetime.utcnow().strftime("%Y%m%d") + registry = f"git.beging.de/troogs/{args.image_name}" + + tags = [f"{registry}:latest", f"{registry}:{date_tag}"] + + build_cmd = ["docker", "build", *sum((['-t', tag] for tag in tags), []), args.context] + if args.dockerfile: + build_cmd.extend(["-f", args.dockerfile]) + + run(build_cmd) + + for tag in tags: + run(["docker", "push", tag]) + + print("✔ Published:", ", ".join(tags)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())