Delete old Azure Functions #6
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: Delete old Azure Functions | |
on: | |
workflow_dispatch: | |
inputs: | |
group: | |
description: 'Which group?' | |
type: 'choice' | |
options: | |
- 'prisma-e2e-linux' | |
- 'prisma-e2e-windows' | |
search: | |
description: 'String matching the function name, e.g. "-2024-01-"' | |
required: true | |
default: '-2024-01-' | |
jobs: | |
cleanup: | |
runs-on: 'ubuntu-latest' | |
env: | |
AZURE_SP_TENANT: ${{ vars.AZURE_SP_TENANT }} | |
AZURE_SP_PASSWORD: ${{ secrets.AZURE_SP_PASSWORD }} | |
AZURE_SP_NAME: ${{ vars.AZURE_SP_NAME }} | |
AZ_DELETE_GROUP: ${{ inputs.group }} | |
AZ_DELETE_SEARCH: ${{ inputs.search }} | |
steps: | |
- name: Azure CLI script | |
uses: azure/cli@v2 | |
with: | |
azcliversion: latest | |
inlineScript: | | |
# Debug | |
# az --version | |
# az account show | |
echo "Logging in to Azure..." | |
az login --service-principal -u "$AZURE_SP_NAME" -p "$AZURE_SP_PASSWORD" --tenant "$AZURE_SP_TENANT" | |
# From https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746 | |
# This is free and unencumbered software released into the public domain. | |
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. | |
# In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# For more information, please refer to https://unlicense.org | |
# | |
# Retry a command up to a specific numer of times until it exits successfully, | |
# with exponential back off. | |
# | |
# $ retry 5 echo Hello | |
# Hello | |
# | |
# $ retry 5 false | |
# Retry 1/5 exited 1, retrying in 60 seconds... | |
# Retry 2/5 exited 1, retrying in 120 seconds... | |
# Retry 3/5 exited 1, retrying in 240 seconds... | |
# Retry 4/5 exited 1, retrying in 480 seconds... | |
# Retry 5/5 exited 1, no more retries left. | |
# | |
function retry { | |
# This was added for logs | |
printf "Running command: retry %s\n" "$*" | |
local retries=$1 | |
shift | |
local count=0 | |
until "$@"; do | |
exit=$? | |
wait=$((2 ** $count * 60)) | |
count=$(($count + 1)) | |
if [ $count -lt $retries ]; then | |
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." | |
sleep $wait | |
else | |
echo "Retry $count/$retries exited $exit, no more retries left." | |
return $exit | |
fi | |
done | |
return 0 | |
} | |
function deleteAZResources { | |
echo "Fetching Azure functions matching the group and search..." | |
# List functions to be deleted | |
FIDS=$(az functionapp list --resource-group $AZ_DELETE_GROUP --output tsv --query "[?contains(@.name, '$AZ_DELETE_SEARCH')==\`true\`].id") | |
if [ "$FIDS" != "" ]; then | |
echo "The following functions will be deleted: $FIDS" | |
az functionapp delete --verbose --ids $FIDS | |
else | |
echo "No functions to delete" | |
fi | |
return 0 | |
} | |
MAX_RETRIES=5 | |
retry $MAX_RETRIES deleteAZResources |