Skip to content

Commit 633c569

Browse files
Add packaging scripts for web app deployment and update Azure configuration
1 parent 032ff4c commit 633c569

File tree

4 files changed

+256
-36
lines changed

4 files changed

+256
-36
lines changed

azure_custom.yaml

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ parameters:
2020
type: string
2121
default: 'https://github.com/microsoft/document-generation-solution-accelerator'
2222

23+
services:
24+
webapp:
25+
project: ./src
26+
language: py
27+
host: appservice
28+
dist: ./dist
29+
hooks:
30+
prepackage:
31+
windows:
32+
shell: pwsh
33+
run: ../infra/scripts/package_webapp.ps1
34+
interactive: true
35+
continueOnError: false
36+
posix:
37+
shell: sh
38+
run: bash ../infra/scripts/package_webapp.sh
39+
interactive: true
40+
continueOnError: false
41+
2342
deployment:
2443
mode: Incremental
2544
template: ./infra/main.bicep # Path to the main.bicep file inside the 'deployment' folder
@@ -28,32 +47,32 @@ deployment:
2847
otherLocation: ${parameters.otherLocation}
2948
baseUrl: ${parameters.baseUrl}
3049

31-
hooks:
32-
postprovision:
33-
windows:
34-
run: |
35-
./scripts/auth_update.ps1
36-
Write-Host "`nBuilding and pushing Docker image to ACR..." -ForegroundColor Yellow
37-
./scripts/build-push-acr.ps1
38-
Write-Host "`nWeb app URL: "
39-
Write-Host "$env:WEB_APP_URL" -ForegroundColor Cyan
40-
Write-Host "`nIf you want to use the Sample Data, run the following command in the Bash terminal to process it:"
41-
Write-Host "bash ./infra/scripts/process_sample_data.sh" -ForegroundColor Cyan
42-
shell: pwsh
43-
continueOnError: false
44-
interactive: true
45-
posix:
46-
run: |
47-
./scripts/auth_update.sh
48-
echo ""
49-
echo "Building and pushing Docker image to ACR..."
50-
bash ./scripts/build-push-acr.sh
51-
echo ""
52-
echo "Web app URL: "
53-
echo $WEB_APP_URL
54-
echo ""
55-
echo "If you want to use the Sample Data, run the following command in the bash terminal to process it:"
56-
echo "bash ./infra/scripts/process_sample_data.sh"
57-
shell: sh
58-
continueOnError: false
59-
interactive: true
50+
# hooks:
51+
# postprovision:
52+
# windows:
53+
# run: |
54+
# ./scripts/auth_update.ps1
55+
# Write-Host "`nBuilding and pushing Docker image to ACR..." -ForegroundColor Yellow
56+
# ./scripts/build-push-acr.ps1
57+
# Write-Host "`nWeb app URL: "
58+
# Write-Host "$env:WEB_APP_URL" -ForegroundColor Cyan
59+
# Write-Host "`nIf you want to use the Sample Data, run the following command in the Bash terminal to process it:"
60+
# Write-Host "bash ./infra/scripts/process_sample_data.sh" -ForegroundColor Cyan
61+
# shell: pwsh
62+
# continueOnError: false
63+
# interactive: true
64+
# posix:
65+
# run: |
66+
# ./scripts/auth_update.sh
67+
# echo ""
68+
# echo "Building and pushing Docker image to ACR..."
69+
# bash ./scripts/build-push-acr.sh
70+
# echo ""
71+
# echo "Web app URL: "
72+
# echo $WEB_APP_URL
73+
# echo ""
74+
# echo "If you want to use the Sample Data, run the following command in the bash terminal to process it:"
75+
# echo "bash ./infra/scripts/process_sample_data.sh"
76+
# shell: sh
77+
# continueOnError: false
78+
# interactive: true

infra/main_custom.bicep

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,23 +1176,21 @@ module webSite 'modules/web-sites.bicep' = {
11761176
name: take('module.web-sites.${webSiteResourceName}', 64)
11771177
params: {
11781178
name: webSiteResourceName
1179-
tags: tags
1179+
tags: union(tags, { 'azd-service-name': 'webapp' })
11801180
location: solutionLocation
1181-
kind: 'app,linux,container'
1181+
kind: 'app,linux'
11821182
serverFarmResourceId: webServerFarm.outputs.resourceId
11831183
managedIdentities: { userAssignedResourceIds: [userAssignedIdentity!.outputs.resourceId] }
11841184
siteConfig: {
1185-
linuxFxVersion: 'DOCKER|${containerRegistryName}.azurecr.io/document-generation/backend:${imageTag}'
1185+
linuxFxVersion: 'PYTHON|3.11'
11861186
minTlsVersion: '1.2'
1187-
acrUseManagedIdentityCreds: true
1188-
acrUserManagedIdentityID: userAssignedIdentity.outputs.clientId
1187+
appCommandLine: 'gunicorn -b 0.0.0.0:8000 app:app'
11891188
}
11901189
configs: concat([
11911190
{
11921191
name: 'appsettings'
11931192
properties: {
1194-
SCM_DO_BUILD_DURING_DEPLOYMENT: 'true'
1195-
DOCKER_REGISTRY_SERVER_URL: 'https://${containerRegistryName}.azurecr.io'
1193+
SCM_DO_BUILD_DURING_DEPLOYMENT: 'false'
11961194
AUTH_ENABLED: 'false'
11971195
AZURE_SEARCH_SERVICE: aiSearch.outputs.name
11981196
AZURE_SEARCH_INDEX: azureSearchIndex

infra/scripts/package_webapp.ps1

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env pwsh
2+
3+
# Package web app for Azure App Service deployment
4+
# This script builds the frontend and packages the backend with static files into a zip
5+
6+
Write-Host "Starting web app packaging for App Service..." -ForegroundColor Cyan
7+
8+
$ErrorActionPreference = "Stop"
9+
10+
# Get the script directory and navigate to project root
11+
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
12+
$projectRoot = Resolve-Path (Join-Path $scriptDir "../..")
13+
$srcDir = Join-Path $projectRoot "src"
14+
$distDir = Join-Path $srcDir "dist"
15+
16+
Write-Host "Project root: $projectRoot" -ForegroundColor Gray
17+
Write-Host "Source directory: $srcDir" -ForegroundColor Gray
18+
Write-Host "Dist directory: $distDir" -ForegroundColor Gray
19+
20+
# Clean dist directory if it exists
21+
if (Test-Path $distDir) {
22+
Write-Host "Cleaning existing dist directory..." -ForegroundColor Yellow
23+
Remove-Item -Path $distDir -Recurse -Force
24+
}
25+
26+
# Create dist directory
27+
Write-Host "Creating dist directory..." -ForegroundColor Yellow
28+
New-Item -Path $distDir -ItemType Directory -Force | Out-Null
29+
30+
# Step 1: Build frontend
31+
Write-Host "`nStep 1: Building frontend..." -ForegroundColor Cyan
32+
$frontendDir = Join-Path $srcDir "frontend"
33+
34+
if (-not (Test-Path (Join-Path $frontendDir "node_modules"))) {
35+
Write-Host "Installing frontend dependencies..." -ForegroundColor Yellow
36+
Push-Location $frontendDir
37+
try {
38+
npm ci
39+
if ($LASTEXITCODE -ne 0) {
40+
throw "npm ci failed"
41+
}
42+
} finally {
43+
Pop-Location
44+
}
45+
}
46+
47+
Write-Host "Running frontend build..." -ForegroundColor Yellow
48+
Push-Location $frontendDir
49+
try {
50+
$env:NODE_OPTIONS = "--max_old_space_size=8192"
51+
npm run build
52+
if ($LASTEXITCODE -ne 0) {
53+
throw "Frontend build failed"
54+
}
55+
} finally {
56+
Pop-Location
57+
Remove-Item Env:\NODE_OPTIONS -ErrorAction SilentlyContinue
58+
}
59+
60+
# Step 2: Copy backend files
61+
Write-Host "`nStep 2: Copying backend files..." -ForegroundColor Cyan
62+
63+
# Copy Python files and backend code
64+
$filesToCopy = @(
65+
"app.py",
66+
"event_utils.py",
67+
"gunicorn.conf.py",
68+
"requirements.txt",
69+
"start.sh",
70+
"start.cmd"
71+
)
72+
73+
foreach ($file in $filesToCopy) {
74+
$sourcePath = Join-Path $srcDir $file
75+
if (Test-Path $sourcePath) {
76+
Write-Host " Copying $file" -ForegroundColor Gray
77+
Copy-Item -Path $sourcePath -Destination $distDir -Force
78+
}
79+
}
80+
81+
# Copy backend directory
82+
$backendSrc = Join-Path $srcDir "backend"
83+
$backendDst = Join-Path $distDir "backend"
84+
if (Test-Path $backendSrc) {
85+
Write-Host " Copying backend directory..." -ForegroundColor Gray
86+
Copy-Item -Path $backendSrc -Destination $backendDst -Recurse -Force
87+
}
88+
89+
# Copy static files (built frontend)
90+
$staticSrc = Join-Path $srcDir "static"
91+
$staticDst = Join-Path $distDir "static"
92+
if (Test-Path $staticSrc) {
93+
Write-Host " Copying static directory (frontend build output)..." -ForegroundColor Gray
94+
Copy-Item -Path $staticSrc -Destination $staticDst -Recurse -Force
95+
} else {
96+
Write-Host " WARNING: Static directory not found at $staticSrc" -ForegroundColor Yellow
97+
}
98+
99+
# Verify the dist directory
100+
$fileCount = (Get-ChildItem -Path $distDir -Recurse -File | Measure-Object).Count
101+
$distSize = (Get-ChildItem -Path $distDir -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
102+
103+
Write-Host "`n✓ Successfully prepared deployment package!" -ForegroundColor Green
104+
Write-Host " Dist location: $distDir" -ForegroundColor Cyan
105+
Write-Host " Total files: $fileCount" -ForegroundColor Cyan
106+
Write-Host " Total size: $([math]::Round($distSize, 2)) MB" -ForegroundColor Cyan
107+
108+
Write-Host "`nPackaging complete! azd will handle zip creation during deployment." -ForegroundColor Green

infra/scripts/package_webapp.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash
2+
3+
# Package web app for Azure App Service deployment
4+
# This script builds the frontend and packages the backend with static files into a zip
5+
6+
set -e
7+
8+
echo "Starting web app packaging for App Service..."
9+
10+
# Get the script directory and navigate to project root
11+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12+
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
13+
SRC_DIR="$PROJECT_ROOT/src"
14+
DIST_DIR="$SRC_DIR/dist"
15+
16+
echo "Project root: $PROJECT_ROOT"
17+
echo "Source directory: $SRC_DIR"
18+
echo "Dist directory: $DIST_DIR"
19+
20+
# Clean dist directory if it exists
21+
if [ -d "$DIST_DIR" ]; then
22+
echo "Cleaning existing dist directory..."
23+
rm -rf "$DIST_DIR"
24+
fi
25+
26+
# Create dist directory
27+
echo "Creating dist directory..."
28+
mkdir -p "$DIST_DIR"
29+
30+
# Step 1: Build frontend
31+
echo ""
32+
echo "Step 1: Building frontend..."
33+
FRONTEND_DIR="$SRC_DIR/frontend"
34+
35+
if [ ! -d "$FRONTEND_DIR/node_modules" ]; then
36+
echo "Installing frontend dependencies..."
37+
cd "$FRONTEND_DIR"
38+
npm ci
39+
cd "$PROJECT_ROOT"
40+
fi
41+
42+
echo "Running frontend build..."
43+
cd "$FRONTEND_DIR"
44+
export NODE_OPTIONS=--max_old_space_size=8192
45+
npm run build
46+
unset NODE_OPTIONS
47+
cd "$PROJECT_ROOT"
48+
49+
# Step 2: Copy backend files
50+
echo ""
51+
echo "Step 2: Copying backend files..."
52+
53+
# Copy Python files and backend code
54+
FILES_TO_COPY=(
55+
"app.py"
56+
"event_utils.py"
57+
"gunicorn.conf.py"
58+
"requirements.txt"
59+
"start.sh"
60+
"start.cmd"
61+
)
62+
63+
for file in "${FILES_TO_COPY[@]}"; do
64+
if [ -f "$SRC_DIR/$file" ]; then
65+
echo " Copying $file"
66+
cp "$SRC_DIR/$file" "$DIST_DIR/"
67+
fi
68+
done
69+
70+
# Copy backend directory
71+
if [ -d "$SRC_DIR/backend" ]; then
72+
echo " Copying backend directory..."
73+
cp -r "$SRC_DIR/backend" "$DIST_DIR/"
74+
fi
75+
76+
# Copy static files (built frontend)
77+
if [ -d "$SRC_DIR/static" ]; then
78+
echo " Copying static directory (frontend build output)..."
79+
cp -r "$SRC_DIR/static" "$DIST_DIR/"
80+
else
81+
echo " WARNING: Static directory not found at $SRC_DIR/static"
82+
fi
83+
84+
# Verify the dist directory
85+
FILE_COUNT=$(find "$DIST_DIR" -type f | wc -l)
86+
DIST_SIZE=$(du -sh "$DIST_DIR" | cut -f1)
87+
88+
echo ""
89+
echo "✓ Successfully prepared deployment package!"
90+
echo " Dist location: $DIST_DIR"
91+
echo " Total files: $FILE_COUNT"
92+
echo " Total size: $DIST_SIZE"
93+
94+
echo ""
95+
echo "Packaging complete! azd will handle zip creation during deployment."

0 commit comments

Comments
 (0)