Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 5 additions & 54 deletions .github/workflows/close-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ on:
pull_request_target:
types: [closed]

permissions:
id-token: write
permissions: {}

jobs:
close:
Expand All @@ -19,57 +18,9 @@ jobs:
environment:
name: azure-preview
deployment: false
env:
AZURE_STATIC_WEB_APP_NAME: ${{ vars.AZURE_STATIC_WEB_APP_NAME }}
AZURE_RESOURCE_GROUP: ${{ vars.AZURE_RESOURCE_GROUP }}
steps:
- name: Log in to Azure
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
- uses: Azure/static-web-apps-deploy@1a947af9992250f3bc2e68ad0754c0b0c11566c9 # v1
with:
client-id: ${{ vars.AZURE_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}

- name: Close preview environment
env:
PR_NUMBER: ${{ inputs.pr || github.event.pull_request.number }}
run: |
set -euo pipefail
: "${AZURE_STATIC_WEB_APP_NAME:?Missing AZURE_STATIC_WEB_APP_NAME environment variable}"
: "${AZURE_RESOURCE_GROUP:?Missing AZURE_RESOURCE_GROUP environment variable}"
if [[ ! "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then
echo "::error::Invalid preview PR number"
exit 1
fi

get_preview_environment() {
az staticwebapp environment list \
--name "$AZURE_STATIC_WEB_APP_NAME" \
--resource-group "$AZURE_RESOURCE_GROUP" \
--query "[?name=='$PR_NUMBER'].name" --output tsv
}

environment="$(get_preview_environment)"
if [[ -z "$environment" ]]; then
echo "No preview environment exists for PR $PR_NUMBER"
exit 0
fi

# Avoid Azure CLI's subscription-scoped long-running-operation polling.
resource_id="/subscriptions/{subscriptionId}/resourceGroups/$AZURE_RESOURCE_GROUP"
resource_id+="/providers/Microsoft.Web/staticSites/$AZURE_STATIC_WEB_APP_NAME"
if ! az rest --method delete --url "$resource_id/builds/$PR_NUMBER?api-version=2024-11-01"; then
echo "::warning::Delete returned an error; checking whether preview $PR_NUMBER was removed"
fi

for _ in {1..30}; do
environment="$(get_preview_environment)"
if [[ -z "$environment" ]]; then
echo "Deleted preview environment for PR $PR_NUMBER"
exit 0
fi
sleep 5
done

echo "::error::Timed out waiting for preview $PR_NUMBER to be deleted"
exit 1
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_PREVIEW }}
action: close
app_location: /dev/null
32 changes: 3 additions & 29 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ permissions:
contents: read
pull-requests: write
actions: read
id-token: write

jobs:
deploy:
Expand All @@ -25,9 +24,6 @@ jobs:
environment:
name: azure-preview
deployment: false
env:
AZURE_STATIC_WEB_APP_NAME: ${{ vars.AZURE_STATIC_WEB_APP_NAME }}
AZURE_RESOURCE_GROUP: ${{ vars.AZURE_RESOURCE_GROUP }}
steps:
- name: Get PR/workflow run info
id: get-info
Expand Down Expand Up @@ -152,37 +148,23 @@ jobs:
with:
node-version: "24.x"

- name: Log in to Azure
if: ${{ steps.get-info.outputs.result != 'null' }}
uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0
with:
client-id: ${{ vars.AZURE_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}

- name: Deploy
id: deploy
if: ${{ steps.get-info.outputs.result != 'null' }}
env:
PR_NUMBER: ${{ fromJson(steps.get-info.outputs.result).pr }}
deployment_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_PREVIEW }}
run: |
set -euo pipefail
: "${AZURE_STATIC_WEB_APP_NAME:?Missing AZURE_STATIC_WEB_APP_NAME environment variable}"
: "${AZURE_RESOURCE_GROUP:?Missing AZURE_RESOURCE_GROUP environment variable}"
if [[ ! "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then
echo "::error::Invalid preview PR number"
exit 1
fi

deployment_token="$(az staticwebapp secrets list \
--name "$AZURE_STATIC_WEB_APP_NAME" \
--resource-group "$AZURE_RESOURCE_GROUP" \
--query properties.apiKey --output tsv)"
if [[ -z "$deployment_token" ]]; then
echo "::error::Azure returned no preview deployment token"
echo "::error::Missing preview deployment token"
exit 1
fi
echo "::add-mask::$deployment_token"
# Keep CLI configuration outside the untrusted artifact.
printf '{"configurations":{"preview":{}}}\n' > "$RUNNER_TEMP/swa-cli.config.json"
runtime_config_dir="$(mktemp -d "$RUNNER_TEMP/swa-runtime-config.XXXXXX")"
Expand All @@ -200,15 +182,7 @@ jobs:
--swa-config-location "$runtime_config_dir" \
--env "$PR_NUMBER"

hostname="$(az staticwebapp environment show \
--name "$AZURE_STATIC_WEB_APP_NAME" \
--resource-group "$AZURE_RESOURCE_GROUP" \
--environment-name "$PR_NUMBER" \
--query hostname --output tsv)"
if [[ ! "$hostname" =~ ^[A-Za-z0-9.-]+\.azurestaticapps\.net$ ]]; then
echo "::error::Azure returned an invalid hostname for preview $PR_NUMBER"
exit 1
fi
hostname="victorious-plant-05c166c10-$PR_NUMBER.centralus.5.azurestaticapps.net"
printf 'static_web_app_url=https://%s\n' "$hostname" >> "$GITHUB_OUTPUT"

- name: Comment on PR
Expand Down
Loading