I have a pwsh script that I am triggering from a GitHub actions workflow. It is supposed to delete resource groups that are found in a subscription (output of previous step is the required resource groups). The problem I find is, Azure is sometimes too fast for itself, so it tries to delete a resource group that has already been deleted. I need a way to prevent the step from failing (essentially not wanting that the az cli sets the error code to 1), for this specific occurence.
This is the code I tried (since try catch doesn't work with az cli, had to do !$?). I basically write the error to txt (I am aware it's not the best solution) and then check for the specific error that occurs. It all works fine, but the $ErrorActionPreference = "Continue" doesn't help in preventing the az cli failing the step.
param(
[Parameter()]
[string]$rgName
)
$ErrorActionPreference = "Stop"
$retries = 5
$sleepInSec = 30
for ($i = 1; $i -le $retries; $i++) {
az group delete --yes --name $rgName 2>fail.txt
if (!$?) {
$errorMsg = Get-Content "fail.txt"
if ($errorMsg -like "*ERROR: (ResourceGroupNotFound)*") {
Write-Host "RG already deleted or never present. Skipping..."
$ErrorActionPreference = "Continue"
break
}
elseif ($i -eq $retries) {
Write-Error "Maximum retries reached ($retries). Need manual inspection. Error: $errorMsg"
$ErrorActionPreference = "Stop"
exit 1
} else {
Write-Host "Failed $i. run. Waiting for $sleepInSec seconds before retrying."
Start-Sleep -Seconds $sleepInSec
continue
}
}
break
}
Any ideas on how to not fail the step on certain errors from az cli?