feat: Add Docker image build and publish script

This commit is contained in:
Andre Beging
2025-10-07 13:48:11 +02:00
parent 1204f5dcde
commit 8e3f2ee9f5
2 changed files with 73 additions and 1 deletions

2
.gitignore vendored
View File

@@ -10,4 +10,4 @@ __pycache__/
pytestcache/ pytestcache/
.coverage .coverage
htmlcov/ htmlcov/
data/hooks.json data/*

72
publish.py Normal file
View File

@@ -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())