-
Notifications
You must be signed in to change notification settings - Fork 0
58 lines (53 loc) · 2.25 KB
/
dapr-test-azure-cleanup.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
#
# Copyright 2021 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Required secrets:
# - AZURE_CREDENTIALS: JSON object containing the Azure service principal credentials. Docs: https://github.com/Azure/login#configure-a-service-principal-with-a-secret
name: Cleanup Azure test resources
on:
# Run every 6 hours
schedule:
- cron: '30 */6 * * *'
# Manual trigger
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Login to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: "Run cleanup script"
run: |
DELETE_BEFORE_DATE=$(date --date '4 hours ago' +'%s')
QUERY="[?(starts_with(name, 'Dapr-Perf-') || starts_with(name, 'Dapr-E2E-') && properties.provisioningState != 'Deleting' && tags.date != '')].{name: name, id: id, date: tags.date}"
LINES=$(az group list --query "$QUERY" | jq -c ".[]")
(
# Run in a sub-shell so we can modify IFS safely
IFS=$'\n'
for line in $(az group list --query "$QUERY" | jq -c '.[]')
do
RG_DATE_STR=$(echo "$line" | jq -r ".date")
if [ "$RG_DATE_STR" != "null" ]; then
RG_DATE=$(date --date "$RG_DATE_STR" +'%s')
RG_NAME=$(echo "$line" | jq -r ".name")
if [ $RG_DATE -lt $DELETE_BEFORE_DATE ] ; then
echo "DELETING Resource Group ${RG_NAME} that was deployed on ${RG_DATE_STR}"
az group delete --no-wait --yes --name "${RG_NAME}" || true
else
echo "Keeping Resource Group ${RG_NAME} that was deployed on ${RG_DATE_STR}"
fi
fi
done
)
shell: bash