Skip to content

Commit cacdb72

Browse files
committed
fix: preserve existing AZURE_LOCATION in randomize scripts
Prevent location conflicts when re-running azd provision by checking if AZURE_LOCATION is already set before randomizing. This fixes the issue where lab users would encounter errors on retry because the location changed between provision attempts. - Add existence check using 'azd env get-value AZURE_LOCATION' - Only randomize location if not already set or empty - Apply fix to both bash and PowerShell versions - Tested with multiple scenarios (existing, empty, persistence)
1 parent 2a0a914 commit cacdb72

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

scripts/randomize-region.ps1

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
# Run this script before 'azd provision' or 'azd up' to use a random region
33
# without being prompted. Otherwise, azd will prompt you to select a region.
44

5-
$regions = @("westus2", "westus3", "eastus2", "northcentralus")
6-
$selectedRegion = Get-Random -InputObject $regions
5+
# Check if AZURE_LOCATION is already set
6+
$existingLocation = azd env get-value AZURE_LOCATION 2>$null
77

8-
Write-Host "🎲 Randomly selected region: $selectedRegion" -ForegroundColor Green
9-
azd env set AZURE_LOCATION $selectedRegion
10-
Write-Host "✅ Region set. Run 'azd provision' to deploy without being prompted." -ForegroundColor Green
11-
Write-Host " To change: azd env set AZURE_LOCATION <region>" -ForegroundColor Gray
8+
if ($existingLocation) {
9+
Write-Host "ℹ️ AZURE_LOCATION is already set to: $existingLocation" -ForegroundColor Cyan
10+
Write-Host " Keeping existing location. To change, run: azd env set AZURE_LOCATION <region>" -ForegroundColor Gray
11+
} else {
12+
$regions = @("westus2", "westus3", "eastus2", "northcentralus")
13+
$selectedRegion = Get-Random -InputObject $regions
14+
15+
Write-Host "🎲 Randomly selected region: $selectedRegion" -ForegroundColor Green
16+
azd env set AZURE_LOCATION $selectedRegion
17+
Write-Host "✅ Region set. Run 'azd provision' to deploy without being prompted." -ForegroundColor Green
18+
Write-Host " To change: azd env set AZURE_LOCATION <region>" -ForegroundColor Gray
19+
}

scripts/randomize-region.sh

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
# Run this script before 'azd provision' or 'azd up' to use a random region
44
# without being prompted. Otherwise, azd will prompt you to select a region.
55

6-
REGIONS=("westus2" "westus3" "eastus2", "northcentralus")
7-
RANDOM_INDEX=$((RANDOM % 3))
8-
SELECTED_REGION="${REGIONS[$RANDOM_INDEX]}"
6+
# Check if AZURE_LOCATION is already set
7+
EXISTING_LOCATION=$(azd env get-value AZURE_LOCATION 2>/dev/null)
98

10-
echo "🎲 Randomly selected region: $SELECTED_REGION"
11-
azd env set AZURE_LOCATION "$SELECTED_REGION"
12-
echo "✅ Region set. Run 'azd provision' to deploy without being prompted."
13-
echo " To change: azd env set AZURE_LOCATION <region>"
9+
if [ -n "$EXISTING_LOCATION" ]; then
10+
echo "ℹ️ AZURE_LOCATION is already set to: $EXISTING_LOCATION"
11+
echo " Keeping existing location. To change, run: azd env set AZURE_LOCATION <region>"
12+
else
13+
REGIONS=("westus2" "westus3" "eastus2", "northcentralus")
14+
RANDOM_INDEX=$((RANDOM % 3))
15+
SELECTED_REGION="${REGIONS[$RANDOM_INDEX]}"
16+
17+
echo "🎲 Randomly selected region: $SELECTED_REGION"
18+
azd env set AZURE_LOCATION "$SELECTED_REGION"
19+
echo "✅ Region set. Run 'azd provision' to deploy without being prompted."
20+
echo " To change: azd env set AZURE_LOCATION <region>"
21+
fi

0 commit comments

Comments
 (0)