@@ -31,6 +31,7 @@ parameters:
3131steps :
3232 - task : AzurePowerShell@5
3333 displayName : ' $${{ parameters.displayName }}'
34+ retryCountOnTaskFailure : 20
3435 inputs :
3536 azureSubscription : $${{ parameters.serviceConnection }}
3637 azurePowerShellVersion : ' LatestVersion'
@@ -165,7 +166,6 @@ steps:
165166 Write-Error "What-If validation failed: $($_.Exception.Message)"
166167 throw
167168 }
168-
169169 } else {
170170 # Deployment Stack mode - actual deployment
171171 $stackParameters = @{
@@ -178,82 +178,57 @@ steps:
178178 Verbose = $true
179179 }
180180
181- $retryCount = 0
182- $retryMax = 20
183- $initialRetryDelay = 20
184- $retryDelayIncrement = 10
185- $finalSuccess = $false
186-
187- while ($retryCount -lt $retryMax) {
188- if ($retryCount -gt 0) {
189- $retryDelay = $initialRetryDelay + ($retryCount * $retryDelayIncrement)
190- Write-Host "Retrying deployment stack after $retryDelay seconds..." -ForegroundColor Yellow
191- Start-Sleep -Seconds $retryDelay
192- Write-Host "Retry attempt $retryCount" -ForegroundColor Yellow
193- }
194-
195- $result = $null
181+ $result = $null
196182
197- try {
198- switch ($deploymentType) {
199- "managementGroup" {
200- $targetManagementGroupId = "$${{ parameters.managementGroupId }}"
201- if ([string]::IsNullOrWhiteSpace($targetManagementGroupId)) {
202- $targetManagementGroupId = (Get-AzContext).Tenant.TenantId
203- }
204-
205- Write-Host "Creating Management Group Deployment Stack: $deploymentName" -ForegroundColor Cyan
206- $result = New-AzManagementGroupDeploymentStack @stackParameters -ManagementGroupId $targetManagementGroupId -Location "$${{ parameters.location }}"
183+ try {
184+ switch ($deploymentType) {
185+ "managementGroup" {
186+ $targetManagementGroupId = "$${{ parameters.managementGroupId }}"
187+ if ([string]::IsNullOrWhiteSpace($targetManagementGroupId)) {
188+ $targetManagementGroupId = (Get-AzContext).Tenant.TenantId
207189 }
208- "subscription" {
209- if (-not [string]::IsNullOrWhiteSpace("$${{ parameters.subscriptionId }}")) {
210- Write-Host "Setting subscription context to: $${{ parameters.subscriptionId }}" -ForegroundColor Cyan
211- Select-AzSubscription -SubscriptionId "$${{ parameters.subscriptionId }}" | Out-Null
212- }
213190
214- Write-Host "Creating Subscription Deployment Stack: $deploymentName" -ForegroundColor Cyan
215- $result = New-AzSubscriptionDeploymentStack @stackParameters -Location "$${{ parameters.location }}"
191+ Write-Host "Creating Management Group Deployment Stack: $deploymentName" -ForegroundColor Cyan
192+ $result = New-AzManagementGroupDeploymentStack @stackParameters -ManagementGroupId $targetManagementGroupId -Location "$${{ parameters.location }}"
193+ }
194+ "subscription" {
195+ if (-not [string]::IsNullOrWhiteSpace("$${{ parameters.subscriptionId }}")) {
196+ Write-Host "Setting subscription context to: $${{ parameters.subscriptionId }}" -ForegroundColor Cyan
197+ Select-AzSubscription -SubscriptionId "$${{ parameters.subscriptionId }}" | Out-Null
216198 }
217- "resourceGroup" {
218- if (-not [string]::IsNullOrWhiteSpace("$${{ parameters.subscriptionId }}")) {
219- Write-Host "Setting subscription context to: $${{ parameters.subscriptionId }}" -ForegroundColor Cyan
220- Select-AzSubscription -SubscriptionId "$${{ parameters.subscriptionId }}" | Out-Null
221- }
222199
223- Write-Host "Creating Resource Group Deployment Stack: $deploymentName" -ForegroundColor Cyan
224- $result = New-AzResourceGroupDeploymentStack @stackParameters -ResourceGroupName "$${{ parameters.resourceGroupName }}"
225- }
226- default {
227- Write-Error "Invalid deployment type: $deploymentType"
228- throw "Invalid deployment type: $deploymentType"
229- }
200+ Write-Host "Creating Subscription Deployment Stack: $deploymentName" -ForegroundColor Cyan
201+ $result = New-AzSubscriptionDeploymentStack @stackParameters -Location "$${{ parameters.location }}"
230202 }
203+ "resourceGroup" {
204+ if (-not [string]::IsNullOrWhiteSpace("$${{ parameters.subscriptionId }}")) {
205+ Write-Host "Setting subscription context to: $${{ parameters.subscriptionId }}" -ForegroundColor Cyan
206+ Select-AzSubscription -SubscriptionId "$${{ parameters.subscriptionId }}" | Out-Null
207+ }
231208
232- if ($result.ProvisioningState -eq "Succeeded") {
233- $finalSuccess = $true
234- Write-Host ""
235- Write-Host "================================================" -ForegroundColor Green
236- Write-Host "✓ Deployment Stack Succeeded: $deploymentName" -ForegroundColor Green
237- Write-Host "================================================" -ForegroundColor Green
238- Write-Host ""
239- break
240- } else {
241- Write-Warning "Deployment stack finished with state: $($result.ProvisioningState)"
242- $retryCount++
209+ Write-Host "Creating Resource Group Deployment Stack: $deploymentName" -ForegroundColor Cyan
210+ $result = New-AzResourceGroupDeploymentStack @stackParameters -ResourceGroupName "$${{ parameters.resourceGroupName }}"
243211 }
244- } catch {
245- Write-Warning "Deployment stack failed with error: $($_.Exception.Message)"
246- $retryCount++
247-
248- if ($retryCount -ge $retryMax) {
249- Write-Error "Deployment stack failed after $retryMax attempts"
250- throw
212+ default {
213+ Write-Error "Invalid deployment type: $deploymentType"
214+ throw "Invalid deployment type: $deploymentType"
251215 }
252216 }
253- }
254217
255- if (-not $finalSuccess) {
256- Write-Error "Deployment stack did not succeed after $retryMax attempts"
257- throw "Deployment stack did not succeed after $retryMax attempts"
218+ if ($result.ProvisioningState -eq "Succeeded") {
219+ $finalSuccess = $true
220+ Write-Host ""
221+ Write-Host "================================================" -ForegroundColor Green
222+ Write-Host "✓ Deployment Stack Succeeded: $deploymentName" -ForegroundColor Green
223+ Write-Host "================================================" -ForegroundColor Green
224+ Write-Host ""
225+ break
226+ } else {
227+ Write-Error "Deployment stack finished with state: $($result.ProvisioningState)"
228+ exit 1
229+ }
230+ } catch {
231+ Write-Error "Deployment stack failed with error: $($_.Exception.Message)"
232+ exit 1
258233 }
259234 }
0 commit comments