-
Notifications
You must be signed in to change notification settings - Fork 84
105 lines (96 loc) · 3.76 KB
/
housekeep.yml
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
name: Housekeeping
on:
schedule:
- cron: "11 3 * * *" # daily at 3:11
workflow_dispatch:
permissions:
id-token: write
jobs:
cancelsubscriptions:
name: Cancel subscriptions
runs-on: ubuntu-latest
steps:
- name: Azure login
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
allow-no-subscriptions: true
- name: Azure cancel subscriptions
uses: azure/CLI@v2
continue-on-error: true
with:
inlineScript: |
az config set extension.use_dynamic_install=yes_without_prompt
echo "==> Cancelling the following subscriptions:"
az account subscription list | jq -r '.[] | select(.state == "Enabled") | select(.displayName | test("^testdeploy.*")) | .subscriptionId'
az account subscription list | jq -r '.[] | select(.state == "Enabled") | select(.displayName | test("^testdeploy.*")) | .subscriptionId' | xargs -n1 -I% az account subscription cancel --yes --id %
echo "==> Done cancelling subscriptions"
- name: Azure logout
uses: azure/CLI@v2
if: always()
with:
inlineScript: |
az logout
az cache purge
az account clear
deletealiases:
name: Delete subscription aliases
runs-on: ubuntu-latest
steps:
- name: Azure login
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
allow-no-subscriptions: true
- name: Azure delete subscription aliases
uses: azure/CLI@v2
continue-on-error: true
with:
inlineScript: |
az config set extension.use_dynamic_install=yes_without_prompt
echo "==> Deleting the following subscription aliases:"
az rest --method GET --uri '/providers/Microsoft.Subscription/aliases/?api-version=2021-10-01' | jq -r '.value[] | select(.name | test("^testdeploy")) | .name'
az rest --method GET --uri '/providers/Microsoft.Subscription/aliases/?api-version=2021-10-01' | jq -r '.value[] | select(.name | test("^testdeploy")) | .name' | xargs -n1 -I% az rest --method DELETE --uri '/providers/Microsoft.Subscription/aliases/%?api-version=2021-10-01'
echo "==> Done deleting subscription aliases"
- name: Azure logout
uses: azure/CLI@v2
if: always()
with:
inlineScript: |
az logout
az cache purge
az account clear
deleteresourcegroups:
name: Delete resource groups
runs-on: ubuntu-latest
steps:
- name: Azure login
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_EXISTING_SUBSCRIPTION_ID }}
- name: Azure delete resourcegroups
uses: azure/CLI@v2
continue-on-error: true
with:
inlineScript: |
if [ ! $(az lock list | jq 'length') -eq 0 ]; then
echo "==> removing locks"
az lock list | jq '.[].id' | xargs az lock delete --ids
fi
echo "==> Deleting the following resource groups:"
az group list | jq -r '.[] | select(.name | test("^testdeploy-")) | .name'
az group list | jq -r '.[] | select(.name | test("^testdeploy-")) | .name' | xargs -I% -P5 az group delete --yes --name %
echo "==> Done deleting resource groups"
- name: Azure logout
uses: azure/CLI@v2
if: always()
with:
inlineScript: |
az logout
az cache purge
az account clear