diff --git a/infra/scripts/acr_build_push.ps1 b/infra/scripts/acr_build_push.ps1 index e7fde048..73530acc 100644 --- a/infra/scripts/acr_build_push.ps1 +++ b/infra/scripts/acr_build_push.ps1 @@ -125,7 +125,7 @@ $DeploymentType = az group show ` # Get the script directory and navigate to repo root $ScriptDir = $PSScriptRoot -$RepoRoot = Resolve-Path (Join-Path $ScriptDir "..\..") +$RepoRoot = (Resolve-Path (Join-Path $ScriptDir "../..")).Path Write-Host "" Write-Host " ACR Name: $ACR_NAME" @@ -144,14 +144,40 @@ function Build-Image { [string]$BuildContext ) - az acr build ` - --registry $ACR_NAME ` - --image "${ImageName}:$IMAGE_TAG" ` - --file $Dockerfile ` - --platform linux ` - $BuildContext + $ContextPath = [System.IO.Path]::GetRelativePath($RepoRoot, $BuildContext) + $DockerfilePath = [System.IO.Path]::GetRelativePath($BuildContext, $Dockerfile) + $StagingDirectory = Join-Path ([System.IO.Path]::GetTempPath()) "acr-build-$([guid]::NewGuid())" + New-Item -ItemType Directory -Path $StagingDirectory | Out-Null + + try { + $SourceFiles = git -C $RepoRoot ls-files ` + --cached ` + --others ` + --exclude-standard ` + -- $ContextPath + + if ($LASTEXITCODE -ne 0) { throw "Failed to collect build context for $ImageName" } + + foreach ($SourceFile in $SourceFiles) { + $StagedFile = [System.IO.Path]::GetRelativePath($ContextPath, $SourceFile) + $Destination = Join-Path $StagingDirectory $StagedFile + $DestinationDirectory = Split-Path -Parent $Destination + New-Item -ItemType Directory -Path $DestinationDirectory -Force | Out-Null + Copy-Item -LiteralPath (Join-Path $RepoRoot $SourceFile) -Destination $Destination + } + + az acr build ` + --registry $ACR_NAME ` + --image "${ImageName}:$IMAGE_TAG" ` + --file (Join-Path $StagingDirectory $DockerfilePath) ` + --platform linux ` + $StagingDirectory - if ($LASTEXITCODE -ne 0) { throw "Failed to build $ImageName image" } + if ($LASTEXITCODE -ne 0) { throw "Failed to build $ImageName image" } + } + finally { + Remove-Item -LiteralPath $StagingDirectory -Recurse -Force -ErrorAction SilentlyContinue + } } try { @@ -168,8 +194,8 @@ try { Write-Host " Building contentprocessor image..." Build-Image ` -ImageName "contentprocessor" ` - -Dockerfile "$RepoRoot\src\ContentProcessor\Dockerfile" ` - -BuildContext "$RepoRoot\src\ContentProcessor" + -Dockerfile (Join-Path $RepoRoot "src/ContentProcessor/Dockerfile") ` + -BuildContext (Join-Path $RepoRoot "src/ContentProcessor") Write-Host " [OK] contentprocessor image built and pushed." # --- ContentProcessorAPI --- @@ -177,8 +203,8 @@ try { Write-Host " Building contentprocessorapi image..." Build-Image ` -ImageName "contentprocessorapi" ` - -Dockerfile "$RepoRoot\src\ContentProcessorAPI\Dockerfile" ` - -BuildContext "$RepoRoot\src\ContentProcessorAPI" + -Dockerfile (Join-Path $RepoRoot "src/ContentProcessorAPI/Dockerfile") ` + -BuildContext (Join-Path $RepoRoot "src/ContentProcessorAPI") Write-Host " [OK] contentprocessorapi image built and pushed." # --- ContentProcessorWeb --- @@ -186,8 +212,8 @@ try { Write-Host " Building contentprocessorweb image..." Build-Image ` -ImageName "contentprocessorweb" ` - -Dockerfile "$RepoRoot\src\ContentProcessorWeb\Dockerfile" ` - -BuildContext "$RepoRoot\src\ContentProcessorWeb" + -Dockerfile (Join-Path $RepoRoot "src/ContentProcessorWeb/Dockerfile") ` + -BuildContext (Join-Path $RepoRoot "src/ContentProcessorWeb") Write-Host " [OK] contentprocessorweb image built and pushed." # --- ContentProcessorWorkflow --- @@ -195,8 +221,8 @@ try { Write-Host " Building contentprocessorworkflow image..." Build-Image ` -ImageName "contentprocessorworkflow" ` - -Dockerfile "$RepoRoot\src\ContentProcessorWorkflow\Dockerfile" ` - -BuildContext "$RepoRoot\src\ContentProcessorWorkflow" + -Dockerfile (Join-Path $RepoRoot "src/ContentProcessorWorkflow/Dockerfile") ` + -BuildContext (Join-Path $RepoRoot "src/ContentProcessorWorkflow") Write-Host " [OK] contentprocessorworkflow image built and pushed." Write-Host "" diff --git a/infra/scripts/acr_build_push.sh b/infra/scripts/acr_build_push.sh index 1e1ad248..2ae4241f 100644 --- a/infra/scripts/acr_build_push.sh +++ b/infra/scripts/acr_build_push.sh @@ -1,171 +1,152 @@ -#!/bin/bash +#!/bin/bash # ============================================================================= # ACR Build and Push Script # This script builds container images remotely using Azure Container Registry # and updates the Container Apps to use the new images. # ============================================================================= - set -e - RESOURCE_GROUP_NAME="$1" - echo "============================================================" echo "ACR Build and Push - Starting..." echo "============================================================" - if [ -z "$RESOURCE_GROUP_NAME" ]; then - echo "Using azd environment values..." - ACR_NAME=$(azd env get-value CONTAINER_REGISTRY_NAME) ACR_LOGIN_SERVER=$(azd env get-value CONTAINER_REGISTRY_LOGIN_SERVER) RESOURCE_GROUP=$(azd env get-value AZURE_RESOURCE_GROUP) - CONTAINER_APP_NAME=$(azd env get-value CONTAINER_APP_NAME) CONTAINER_API_APP_NAME=$(azd env get-value CONTAINER_API_APP_NAME) CONTAINER_WEB_APP_NAME=$(azd env get-value CONTAINER_WEB_APP_NAME) CONTAINER_WORKFLOW_APP_NAME=$(azd env get-value CONTAINER_WORKFLOW_APP_NAME) - else - echo "Using existing deployment from Resource Group: $RESOURCE_GROUP_NAME" - RESOURCE_GROUP="$RESOURCE_GROUP_NAME" - ACR_NAME=$(az acr list \ --resource-group "$RESOURCE_GROUP" \ --query "[0].name" \ -o tsv) - ACR_LOGIN_SERVER=$(az acr show \ --name "$ACR_NAME" \ --resource-group "$RESOURCE_GROUP" \ --query loginServer \ -o tsv) - while read APP; do - case "$APP" in - *-app) CONTAINER_APP_NAME="$APP" ;; - *-api) CONTAINER_API_APP_NAME="$APP" ;; - *-web) CONTAINER_WEB_APP_NAME="$APP" ;; - *-wkfl) CONTAINER_WORKFLOW_APP_NAME="$APP" ;; - esac - done < <( az containerapp list \ --resource-group "$RESOURCE_GROUP" \ --query "[].name" \ -o tsv ) - fi - IMAGE_TAG="latest" DEPLOYMENT_TYPE=$(az group show \ --name "$RESOURCE_GROUP" \ --query "tags.Type" \ -o tsv) - if [ "$DEPLOYMENT_TYPE" = "WAF" ]; then - echo "" echo "WAF deployment detected. Temporarily relaxing ACR restrictions..." - az acr update \ --name "$ACR_NAME" \ --resource-group "$RESOURCE_GROUP" \ --allow-exports true - az acr update \ --name "$ACR_NAME" \ --resource-group "$RESOURCE_GROUP" \ --public-network-enabled true - az acr update \ --name "$ACR_NAME" \ --resource-group "$RESOURCE_GROUP" \ --default-action Allow - echo "ACR restrictions temporarily relaxed." - fi - # Get the directory where this script is located SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Navigate to repo root (infra/scripts -> root) REPO_ROOT="$(realpath "$SCRIPT_DIR/../..")" - echo "" echo " ACR Name: $ACR_NAME" echo " ACR Login Server: $ACR_LOGIN_SERVER" echo " Resource Group: $RESOURCE_GROUP" echo " Image Tag: $IMAGE_TAG" echo "" - cleanup() { - if [ "$DEPLOYMENT_TYPE" = "WAF" ]; then - echo "" echo "Restoring WAF ACR configuration..." - az acr update \ --name "$ACR_NAME" \ --resource-group "$RESOURCE_GROUP" \ --default-action Deny - az acr update \ --name "$ACR_NAME" \ --resource-group "$RESOURCE_GROUP" \ --public-network-enabled false - az acr update \ --name "$ACR_NAME" \ --resource-group "$RESOURCE_GROUP" \ --allow-exports false - echo "ACR configuration restored." - fi } - trap cleanup EXIT - build_image() { local image_name="$1" local dockerfile="$2" local build_context="$3" - - az acr build \ + local context_path + local dockerfile_path + local staging_dir + local source_file + local staged_file + context_path="${build_context#"$REPO_ROOT"/}" + dockerfile_path="${dockerfile#"$build_context"/}" + staging_dir="$(mktemp -d)" + while IFS= read -r -d '' source_file; do + staged_file="${source_file#"$context_path"/}" + mkdir -p "$staging_dir/$(dirname "$staged_file")" + cp -a "$REPO_ROOT/$source_file" "$staging_dir/$staged_file" + done < <( + git -C "$REPO_ROOT" ls-files \ + --cached \ + --others \ + --exclude-standard \ + -z \ + -- "$context_path" + ) + if az acr build \ --registry "$ACR_NAME" \ --image "$image_name:$IMAGE_TAG" \ - --file "$dockerfile" \ + --file "$staging_dir/$dockerfile_path" \ --platform linux \ - "$build_context" + "$staging_dir"; then + rm -rf "$staging_dir" + return 0 + fi + rm -rf "$staging_dir" + return 1 } - # ============================================================================= # Step 1: Build and push images to ACR using az acr build # ============================================================================= echo "============================================================" echo "Step 1: Building and pushing images to ACR..." echo "============================================================" - # --- ContentProcessor --- echo "" echo " Building contentprocessor image..." @@ -173,9 +154,7 @@ build_image \ "contentprocessor" \ "$REPO_ROOT/src/ContentProcessor/Dockerfile" \ "$REPO_ROOT/src/ContentProcessor" - echo " ✅ contentprocessor image built and pushed." - # --- ContentProcessorAPI --- echo "" echo " Building contentprocessorapi image..." @@ -183,9 +162,7 @@ build_image \ "contentprocessorapi" \ "$REPO_ROOT/src/ContentProcessorAPI/Dockerfile" \ "$REPO_ROOT/src/ContentProcessorAPI" - echo " ✅ contentprocessorapi image built and pushed." - # --- ContentProcessorWeb --- echo "" echo " Building contentprocessorweb image..." @@ -193,9 +170,7 @@ build_image \ "contentprocessorweb" \ "$REPO_ROOT/src/ContentProcessorWeb/Dockerfile" \ "$REPO_ROOT/src/ContentProcessorWeb" - echo " ✅ contentprocessorweb image built and pushed." - # --- ContentProcessorWorkflow --- echo "" echo " Building contentprocessorworkflow image..." @@ -203,12 +178,9 @@ build_image \ "contentprocessorworkflow" \ "$REPO_ROOT/src/ContentProcessorWorkflow/Dockerfile" \ "$REPO_ROOT/src/ContentProcessorWorkflow" - echo " ✅ contentprocessorworkflow image built and pushed." - echo "" echo " All images built and pushed successfully." - # ============================================================================= # Step 2: Update Container Apps to use the new images from ACR # ============================================================================= @@ -216,7 +188,6 @@ echo "" echo "============================================================" echo "Step 2: Updating Container Apps with new images..." echo "============================================================" - # --- Update ContentProcessor Container App --- echo "" echo " Updating $CONTAINER_APP_NAME..." @@ -224,9 +195,7 @@ az containerapp update \ --name "$CONTAINER_APP_NAME" \ --resource-group "$RESOURCE_GROUP" \ --image "$ACR_LOGIN_SERVER/contentprocessor:$IMAGE_TAG" - echo " ✅ $CONTAINER_APP_NAME updated." - # --- Update ContentProcessorAPI Container App --- echo "" echo " Updating $CONTAINER_API_APP_NAME..." @@ -234,9 +203,7 @@ az containerapp update \ --name "$CONTAINER_API_APP_NAME" \ --resource-group "$RESOURCE_GROUP" \ --image "$ACR_LOGIN_SERVER/contentprocessorapi:$IMAGE_TAG" - echo " ✅ $CONTAINER_API_APP_NAME updated." - # --- Update ContentProcessorWeb Container App --- echo "" echo " Updating $CONTAINER_WEB_APP_NAME..." @@ -244,9 +211,7 @@ az containerapp update \ --name "$CONTAINER_WEB_APP_NAME" \ --resource-group "$RESOURCE_GROUP" \ --image "$ACR_LOGIN_SERVER/contentprocessorweb:$IMAGE_TAG" - echo " ✅ $CONTAINER_WEB_APP_NAME updated." - # --- Update ContentProcessorWorkflow Container App --- echo "" echo " Updating $CONTAINER_WORKFLOW_APP_NAME..." @@ -254,10 +219,9 @@ az containerapp update \ --name "$CONTAINER_WORKFLOW_APP_NAME" \ --resource-group "$RESOURCE_GROUP" \ --image "$ACR_LOGIN_SERVER/contentprocessorworkflow:$IMAGE_TAG" - echo " ✅ $CONTAINER_WORKFLOW_APP_NAME updated." - echo "" echo "============================================================" echo "ACR Build and Push - Completed Successfully!" -echo "============================================================" \ No newline at end of file +echo "============================================================" + \ No newline at end of file diff --git a/src/ContentProcessorWeb/.dockerignore b/src/ContentProcessorWeb/.dockerignore index 31e8019c..e466503d 100644 --- a/src/ContentProcessorWeb/.dockerignore +++ b/src/ContentProcessorWeb/.dockerignore @@ -1,9 +1,3 @@ -# Include any files or directories that you don't want to be copied to your -# container here (e.g., local build artifacts, temporary files, etc.). -# -# For more help, visit the .dockerignore file reference guide at -# https://docs.docker.com/engine/reference/builder/#dockerignore-file - **/.DS_Store **/__pycache__ **/.venv @@ -32,11 +26,13 @@ **/values.dev.yaml LICENSE README.md -# Include any files or directories that you don't want to be copied to your -# container here (e.g., local build artifacts, temporary files, etc.). -# -# For more help, visit the .dockerignore file reference guide at -# https://docs.docker.com/engine/reference/builder/#dockerignore-file - -**/node_modules \ No newline at end of file +# Azure CLI context packaging requires explicit root-level exclusions. +node_modules +build +coverage +.cache +.parcel-cache +.pnpm-store +.pnpm-cache +.pnpm \ No newline at end of file