merge: align dev branch with main
This commit is contained in:
@@ -6,9 +6,7 @@ import subprocess
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def build_image(
|
||||
instance_dir: str, dockerfile: str, tag: str, build_context: dict | None = None
|
||||
) -> tuple[int, str, str]:
|
||||
def build_image(instance_dir: str, dockerfile: str, tag: str, build_context: dict | None = None) -> tuple[int, str, str]:
|
||||
"""Build a Docker image from a Dockerfile.
|
||||
|
||||
Args:
|
||||
@@ -20,18 +18,13 @@ def build_image(
|
||||
Returns:
|
||||
Tuple of (returncode, stdout, stderr)
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Defensive: normalise any CRLF that may have crept in from manifest DB
|
||||
# strings — Docker's legacy builder treats \r as a character after the
|
||||
# backslash, breaking RUN continuations and producing
|
||||
# "unknown instruction" errors.
|
||||
dockerfile = dockerfile.replace("\r\n", "\n").replace("\r", "\n")
|
||||
|
||||
# Write Dockerfile
|
||||
dockerfile_path = Path(instance_dir) / "Dockerfile"
|
||||
dockerfile_path.write_text(dockerfile, newline="\n")
|
||||
logger.debug("Wrote Dockerfile to %s (%d bytes)", dockerfile_path, len(dockerfile))
|
||||
dockerfile_path.write_text(dockerfile)
|
||||
logger.info("Wrote Dockerfile to %s", dockerfile_path)
|
||||
|
||||
# Write build context files
|
||||
if build_context:
|
||||
@@ -41,27 +34,19 @@ def build_image(
|
||||
try:
|
||||
full_path.resolve().relative_to(Path(instance_dir).resolve())
|
||||
except ValueError:
|
||||
logger.error(
|
||||
"Build context file path escapes instance directory: %s", file_path
|
||||
)
|
||||
raise ValueError(
|
||||
f"Build context file path '{file_path}' escapes instance directory"
|
||||
)
|
||||
|
||||
logger.error("Build context file path escapes instance directory: %s", file_path)
|
||||
raise ValueError(f"Build context file path '{file_path}' escapes instance directory")
|
||||
|
||||
full_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
normalized = content.replace("\r\n", "\n").replace("\r", "\n")
|
||||
full_path.write_text(normalized, newline="\n")
|
||||
logger.debug("Wrote build context file: %s", full_path)
|
||||
full_path.write_text(content)
|
||||
logger.info("Wrote build context file: %s", full_path)
|
||||
|
||||
# Build image
|
||||
logger.debug("Building Docker image with tag: %s", tag)
|
||||
logger.info("Building Docker image with tag: %s", tag)
|
||||
cmd = [
|
||||
"docker",
|
||||
"build",
|
||||
"-t",
|
||||
tag,
|
||||
"-f",
|
||||
str(dockerfile_path),
|
||||
"docker", "build",
|
||||
"-t", tag,
|
||||
"-f", str(dockerfile_path),
|
||||
instance_dir,
|
||||
]
|
||||
|
||||
@@ -72,7 +57,7 @@ def build_image(
|
||||
text=True,
|
||||
timeout=300, # 5 minute timeout for builds
|
||||
)
|
||||
logger.debug("Docker build completed: returncode=%d", result.returncode)
|
||||
logger.info("Docker build completed: returncode=%d", result.returncode)
|
||||
if result.returncode != 0:
|
||||
logger.error("Docker build failed: %s", result.stderr[:1000])
|
||||
return result.returncode, result.stdout, result.stderr
|
||||
|
||||
Reference in New Issue
Block a user