@@ -24,42 +24,55 @@ if ($USE_LOCAL_BUILD -eq $false) {
2424
2525Write-Output " Local Build enabled. Starting build process."
2626
27- # STEP 1: Set Azure subscription
27+ # STEP 1: Ensure user is logged into Azure
28+ Write-Host " Checking Azure login status..."
29+ $account = az account show 2> $null | ConvertFrom-Json
30+
31+ if (-not $account ) {
32+ Write-Host " Not logged in. Attempting az login..."
33+ az login | Out-Null
34+ if ($LASTEXITCODE -ne 0 ) {
35+ Write-Error " Azure login failed."
36+ exit 1
37+ }
38+ } else {
39+ Write-Host " Already logged in to Azure as: $ ( $account.user.name ) "
40+ }
41+
42+ # STEP 2: Set Azure subscription
2843az account set -- subscription " $AZURE_SUBSCRIPTION_ID "
2944if ($LASTEXITCODE -ne 0 ) {
3045 Write-Error " Failed to set Azure subscription."
3146 exit 1
3247}
3348
34- # STEP 2 : Deploy container registry
49+ # STEP 3 : Deploy container registry
3550Write-Host " Deploying container registry in location: $AZURE_LOCATION "
3651$OUTPUTS = az deployment group create -- resource- group $AZURE_RESOURCE_GROUP -- template- file " ./infra/deploy_container_registry.bicep" -- parameters environmentName= $ENV_NAME -- query " properties.outputs" -- output json | ConvertFrom-Json
3752
3853# Extract ACR name and endpoint
3954$ACR_NAME = $OUTPUTS.createdAcrName.value
40- $ACR_ENDPOINT = $OUTPUTS.acrEndpoint.value
4155
4256Write-Host " Extracted ACR Name: $ACR_NAME "
43- Write-Host " Extracted ACR Endpoint: $ACR_ENDPOINT "
4457
45- # STEP 3 : Login to Azure Container Registry
58+ # STEP 4 : Login to Azure Container Registry
4659Write-Host " Logging into Azure Container Registry: $ACR_NAME "
4760az acr login - n $ACR_NAME
4861if ($LASTEXITCODE -ne 0 ) {
4962 Write-Error " Failed to log in to ACR"
5063 exit 1
5164}
5265
53- # STEP 4 : Get current script directory
66+ # STEP 5 : Get current script directory
5467$ScriptDir = Split-Path - Parent $MyInvocation.MyCommand.Path
5568
56- # STEP 5 : Resolve full paths to Dockerfiles and build contexts
69+ # STEP 6 : Resolve full paths to Dockerfiles and build contexts
5770$WebAppDockerfilePath = Join-Path $ScriptDir " ..\..\src\App\WebApp.Dockerfile" | Resolve-Path
5871$WebAppContextPath = Join-Path $ScriptDir " ..\..\src\App" | Resolve-Path
5972$ApiAppDockerfilePath = Join-Path $ScriptDir " ..\..\src\api\ApiApp.Dockerfile" | Resolve-Path
6073$ApiAppContextPath = Join-Path $ScriptDir " ..\..\src\api" | Resolve-Path
6174
62- # STEP 6 : Define function to build and push Docker images
75+ # STEP 7 : Define function to build and push Docker images
6376function Build-And-Push-Image {
6477 param (
6578 [string ]$IMAGE_NAME ,
@@ -88,7 +101,7 @@ function Build-And-Push-Image {
88101 Write-Host " --- Docker image pushed successfully: $IMAGE_URI ---`n "
89102}
90103
91- # STEP 7 : Build and push images with provided tag
104+ # STEP 8 : Build and push images with provided tag
92105$ACR_IMAGE_TAG = " latest"
93106Build-And - Push-Image " km-api" $ApiAppDockerfilePath $ApiAppContextPath $ACR_IMAGE_TAG
94107Build-And - Push-Image " km-app" $WebAppDockerfilePath $WebAppContextPath $ACR_IMAGE_TAG
0 commit comments