Merge pull request #28 from evilru/hotfix/nc-css #110
This file contains hidden or 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
| on: | |
| push: | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-22.04 | |
| steps: | |
| - run: | | |
| set -euo pipefail | |
| # provide secrets | |
| ACCOUNT_ID="${{ secrets.CLOUDFLARE_ACCOUNT_ID }}" | |
| PROJECT_NAME="${{ vars.CLOUDFLARE_PROJECT_NAME }}" | |
| API_TOKEN="${{ secrets.CLOUDFLARE_API_TOKEN }}" | |
| API_URL="https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME/deployments" | |
| for entry in "production:10" "preview:50"; do | |
| env="${entry%%:*}" | |
| keep="${entry##*:}" | |
| echo "Processing environment: $env (keep last $keep deployments)" | |
| deployments=$(curl -sS -H "Authorization: Bearer $API_TOKEN" "$API_URL?env=$env") | |
| if ! echo "$deployments" | jq -e '.success == true' >/dev/null 2>&1; then | |
| echo "Cloudflare API error for env $env:" | |
| echo "$deployments" | jq '.errors, .messages' || echo "$deployments" | |
| exit 1 | |
| fi | |
| deployment_ids=$(echo "$deployments" | jq -r --argjson keep "$keep" '(.result // []) | sort_by(.created_on) | reverse | .[$keep:] | .[].id' 2>/dev/null || echo "") | |
| if [ -z "$deployment_ids" ]; then | |
| echo "No $env deployments to delete." | |
| continue | |
| fi | |
| for deployment in $deployment_ids; do | |
| if [ -n "$deployment" ]; then | |
| echo "Deleting $env deployment $deployment" | |
| force_param="" | |
| [ "$env" = "preview" ] && force_param="?force=true" | |
| del=$(curl -sS -w "\n%{http_code}" -X DELETE -H "Authorization: Bearer $API_TOKEN" "$API_URL/$deployment$force_param") | |
| code=$(echo "$del" | tail -n1) | |
| body=$(echo "$del" | sed '$d') | |
| if [ "$code" -ge 400 ]; then | |
| echo "Delete failed ($code) for $deployment:" | |
| echo "$body" | jq '.errors? // .' | |
| exit 1 | |
| else | |
| echo "Deleted $deployment" | |
| fi | |
| fi | |
| done | |
| done |