95 lines
3.6 KiB
Python
95 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Set Gitea workflow outputs for validation and affected container images."""
|
||
|
|
import os
|
||
|
|
import re
|
||
|
|
import subprocess
|
||
|
|
|
||
|
|
|
||
|
|
def changed_paths() -> list[str]:
|
||
|
|
event = os.environ.get("EVENT_NAME", "")
|
||
|
|
ref = os.environ.get("REF", "")
|
||
|
|
if event == "push" and ref.startswith("refs/tags/"):
|
||
|
|
tag = ref.removeprefix("refs/tags/")
|
||
|
|
if not re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+(?:[.-][0-9A-Za-z.-]+)?", tag):
|
||
|
|
raise SystemExit("Release tags must be MAJOR.MINOR.PATCH with an optional prerelease suffix")
|
||
|
|
return ["*"]
|
||
|
|
if event == "pull_request":
|
||
|
|
base = os.environ["PR_BASE_SHA"]
|
||
|
|
return subprocess.check_output(
|
||
|
|
["git", "diff", "--name-only", f"{base}...HEAD"], text=True
|
||
|
|
).splitlines()
|
||
|
|
before = os.environ.get("PUSH_BEFORE_SHA", "")
|
||
|
|
current = os.environ.get("CURRENT_SHA", "HEAD")
|
||
|
|
if before and before != "0" * 40:
|
||
|
|
return subprocess.check_output(
|
||
|
|
["git", "diff", "--name-only", before, current], text=True
|
||
|
|
).splitlines()
|
||
|
|
return ["*"]
|
||
|
|
|
||
|
|
|
||
|
|
def under(path: str, *prefixes: str) -> bool:
|
||
|
|
return any(path == prefix or path.startswith(prefix.rstrip("/") + "/") for prefix in prefixes)
|
||
|
|
|
||
|
|
|
||
|
|
def scope_for(paths: list[str]) -> dict[str, bool]:
|
||
|
|
all_images = "*" in paths
|
||
|
|
validation_prefixes = ("src/", "tests/", "scripts/ci/", "docker/", "install/", "models/")
|
||
|
|
validation_files = {
|
||
|
|
"Heimdall.slnx", "global.json", ".dockerignore", ".env.example", "Makefile",
|
||
|
|
"GitVersion.yml", "GitVersion.Agent.yml", "Directory.Packages.props", "NuGet.Config",
|
||
|
|
}
|
||
|
|
run_validation = all_images or any(
|
||
|
|
under(path, *validation_prefixes) or path in validation_files or
|
||
|
|
path.startswith(".gitea/workflows/") or path.startswith("Directory.Build.")
|
||
|
|
for path in paths
|
||
|
|
)
|
||
|
|
|
||
|
|
# Shared projects included by both Docker build graphs.
|
||
|
|
shared = {
|
||
|
|
"global.json", "Directory.Packages.props", "NuGet.Config", "GitVersion.yml",
|
||
|
|
}
|
||
|
|
shared_image_change = all_images or any(
|
||
|
|
path in shared or path.startswith("Directory.Build.") for path in paths
|
||
|
|
)
|
||
|
|
server = shared_image_change or any(
|
||
|
|
under(path, "src/Heimdall.Web", "src/Heimdall.Infrastructure",
|
||
|
|
"src/Heimdall.Application", "src/Heimdall.Domain",
|
||
|
|
"src/Heimdall.Domain.Primitives", "src/Heimdall.Contracts")
|
||
|
|
or path == "docker/Dockerfile" or under(path, "install")
|
||
|
|
for path in paths
|
||
|
|
)
|
||
|
|
agent_projects = (
|
||
|
|
"src/Heimdall.Agent", "src/Heimdall.Application", "src/Heimdall.Domain",
|
||
|
|
"src/Heimdall.Domain.Primitives", "src/Heimdall.Contracts", "src/Heimdall.Mdns",
|
||
|
|
)
|
||
|
|
agent_common = any(
|
||
|
|
under(path, *agent_projects) or path == "models/yolov8n.onnx"
|
||
|
|
for path in paths
|
||
|
|
)
|
||
|
|
agent = shared_image_change or agent_common or any(
|
||
|
|
path == "docker/Dockerfile.agent" or path == "GitVersion.Agent.yml"
|
||
|
|
for path in paths
|
||
|
|
)
|
||
|
|
agent_gpu = shared_image_change or agent_common or any(
|
||
|
|
path == "docker/Dockerfile.agent.gpu" or path == "GitVersion.Agent.yml"
|
||
|
|
for path in paths
|
||
|
|
)
|
||
|
|
return {
|
||
|
|
"run_validation": run_validation,
|
||
|
|
"build_server": server,
|
||
|
|
"build_agent": agent,
|
||
|
|
"build_agent_gpu": agent_gpu,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> None:
|
||
|
|
output = os.environ.get("GITEA_OUTPUT")
|
||
|
|
if not output:
|
||
|
|
raise SystemExit("GITEA_OUTPUT is required")
|
||
|
|
with open(output, "a", encoding="utf-8") as stream:
|
||
|
|
for name, selected in scope_for(changed_paths()).items():
|
||
|
|
stream.write(f"{name}={'true' if selected else 'false'}\n")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|