Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added paginated curl to get all deployments for cloudflare pages #355

Merged
merged 2 commits into from
Nov 28, 2024
Merged
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
36 changes: 29 additions & 7 deletions .github/workflows/remove-preview-apps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,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"
Expand Down