-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcleanup-resources.yml
More file actions
60 lines (53 loc) · 1.87 KB
/
cleanup-resources.yml
File metadata and controls
60 lines (53 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
parameters:
- name: regions
type: object
default: {}
- name: cloud
type: string
- name: terraform_arguments
type: string
default: ''
- name: retry_attempt_count
type: number
default: 3
steps:
- template: /steps/terraform/run-command.yml
parameters:
command: destroy
arguments: ${{ parameters.terraform_arguments }}
regions: ${{ parameters.regions }}
cloud: ${{ parameters.cloud }}
retry_attempt_count: ${{ parameters.retry_attempt_count }}
skip_resource_deletion: "${SKIP_RESOURCE_DELETION}"
- script: |
set -eu
if [[ "${SKIP_RESOURCE_DELETION:-false}" == "true" && "$BUILD_REASON" == "Manual" ]]; then
echo "##vso[task.logissue type=warning;] SKIP_RESOURCE_DELETION is set. Skipping resource group deletion. Please delete manually:
az group delete --name $RUN_ID --yes"
exit 0
fi
echo "Delete resource group $RUN_ID"
az group delete --name $RUN_ID --yes --no-wait
# Poll until the resource group is fully deleted or timeout after 20 minutes
TIMEOUT=1200
INTERVAL=30
WAIT_TIME=0
while [ $WAIT_TIME -lt $TIMEOUT ]; do
if ! az group show --name "$RUN_ID" &>/dev/null; then
echo "Resource group $RUN_ID deleted successfully."
exit 0
fi
echo "Resource group $RUN_ID still exists. Waiting... ($WAIT_TIME/$TIMEOUT seconds elapsed)"
sleep $INTERVAL
WAIT_TIME=$((WAIT_TIME + INTERVAL))
done
echo "##vso[task.logissue type=error;] Resource group $RUN_ID was not deleted within $TIMEOUT seconds."
exit 1
displayName: "Destroy Resource Group"
condition: and(${{ eq(parameters.cloud, 'azure') }}, ne(variables['SKIP_RESOURCE_MANAGEMENT'], 'true'))
env:
BUILD_REASON: $(Build.Reason)
- ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
- template: /steps/collect-terraform-operation-metadata.yml
parameters:
cloud: ${{ parameters.cloud }}