Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions infra/scripts/acr_build_push.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Comment thread
Priyanka2-Microsoft marked this conversation as resolved.

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 {
Expand All @@ -168,35 +194,35 @@ 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 ---
Write-Host ""
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 ---
Write-Host ""
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 ---
Write-Host ""
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 ""
Expand Down
98 changes: 31 additions & 67 deletions infra/scripts/acr_build_push.sh
Original file line number Diff line number Diff line change
@@ -1,263 +1,227 @@
#!/bin/bash

#!/bin/bash
Comment thread
Priyanka2-Microsoft marked this conversation as resolved.
# =============================================================================
# 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"
Comment thread
Priyanka2-Microsoft marked this conversation as resolved.
)
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..."
build_image \
"contentprocessor" \
"$REPO_ROOT/src/ContentProcessor/Dockerfile" \
"$REPO_ROOT/src/ContentProcessor"

echo " ✅ contentprocessor image built and pushed."

# --- ContentProcessorAPI ---
echo ""
echo " Building contentprocessorapi image..."
build_image \
"contentprocessorapi" \
"$REPO_ROOT/src/ContentProcessorAPI/Dockerfile" \
"$REPO_ROOT/src/ContentProcessorAPI"

echo " ✅ contentprocessorapi image built and pushed."

# --- ContentProcessorWeb ---
echo ""
echo " Building contentprocessorweb image..."
build_image \
"contentprocessorweb" \
"$REPO_ROOT/src/ContentProcessorWeb/Dockerfile" \
"$REPO_ROOT/src/ContentProcessorWeb"

echo " ✅ contentprocessorweb image built and pushed."

# --- ContentProcessorWorkflow ---
echo ""
echo " Building contentprocessorworkflow image..."
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
# =============================================================================
echo ""
echo "============================================================"
echo "Step 2: Updating Container Apps with new images..."
echo "============================================================"

# --- Update ContentProcessor Container App ---
echo ""
echo " Updating $CONTAINER_APP_NAME..."
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..."
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..."
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..."
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 "============================================================"
echo "============================================================"

Loading