"""Compile-time Docker identity and immutable release pins for a source build.""" import importlib.util from pathlib import Path import re import subprocess def module(name, file): spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(file)) result = importlib.util.module_from_spec(spec) spec.loader.exec_module(result) return result def release_args(releases): """Build arguments for the resolved OpenCode digest/version and Pi version.""" return ["--build-arg", "OPENCODE_VERSION=" + releases["opencode"]["version"], "--build-arg", "OPENCODE_DIGEST=" + releases["opencode"]["digest"], "--build-arg", "PI_VERSION=" + releases["pi"]["version"]] def docker_args(directory, runner=subprocess.run): def git(*args): result = runner(["git", *args], cwd=directory, capture_output=True, text=True) if result.returncode: raise ValueError("Cannot identify source checkout; build from Git or supply Docker build args explicitly") return result.stdout.strip() commit = git("rev-parse", "HEAD") if not re.fullmatch(r"[0-9a-f]{40,64}", commit): raise ValueError("Invalid Git revision for Docker build") dirty = bool(git("status", "--porcelain", "--untracked-files=normal")) version = commit[:12] + ("-dirty" if dirty else "") return ["--build-arg", "PIPELINES_VERSION=" + version, "--build-arg", "PIPELINES_COMMIT=" + commit, "--build-arg", "PIPELINES_DIRTY=" + str(dirty).lower()] if __name__ == "__main__": import sys directory = Path(__file__).resolve().parent.parent try: # The runtime stage is FROM the immutable digest, so a standalone build # must resolve the current releases itself rather than fail or fall back. releases = module("resolve_releases", "resolve-releases.py").resolve() subprocess.run(["docker", "compose", "build", *docker_args(directory), *release_args(releases), *sys.argv[1:], "pipelines"], cwd=directory, check=True) except KeyboardInterrupt: sys.exit(130) except (ValueError, OSError, subprocess.CalledProcessError) as error: sys.exit(f"Docker build incomplete: {error}")