From d339a4b436032744231d3f9079c1623878146a23 Mon Sep 17 00:00:00 2001 From: David Rojas Date: Wed, 27 Nov 2024 19:52:27 -0500 Subject: [PATCH 1/2] Added paginated curl to get all deployments for cloudflare pages --- .github/workflows/remove-preview-apps.yaml | 37 +++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/.github/workflows/remove-preview-apps.yaml b/.github/workflows/remove-preview-apps.yaml index 31b3b5ad..53e6805c 100644 --- a/.github/workflows/remove-preview-apps.yaml +++ b/.github/workflows/remove-preview-apps.yaml @@ -2,7 +2,6 @@ name: Delete Cloudflare Pages Preview Deployment on: pull_request: - types: [closed] jobs: delete-preview-deployment: @@ -15,16 +14,38 @@ jobs: REPO: ${{ github.repository }} PR_NO: ${{ github.event.pull_request.number }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Print branch name - run: echo "Branch name is ${{ steps.get-branch.outputs.branch }}" - name: Delete Cloudflare Pages preview deployments run: | - DEPLOY_BRANCH="${{ steps.get-branch.outputs.branch }}" - deployment_ids=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/${{ github.event.repository.name }}/deployments" \ - -H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \ - -H "Content-Type: application/json" \ - | jq -r --arg DEPLOY_BRANCH "$DEPLOY_BRANCH" '.result[] | select(.deployment_trigger.metadata.branch == $DEPLOY_BRANCH) | .id') + deployment_ids=$( + ACCOUNT_ID=${{ secrets.CF_ACCOUNT_ID }} \ + AUTH_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }} \ + PROJECT=${{ github.event.repository.name }} \ + DEPLOY_BRANCH="${{ steps.get-branch.outputs.branch }}" \ + bash -c ' + PAGE=1 + DEPLOYMENT_IDS="" + + while true; do + response=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT/deployments?page=$PAGE" \ + -H "Authorization: Bearer $AUTH_TOKEN" \ + -H "Content-Type: application/json") + + page_ids=$(echo "$response" | jq -r --arg DEPLOY_BRANCH "$DEPLOY_BRANCH" '"'"'.result[] | select(.deployment_trigger.metadata.branch == $DEPLOY_BRANCH) | .id'"'"') + DEPLOYMENT_IDS+=" $page_ids" + + count=$(echo "$response" | jq -r .result_info.count) + + if [ "$count" -eq "0" ]; then + break + fi + + PAGE=$((PAGE + 1)) + done + + echo "$DEPLOYMENT_IDS" | xargs + ' + ) for deployment_id in $deployment_ids; do echo "Deleting deployment $deployment_id" From a8554ae4c18f7df142e7d72a32167e7c7f6ef639 Mon Sep 17 00:00:00 2001 From: David Rojas Date: Wed, 27 Nov 2024 20:08:45 -0500 Subject: [PATCH 2/2] Added trigger type of closed --- .github/workflows/remove-preview-apps.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/remove-preview-apps.yaml b/.github/workflows/remove-preview-apps.yaml index 53e6805c..85b8a6c3 100644 --- a/.github/workflows/remove-preview-apps.yaml +++ b/.github/workflows/remove-preview-apps.yaml @@ -2,6 +2,7 @@ name: Delete Cloudflare Pages Preview Deployment on: pull_request: + types: [closed] jobs: delete-preview-deployment: