Skip to content

Commit 99766bc

Browse files
custom templates for developer experience
1 parent 3a357e5 commit 99766bc

File tree

2 files changed

+1580
-0
lines changed

2 files changed

+1580
-0
lines changed

azure_custom.yaml

Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json
2+
3+
name: build-your-own-copilot-solution-accelerator
4+
5+
requiredVersions:
6+
azd: ">= 1.18.0"
7+
8+
metadata:
9+
10+
11+
12+
hooks:
13+
# Pre-package hook to set container registry variables
14+
prepackage:
15+
windows:
16+
run: |
17+
Write-Host "Setting up container registry variables..." -ForegroundColor Green
18+
19+
# Get the ACR name from the deployed infrastructure
20+
$acrName = azd env get-values --output json | ConvertFrom-Json | Select-Object -ExpandProperty "AZURE_CONTAINER_REGISTRY_NAME" -ErrorAction SilentlyContinue
21+
22+
if ($acrName) {
23+
Write-Host "Using deployed ACR: $acrName" -ForegroundColor Cyan
24+
azd env set AZURE_CONTAINER_REGISTRY_ENDPOINT "$acrName.azurecr.io"
25+
azd env set AZURE_CONTAINER_REGISTRY_NAME $acrName
26+
} else {
27+
Write-Host "Warning: ACR not found in environment. Make sure infrastructure is deployed first." -ForegroundColor Yellow
28+
}
29+
shell: pwsh
30+
continueOnError: true
31+
32+
posix:
33+
run: |
34+
echo "Setting up container registry variables..."
35+
36+
# Get the ACR name from the deployed infrastructure
37+
ACR_NAME=$(azd env get-values --output json | jq -r '.AZURE_CONTAINER_REGISTRY_NAME // empty')
38+
39+
if [ ! -z "$ACR_NAME" ]; then
40+
echo "Using deployed ACR: $ACR_NAME"
41+
azd env set AZURE_CONTAINER_REGISTRY_ENDPOINT "$ACR_NAME.azurecr.io"
42+
azd env set AZURE_CONTAINER_REGISTRY_NAME "$ACR_NAME"
43+
else
44+
echo "Warning: ACR not found in environment. Make sure infrastructure is deployed first."
45+
fi
46+
shell: sh
47+
continueOnError: true
48+
49+
# Pre-deploy hook to build and push containers
50+
predeploy:
51+
windows:
52+
run: |
53+
Write-Host "🚀 Starting container deployment process..." -ForegroundColor Green
54+
55+
# Get environment variables from azd
56+
$acrName = azd env get-value AZURE_CONTAINER_REGISTRY_NAME
57+
$resourceGroup = azd env get-value AZURE_RESOURCE_GROUP
58+
$webAppName = azd env get-value WEB_APP_NAME
59+
$imageName = "byc-wa-app"
60+
$imageTag = "latest"
61+
62+
if (-not $acrName) {
63+
Write-Host "❌ Error: AZURE_CONTAINER_REGISTRY_NAME not set. Run 'azd provision' first." -ForegroundColor Red
64+
exit 1
65+
}
66+
67+
if (-not $resourceGroup) {
68+
Write-Host "❌ Error: AZURE_RESOURCE_GROUP not set. Run 'azd provision' first." -ForegroundColor Red
69+
exit 1
70+
}
71+
72+
if (-not $webAppName) {
73+
Write-Host "❌ Error: WEB_APP_NAME not set. Run 'azd provision' first." -ForegroundColor Red
74+
exit 1
75+
}
76+
77+
Write-Host "📋 Configuration:" -ForegroundColor Cyan
78+
Write-Host " ACR Name: $acrName" -ForegroundColor White
79+
Write-Host " Resource Group: $resourceGroup" -ForegroundColor White
80+
Write-Host " Web App: $webAppName" -ForegroundColor White
81+
Write-Host " Image: $imageName`:$imageTag" -ForegroundColor White
82+
83+
# Login to ACR
84+
Write-Host "🔐 Logging into ACR..." -ForegroundColor Yellow
85+
az acr login --name $acrName
86+
87+
if ($LASTEXITCODE -ne 0) {
88+
Write-Host "❌ Failed to login to ACR" -ForegroundColor Red
89+
exit 1
90+
}
91+
92+
# Build and push the container image
93+
Write-Host "🏗️ Building container image..." -ForegroundColor Yellow
94+
$fullImageName = "$acrName.azurecr.io/$imageName`:$imageTag"
95+
docker build -f "./src/App/WebApp.Dockerfile" -t $fullImageName "./src"
96+
97+
if ($LASTEXITCODE -ne 0) {
98+
Write-Host "❌ Failed to build container image" -ForegroundColor Red
99+
exit 1
100+
}
101+
102+
Write-Host "📤 Pushing container image to ACR..." -ForegroundColor Yellow
103+
docker push $fullImageName
104+
105+
if ($LASTEXITCODE -ne 0) {
106+
Write-Host "❌ Failed to push container image" -ForegroundColor Red
107+
exit 1
108+
}
109+
110+
# Update environment variables
111+
Write-Host "🔧 Updating azd environment variables..." -ForegroundColor Yellow
112+
azd env set CONTAINER_REGISTRY_HOSTNAME "$acrName.azurecr.io"
113+
azd env set CONTAINER_IMAGE_NAME $imageName
114+
azd env set IMAGE_TAG $imageTag
115+
116+
# Configure web app ACR authentication using managed identity
117+
Write-Host "🔑 Configuring ACR authentication for web app..." -ForegroundColor Yellow
118+
$webappIdentity = az webapp identity show --name $webAppName --resource-group $resourceGroup --query principalId --output tsv
119+
120+
if (-not $webappIdentity -or $webappIdentity -eq "null") {
121+
Write-Host "🔄 Enabling managed identity for web app..." -ForegroundColor Yellow
122+
$webappIdentity = az webapp identity assign --name $webAppName --resource-group $resourceGroup --query principalId --output tsv
123+
}
124+
125+
Write-Host " Web app identity: $webappIdentity" -ForegroundColor White
126+
127+
# Assign AcrPull role to web app managed identity
128+
Write-Host "🔐 Assigning AcrPull role to web app..." -ForegroundColor Yellow
129+
$acrResourceId = az acr show --name $acrName --resource-group $resourceGroup --query id --output tsv
130+
az role assignment create --assignee $webappIdentity --role AcrPull --scope $acrResourceId
131+
if ($LASTEXITCODE -ne 0) {
132+
Write-Host "⚠️ Role assignment may already exist" -ForegroundColor Yellow
133+
}
134+
135+
# Configure web app to use ACR with managed identity
136+
Write-Host "🔧 Configuring web app container settings..." -ForegroundColor Yellow
137+
az webapp config appsettings set --name $webAppName --resource-group $resourceGroup --settings `
138+
DOCKER_REGISTRY_SERVER_URL="https://$acrName.azurecr.io" `
139+
DOCKER_ENABLE_CI=true
140+
141+
# Configure web app to use managed identity for ACR authentication
142+
Write-Host "🔐 Enabling ACR managed identity authentication..." -ForegroundColor Yellow
143+
az webapp config set --name $webAppName --resource-group $resourceGroup --acr-use-identity true
144+
145+
# Update web app to use the new container image
146+
Write-Host "🚀 Updating web app container image..." -ForegroundColor Yellow
147+
$dockerImage = "DOCKER|$fullImageName"
148+
149+
# Use cmd to avoid PowerShell pipe interpretation issues
150+
$cmd = "az webapp config set --name `"$webAppName`" --resource-group `"$resourceGroup`" --linux-fx-version `"$dockerImage`""
151+
cmd /c $cmd
152+
153+
if ($LASTEXITCODE -ne 0) {
154+
Write-Host "❌ Failed to update web app configuration" -ForegroundColor Red
155+
exit 1
156+
}
157+
158+
# Restart the web app to ensure it picks up the new configuration
159+
Write-Host "🔄 Restarting web app..." -ForegroundColor Yellow
160+
az webapp restart --name $webAppName --resource-group $resourceGroup
161+
162+
Write-Host "✅ Container deployment completed successfully!" -ForegroundColor Green
163+
Write-Host "🌐 Web app URL: https://$webAppName.azurewebsites.net" -ForegroundColor Cyan
164+
Write-Host "📦 Container image: $fullImageName" -ForegroundColor Cyan
165+
166+
Write-Host ""
167+
Write-Host "⏳ The web app may take a few minutes to start up with the new container..." -ForegroundColor Yellow
168+
Write-Host " You can monitor the logs with:" -ForegroundColor White
169+
Write-Host " az webapp log tail --name $webAppName --resource-group $resourceGroup" -ForegroundColor Cyan
170+
shell: pwsh
171+
continueOnError: false
172+
173+
posix:
174+
run: |
175+
echo "🚀 Starting container deployment process..."
176+
177+
# Get environment variables from azd
178+
ACR_NAME=$(azd env get-value AZURE_CONTAINER_REGISTRY_NAME)
179+
RESOURCE_GROUP=$(azd env get-value AZURE_RESOURCE_GROUP)
180+
WEB_APP_NAME=$(azd env get-value WEB_APP_NAME)
181+
IMAGE_TAG="latest"
182+
IMAGE_NAME="byc-wa-app"
183+
184+
if [ -z "$ACR_NAME" ]; then
185+
echo "❌ Error: AZURE_CONTAINER_REGISTRY_NAME not set. Run 'azd provision' first."
186+
exit 1
187+
fi
188+
189+
if [ -z "$RESOURCE_GROUP" ]; then
190+
echo "❌ Error: AZURE_RESOURCE_GROUP not set. Run 'azd provision' first."
191+
exit 1
192+
fi
193+
194+
if [ -z "$WEB_APP_NAME" ]; then
195+
echo "❌ Error: WEB_APP_NAME not set. Run 'azd provision' first."
196+
exit 1
197+
fi
198+
199+
echo "📋 Configuration:"
200+
echo " ACR Name: $ACR_NAME"
201+
echo " Resource Group: $RESOURCE_GROUP"
202+
echo " Web App: $WEB_APP_NAME"
203+
echo " Image: $IMAGE_NAME:$IMAGE_TAG"
204+
205+
# Login to ACR
206+
echo "🔐 Logging into ACR..."
207+
az acr login --name $ACR_NAME
208+
209+
# Build and push the container image
210+
echo "🏗️ Building container image..."
211+
FULL_IMAGE_NAME="$ACR_NAME.azurecr.io/$IMAGE_NAME:$IMAGE_TAG"
212+
docker build -f "./src/App/WebApp.Dockerfile" -t $FULL_IMAGE_NAME "./src"
213+
214+
echo "📤 Pushing container image to ACR..."
215+
docker push $FULL_IMAGE_NAME
216+
217+
# Update environment variables
218+
echo "🔧 Updating azd environment variables..."
219+
azd env set CONTAINER_REGISTRY_HOSTNAME "$ACR_NAME.azurecr.io"
220+
azd env set CONTAINER_IMAGE_NAME $IMAGE_NAME
221+
azd env set IMAGE_TAG $IMAGE_TAG
222+
223+
# Configure web app ACR authentication using managed identity
224+
echo "🔑 Configuring ACR authentication for web app..."
225+
WEBAPP_IDENTITY=$(az webapp identity show --name $WEB_APP_NAME --resource-group $RESOURCE_GROUP --query principalId --output tsv)
226+
227+
if [ -z "$WEBAPP_IDENTITY" ] || [ "$WEBAPP_IDENTITY" = "null" ]; then
228+
echo "🔄 Enabling managed identity for web app..."
229+
WEBAPP_IDENTITY=$(az webapp identity assign --name $WEB_APP_NAME --resource-group $RESOURCE_GROUP --query principalId --output tsv)
230+
fi
231+
232+
echo " Web app identity: $WEBAPP_IDENTITY"
233+
234+
# Assign AcrPull role to web app managed identity
235+
echo "🔐 Assigning AcrPull role to web app..."
236+
ACR_RESOURCE_ID=$(az acr show --name $ACR_NAME --resource-group $RESOURCE_GROUP --query id --output tsv)
237+
az role assignment create --assignee $WEBAPP_IDENTITY --role AcrPull --scope $ACR_RESOURCE_ID || echo "⚠️ Role assignment may already exist"
238+
239+
# Configure web app to use ACR with managed identity
240+
echo "🔧 Configuring web app container settings..."
241+
az webapp config appsettings set --name $WEB_APP_NAME --resource-group $RESOURCE_GROUP --settings \
242+
DOCKER_REGISTRY_SERVER_URL="https://$ACR_NAME.azurecr.io" \
243+
DOCKER_ENABLE_CI=true
244+
245+
# Configure web app to use managed identity for ACR authentication
246+
echo "🔐 Enabling ACR managed identity authentication..."
247+
az webapp config set --name $WEB_APP_NAME --resource-group $RESOURCE_GROUP --acr-use-identity true
248+
249+
# Update web app to use the new container image
250+
echo "🚀 Updating web app container image..."
251+
DOCKER_IMAGE="DOCKER|$FULL_IMAGE_NAME"
252+
az webapp config set --name $WEB_APP_NAME --resource-group $RESOURCE_GROUP --linux-fx-version "$DOCKER_IMAGE"
253+
254+
# Restart the web app to ensure it picks up the new configuration
255+
echo "🔄 Restarting web app..."
256+
az webapp restart --name $WEB_APP_NAME --resource-group $RESOURCE_GROUP
257+
258+
echo "✅ Container deployment completed successfully!"
259+
echo "🌐 Web app URL: https://$WEB_APP_NAME.azurewebsites.net"
260+
echo "📦 Container image: $FULL_IMAGE_NAME"
261+
262+
echo ""
263+
echo "⏳ The web app may take a few minutes to start up with the new container..."
264+
echo " You can monitor the logs with:"
265+
echo " az webapp log tail --name $WEB_APP_NAME --resource-group $RESOURCE_GROUP"
266+
shell: sh
267+
continueOnError: false
268+
269+
postprovision:
270+
windows:
271+
run: |
272+
Write-Host "Deployment completed successfully!" -ForegroundColor Green
273+
Write-Host "Web app URL: " -NoNewline
274+
Write-Host "$env:WEB_APP_URL" -ForegroundColor Cyan
275+
Write-Host ""
276+
Write-Host "Container Registry: " -NoNewline
277+
Write-Host "$env:AZURE_CONTAINER_REGISTRY_NAME.azurecr.io" -ForegroundColor Cyan
278+
Write-Host ""
279+
Write-Host "Next steps:" -ForegroundColor Yellow
280+
Write-Host "1. Run the following command to grant permissions and load sample data:" -ForegroundColor White
281+
Write-Host " bash ./infra/scripts/process_sample_data.sh $env:AZURE_RESOURCE_GROUP" -ForegroundColor Cyan
282+
Write-Host ""
283+
Write-Host "2. For local development, see ./docs/LocalDevelopmentGuide.md" -ForegroundColor White
284+
Write-Host ""
285+
Write-Host "3. To build and deploy custom containers:" -ForegroundColor White
286+
Write-Host " azd deploy" -ForegroundColor Cyan
287+
shell: pwsh
288+
continueOnError: false
289+
interactive: true
290+
291+
posix:
292+
run: |
293+
echo "Deployment completed successfully!"
294+
echo "Web app URL: $WEB_APP_URL"
295+
echo ""
296+
echo "Container Registry: $AZURE_CONTAINER_REGISTRY_NAME.azurecr.io"
297+
echo ""
298+
echo "Next steps:"
299+
echo "1. Run the following command to grant permissions and load sample data:"
300+
echo " bash ./infra/scripts/process_sample_data.sh $AZURE_RESOURCE_GROUP"
301+
echo ""
302+
echo "2. For local development, see ./docs/LocalDevelopmentGuide.md"
303+
echo ""
304+
echo "3. To build and deploy custom containers:"
305+
echo " azd deploy"
306+
shell: sh
307+
continueOnError: false
308+
interactive: true
309+
310+
postdeploy:
311+
windows:
312+
run: |
313+
Write-Host "✅ Deployment completed! Container deployment was handled by predeploy hook." -ForegroundColor Green
314+
$webAppUrl = azd env get-value WEB_APP_URL
315+
Write-Host "🌐 Web app URL: $webAppUrl" -ForegroundColor Cyan
316+
shell: pwsh
317+
continueOnError: true
318+
319+
posix:
320+
run: |
321+
echo "✅ Deployment completed! Container deployment was handled by predeploy hook."
322+
WEB_APP_URL=$(azd env get-value WEB_APP_URL)
323+
echo "🌐 Web app URL: $WEB_APP_URL"
324+
shell: sh
325+
continueOnError: true
326+
327+
# Infrastructure configuration
328+
infra:
329+
provider: bicep
330+
path: infra
331+
module: main.bicep
332+
parameters:
333+
containerRegistryHostname: ${CONTAINER_REGISTRY_HOSTNAME=""}
334+
containerImageName: ${CONTAINER_IMAGE_NAME="byc-wa-app"}
335+
imageTag: ${IMAGE_TAG="latest"}

0 commit comments

Comments
 (0)