11# ==============================================================================
22# Project: Python Build System (Containerized & Secured)
3+ # Description: Multi-stage build system with caching and security gates.
34# ==============================================================================
45
56# --- Configuration & Defaults -------------------------------------------------
1314 Q :=
1415endif
1516
16- # Versioning
17+ # --- Versioning ---------------------------------------------------------------
1718PYTHON_VERSION ?= 3.13.3
1819ACTIONS_PYTHON_VERSIONS ?= 3.13.3-14344076652
1920POWERSHELL_VERSION ?= v7.5.2
2021POWERSHELL_NATIVE_VERSION ?= v7.4.0
2122UBUNTU_VERSION ?= 24.04
2223
23- # [UPDATED] Security Scanning & Gates
24- # Latest Trivy version as of Dec 2025
24+ # --- Security Scanning --------------------------------------------------------
25+ # Latest Trivy version ( Dec 2025)
2526TRIVY_VERSION ?= v0.68.2
2627
27- # [UPDATED] Gates Enabled ( 1 = Fail Build)
28+ # Gates: 0 = Log Only, 1 = Fail Build
2829FAIL_ON_CRITICAL ?= 1
2930FAIL_ON_HIGH ?= 1
3031FAIL_ON_MEDIUM ?= 0
3132FAIL_ON_SECRET ?= 1
3233
33- # Architecture
34+ # --- System Architecture ------------------------------------------------------
3435ARCH_RAW := $(shell uname -m)
3536ifeq ($(ARCH_RAW ) ,x86_64)
3637 ARCH := amd64
4041 ARCH := $(ARCH_RAW )
4142endif
4243
43- # Container Engine
44+ # --- Container Engine ---------------------------------------------------------
4445CONTAINER_ENGINE := $(shell command -v podman 2>/dev/null || command -v docker)
4546ifeq ($(strip $(CONTAINER_ENGINE ) ) ,)
4647 $(error No container runtime found. Please install `docker` or `podman`)
4748endif
4849
4950# --- Internal Variables -------------------------------------------------------
5051
52+ # The base image tag used internally between stages
5153BASE_IMAGE := powershell:ubuntu-$(UBUNTU_VERSION )
5254
5355OUTPUT_DIR := python-versions/output
5456IMAGE_NAME := python:$(PYTHON_VERSION ) -ubuntu-$(UBUNTU_VERSION ) -$(ARCH )
5557TEMP_CONTAINER_NAME := python-build-$(PYTHON_VERSION ) -$(ARCH ) -tmp
5658
59+ # Artifact filenames
5760INTERNAL_ARTIFACT_NAME := python-$(PYTHON_VERSION ) -linux-$(ARCH ) .tar.gz
58- HOST_ARTIFACT_NAME := python-$(PYTHON_VERSION ) -linux-$(UBUNTU_VERSION ) -$(ARCH ) .tar.gz
61+ HOST_ARTIFACT_NAME := python-$(PYTHON_VERSION ) -linux-$(UBUNTU_VERSION ) -$(ARCH ) .tar.gz
5962
6063PS_DIR := PowerShell
6164PS_PREREQS := \
@@ -66,20 +69,17 @@ PS_PREREQS := \
6669
6770# --- Build Strategy Logic -----------------------------------------------------
6871
69- # Default: Standard local build
72+ # Default: Standard local build (Works for local dev)
7073BUILD_CMD := $(CONTAINER_ENGINE ) build
7174BUILD_OPTS :=
7275
73- # GHA Override: Use Buildx with Caching and explicit Loading
76+ # GHA Override: Use Buildx with Caching
77+ # Critical Fix: We use '--load' to ensure the built image is exported
78+ # from the isolated BuildKit container to the local Docker daemon.
7479ifeq ($(USE_GHA_CACHE ) ,1)
7580 BUILDX_BUILDER ?= gha-builder
76- CACHE_SCOPE ?= default
77-
7881 BUILD_CMD := $(CONTAINER_ENGINE ) buildx build
79- BUILD_OPTS := --builder $(BUILDX_BUILDER ) \
80- --load \
81- --cache-from type=gha,scope=$(CACHE_SCOPE ) \
82- --cache-to type=gha,mode=max,scope=$(CACHE_SCOPE )
82+ BUILD_OPTS := --builder $(BUILDX_BUILDER ) --load
8383endif
8484
8585# --- Targets ------------------------------------------------------------------
@@ -88,11 +88,15 @@ endif
8888
8989all : $(OUTPUT_DIR ) /$(HOST_ARTIFACT_NAME ) verify-gate
9090
91- # 1. Build the Python Artifact
91+ # 1. Build the Python Artifact (Stage 2)
92+ # Depends on 'powershell' target being run first to create the BASE_IMAGE
9293$(OUTPUT_DIR ) /$(HOST_ARTIFACT_NAME ) : powershell | $(OUTPUT_DIR )
9394 @echo " --- Building Python $( PYTHON_VERSION) Image ($( ARCH) ) ---"
9495 @echo " Security Gate: CRIT=$( FAIL_ON_CRITICAL) HIGH=$( FAIL_ON_HIGH) SECRET=$( FAIL_ON_SECRET) "
96+
9597 $(Q ) cd python-versions && $(BUILD_CMD ) $(BUILD_OPTS ) \
98+ --cache-from type=gha,scope=python-$(ARCH ) -$(UBUNTU_VERSION ) \
99+ --cache-to type=gha,mode=max,scope=python-$(ARCH ) -$(UBUNTU_VERSION ) \
96100 --network=host \
97101 --build-arg PYTHON_VERSION=$(PYTHON_VERSION ) \
98102 --build-arg ACTIONS_PYTHON_VERSIONS=$(ACTIONS_PYTHON_VERSIONS ) \
@@ -138,10 +142,13 @@ verify-gate:
138142 echo " " ; \
139143 fi
140144
141- # 3. Build Base PowerShell Image
145+ # 3. Build Base PowerShell Image (Stage 1)
146+ # Uses separate cache scope to prevent overwriting the Python cache
142147powershell : $(PS_PREREQS )
143148 @echo " --- Building PowerShell Base Image ---"
144149 $(Q ) cd $(PS_DIR ) && $(BUILD_CMD ) $(BUILD_OPTS ) \
150+ --cache-from type=gha,scope=powershell-$(ARCH ) -$(UBUNTU_VERSION ) \
151+ --cache-to type=gha,mode=max,scope=powershell-$(ARCH ) -$(UBUNTU_VERSION ) \
145152 --network=host \
146153 --build-arg POWERSHELL_VERSION=$(POWERSHELL_VERSION ) \
147154 --build-arg POWERSHELL_NATIVE_VERSION=$(POWERSHELL_NATIVE_VERSION ) \
0 commit comments