blumeops/mise-tasks/container-tag-and-release
Erich Blume 2623c1c6fe
Some checks failed
Build Container / build (push) Successful in 20s
Build Container (Nix) / build (push) Failing after 26s
Use separate registry tags for nix vs dockerfile builds
Nix builds push to :v<version>-nix so both variants coexist in the
registry instead of overwriting each other.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 07:59:05 -08:00

155 lines
5 KiB
Text
Executable file

#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = ["typer>=0.15.0"]
# ///
#MISE description="Release a container image by creating a git tag"
#USAGE arg "<container>" help="Container name (directory under containers/)"
#USAGE arg "<version>" help="Version in vX.Y.Z format"
#USAGE flag "--nix" help="Release only the nix variant"
#USAGE flag "--dockerfile" help="Release only the dockerfile variant"
#USAGE flag "--dry-run" help="Show what would be done without creating tags"
"""Release a container image by creating git tag(s) that trigger CI builds.
When a container has both a Dockerfile and default.nix, both tags are created
by default. Use --nix or --dockerfile to release only one variant.
Tag conventions:
<container>-v<version> -> build-container.yaml -> :v<version>
<container>-nix-v<version> -> build-container-nix.yaml -> :v<version>-nix
"""
import re
import subprocess
import sys
from pathlib import Path
import typer
REGISTRY = "registry.ops.eblu.me"
FORGE_ACTIONS = "https://forge.ops.eblu.me/eblume/blumeops/actions"
app = typer.Typer(add_completion=False)
def git(*args: str) -> str:
result = subprocess.run(
["git", *args], capture_output=True, text=True, check=True
)
return result.stdout.strip()
def git_tag_exists(tag: str) -> bool:
result = subprocess.run(
["git", "rev-parse", tag], capture_output=True, text=True
)
return result.returncode == 0
def list_containers() -> None:
typer.echo("Available containers:")
for d in sorted(Path("containers").iterdir()):
if not d.is_dir():
continue
types = []
if (d / "Dockerfile").exists():
types.append("dockerfile")
if (d / "default.nix").exists():
types.append("nix")
if types:
typer.echo(f" - {d.name} ({', '.join(types)})")
def create_and_push_tag(tag: str, image: str, image_tag: str, dry_run: bool) -> bool:
"""Create a git tag and push it. Returns True on success."""
if git_tag_exists(tag):
typer.echo(f" Skip: Tag '{tag}' already exists")
return False
if dry_run:
typer.echo(f" [dry-run] Would create and push: {tag} -> {REGISTRY}/{image}:{image_tag}")
else:
git("tag", tag)
git("push", "origin", tag)
typer.echo(f" {tag} -> {REGISTRY}/{image}:{image_tag}")
return True
@app.command()
def main(
container: str = typer.Argument(help="Container name (directory under containers/)"),
version: str = typer.Argument(help="Version in vX.Y.Z format"),
nix: bool = typer.Option(False, "--nix", help="Release only the nix variant"),
dockerfile: bool = typer.Option(False, "--dockerfile", help="Release only the dockerfile variant"),
dry_run: bool = typer.Option(False, "--dry-run", help="Show what would be done without creating tags"),
) -> None:
"""Release a container image by creating git tag(s) that trigger CI builds."""
if nix and dockerfile:
typer.echo("Error: --nix and --dockerfile are mutually exclusive")
raise typer.Exit(1)
if not re.match(r"^v\d+\.\d+\.\d+$", version):
typer.echo("Error: Version must be in format vX.Y.Z (e.g. v1.0.0)")
raise typer.Exit(1)
container_dir = Path("containers") / container
has_dockerfile = (container_dir / "Dockerfile").exists()
has_nix = (container_dir / "default.nix").exists()
if not has_dockerfile and not has_nix:
typer.echo(f"Error: No Dockerfile or default.nix found in '{container_dir}'")
typer.echo()
list_containers()
raise typer.Exit(1)
if nix and not has_nix:
typer.echo(f"Error: --nix specified but no default.nix in '{container_dir}'")
raise typer.Exit(1)
if dockerfile and not has_dockerfile:
typer.echo(f"Error: --dockerfile specified but no Dockerfile in '{container_dir}'")
raise typer.Exit(1)
# Decide which builds to release
builds: list[str] = []
if nix:
builds = ["nix"]
elif dockerfile:
builds = ["dockerfile"]
else:
if has_dockerfile:
builds.append("dockerfile")
if has_nix:
builds.append("nix")
image = f"blumeops/{container}"
if dry_run:
typer.echo("[dry-run mode]")
typer.echo(f"Container: {container}")
typer.echo(f"Image: {REGISTRY}/{image}")
typer.echo(f"Version: {version}")
typer.echo(f"Builds: {', '.join(builds)}")
typer.echo()
# Create and push tags
tags_created = 0
for build in builds:
if build == "nix":
tag = f"{container}-nix-{version}"
image_tag = f"{version}-nix"
else:
tag = f"{container}-{version}"
image_tag = version
if create_and_push_tag(tag, image, image_tag, dry_run):
tags_created += 1
if tags_created == 0:
typer.echo()
typer.echo("No tags created (all already existed)")
raise typer.Exit(1)
typer.echo()
typer.echo(f"Monitor builds at: {FORGE_ACTIONS}")
if __name__ == "__main__":
app()