-
Notifications
You must be signed in to change notification settings - Fork 26
54 lines (48 loc) · 2.18 KB
/
purge-test-resources.yaml
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
name: Purge test resources
on:
schedule:
- cron: "30 0,12 * * *"
pull_request:
types: [opened, synchronize, reopened]
branches:
- v*.*
- edge
env:
AZURE_RG_DELETE_LIST_FILE: "az_rg_list.txt"
VALID_RESOURCE_WINDOW: 6*60*60
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
purge_azure_resources:
name: Azure resources clean-ups
runs-on: [ubuntu-latest]
steps:
- name: Login to Azure
run: |
az login --service-principal \
--username ${{ secrets.AZURE_SP_TESTS_APPID }} \
--password ${{ secrets.AZURE_SP_TESTS_PASSWORD }} \
--tenant ${{ secrets.AZURE_SP_TESTS_TENANTID }}
az account set --subscription ${{ secrets.AZURE_SUBSCRIPTIONID_TESTS }}
- name: List Test Resource Groups
run: |
echo "## Test resource group list" >> $GITHUB_STEP_SUMMARY
az group list --query "[?starts_with(name, 'samplestest-')].{Name:name, creationTime:tags.creationTime}" -o json > resource_groups.json
current_time=$(date +%s)
hours_ago=$((current_time - ${{ env.VALID_RESOURCE_WINDOW }}))
echo "Hours ago: $hours_ago"
jq -r '.[] | select(.creationTime == null or .creationTime < '$hours_ago') | .Name' resource_groups.json > ${{ env.AZURE_RG_DELETE_LIST_FILE}}
jq -r '.[] | {name: .Name, creationTime: .creationTime // "None"}' resource_groups.json > $GITHUB_STEP_SUMMARY
- name: Delete Azure Resource Groups
run: |
echo "## Deleting resource group list" >> $GITHUB_STEP_SUMMARY
cat ${{ env.AZURE_RG_DELETE_LIST_FILE}} | while read -r line
do
echo " * $line" >> $GITHUB_STEP_SUMMARY
az group delete --resource-group $line --yes --verbose
done
- name: Create GitHub issue on failure
if: failure() && github.event_name == 'schedule'
run: |
gh issue create --title "Samples purge test resources failed" \
--body "Test failed on ${{ github.repository }}. See [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details." \
--repo ${{ github.repository }}