-
Notifications
You must be signed in to change notification settings - Fork 11
71 lines (56 loc) · 2.13 KB
/
delete-pr-environments.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
name: Tyger-Delete-PR-Envs
on:
schedule:
- cron: "0 */6 * * *"
workflow_dispatch:
permissions:
id-token: write
contents: read
pull-requests: read
env:
AZURE_CLIENT_ID: 789b8572-1fae-4a5f-b376-6d9d14651245
AZURE_TENANT_ID: 72f988bf-86f1-41af-91ab-2d7cd011db47
AZURE_SUBSCRIPTION_ID: 87d8acb3-5176-4651-b457-6ab9cefd8e3d
CAN_ACCESS_SECRETS: ${{ secrets.CAN_ACCESS_SECRETS }}
jobs:
delete-pr-envs:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Log in to Azure
uses: ./.github/actions/login-azure
- uses: actions/setup-go@v4
with:
go-version-file: cli/go.mod
cache-dependency-path: cli/go.sum
- name: "Delete PR Envs"
run: |
set -eo pipefail
mapfile -t active_prs < <(curl -sS -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/microsoft/tyger/pulls" | jq -r .[].number)
for pr in "${active_prs[@]}"; do
echo "Found active PR $pr"
done
# Creating a new array to store modified numbers
declare -a active_pr_environment_ids
for pr_number in "${active_prs[@]}"; do
# Reuse environment names so that we are not creating new Let's Encrypt certificate requests for each PR
new_value=$(( (pr_number % 15) + 38 ))
active_pr_environment_ids+=("$new_value")
done
prefix='tyger-gpr'
resource_groups=$(az group list --query "[?starts_with(name, '${prefix}')]" | jq ".[] | select(.name | test(\"^${prefix}[0-9]+$\")) | .name" -r)
for rg_name in $resource_groups; do
deployed_id="${rg_name#"$prefix"}"
if [[ ! " ${active_pr_environment_ids[*]} " =~ ${deployed_id} ]]; then
echo "deleting $rg_name"
TYGER_ENVIRONMENT_NAME="$rg_name" make remove-environment
else
echo "Not deleting $rg_name because there is an active PR for it"
fi
done