Skip to content

Instantly share code, notes, and snippets.

@jsiegmund
Created June 23, 2025 13:35
Show Gist options
  • Save jsiegmund/70606f96dc37892aa415b0147405915a to your computer and use it in GitHub Desktop.
Save jsiegmund/70606f96dc37892aa415b0147405915a to your computer and use it in GitHub Desktop.
Remove static webapp preview URL from app registration
- name: Remove Preview URL from App Registration
if: github.event_name == 'pull_request' && github.event.action == 'closed'
run: |
# Login to Azure
az login --service-principal -u ${{ vars.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ vars.AZURE_TENANT_ID }} --allow-no-subscriptions
APP_ID=${{ vars.APP_REGISTRATION_ID }}
# Get the preview URL for this PR
PR_NUMBER=${{ github.event.pull_request.number }}
DEFAULT_HOSTNAME=$(az staticwebapp show --name ${{ vars.STATIC_WEBAPP_NAME }} --resource-group ${{ vars.STATIC_WEBAPP_RESOURCE_GROUP }} --query "defaultHostname" --subscription ${{ vars.STATIC_WEBAPP_SUBSCRIPTION }} -o tsv)
REGION="westeurope" # Set your region here
# Extract app name and random part
APP_NAME=$(echo "$DEFAULT_HOSTNAME" | cut -d. -f1)
RANDOM_PART=$(echo "$DEFAULT_HOSTNAME" | cut -d. -f2)
PREVIEW_HOSTNAME="${APP_NAME}-${PR_NUMBER}.${REGION}.${RANDOM_PART}.azurestaticapps.net"
CLOSED_PR_URL="https://$PREVIEW_HOSTNAME/login-redirect"
# Get current SPA redirect URIs
CURRENT_URIS=$(az rest --method GET --url "https://graph.microsoft.com/v1.0/applications/$APP_ID" --query "spa.redirectUris" -o tsv)
# Remove the closed PR URI and build updated list
UPDATED_URIS=$(echo "$CURRENT_URIS" | tr " " "\n" | grep -v "$CLOSED_PR_URL" | grep -v '^$' | jq -R . | jq -s .)
if [ "$UPDATED_URIS" != "[]" ]; then
# Update SPA redirect URIs using Microsoft Graph
az rest --method PATCH \
--url "https://graph.microsoft.com/v1.0/applications/$APP_ID" \
--headers 'Content-Type=application/json' \
--body "{\"spa\": {\"redirectUris\": $UPDATED_URIS }}"
else
echo "No SPA redirect URIs remain, skipping update."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment