Tyger-Delete-PR-Envs #1913
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |