feat: Add Docker image build and publish script
This commit is contained in:
72
publish.py
Normal file
72
publish.py
Normal 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())
|
||||
Reference in New Issue
Block a user