Skip to content

reduce time window for recent deployment check in clear cache workflo… #5

reduce time window for recent deployment check in clear cache workflo…

reduce time window for recent deployment check in clear cache workflo… #5

Workflow file for this run

name: Clear Cloudflare Cache After Deployments
on:
push: {} # For testing - change to specific branches later
jobs:
clear-cache:
runs-on: ubuntu-latest
steps:
- name: Poll deployments
id: poll-deployments
env:
DIGITALOCEAN_TOKEN: ${{ secrets.DIGITALOCEAN_TOKEN }}
run: |
echo "Initial 30 second delay..."
sleep 30
APP_IDS="52a99a96-7e6a-4258-b523-23d615c05571,7917c4fa-2426-4f51-b8cd-15a680fcaf1f,7f68aa01-e022-4244-81a4-0e5a50893703,476726eb-3be5-4b53-abeb-fba6db77786e"
CURRENT_COMMIT="${{ github.sha }}"
echo "Current commit: $CURRENT_COMMIT"
for attempt in {1..20}; do
echo "Check attempt $attempt of 20..."
for app_id in ${APP_IDS//,/ }; do
echo "Checking app: $app_id"
response=$(curl -s \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
-H "Content-Type: application/json" \
"https://api.digitalocean.com/v2/apps/$app_id/deployments?page=1&per_page=1")
created_at=$(echo $response | jq -r '.deployments[0].created_at')
status=$(echo $response | jq -r '.deployments[0].phase')
commit_hash=$(echo $response | jq -r '.deployments[0].commit_hash')
echo "Latest deployment status: $status (created: $created_at, commit: $commit_hash)"
# Check if deployment is from last 10 minutes, is active, and matches our commit
if [ "$status" = "ACTIVE" ] && \
[ $(( $(date +%s) - $(date -d "$created_at" +%s) )) -lt 300 ] && \
[ "$commit_hash" = "$CURRENT_COMMIT" ]; then
echo "Found recent successful deployment matching current commit"
echo "status=success" >> $GITHUB_OUTPUT
exit 0
fi
done
if [ $attempt -eq 20 ]; then
echo "Timeout after 10 minutes - no matching successful deployments found"
echo "status=timeout" >> $GITHUB_OUTPUT
exit 1
fi
echo "No matching successful deployments yet, waiting 30 seconds..."
sleep 30
done
- name: Purge Cloudflare cache
if: steps.poll-deployments.outputs.status == 'success'
env:
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
echo "Purging Cloudflare cache..."
response=$(curl -s -X POST \
"https://api.cloudflare.com/client/v4/zones/04102fd3a28043d9d40a2688282d688a/purge_cache" \
-H "Authorization: Bearer $CLOUDFLARE_TOKEN" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}')
if [ "$(echo $response | jq -r '.success')" = "true" ]; then
echo "Successfully purged Cloudflare cache"
else
echo "Failed to purge cache:"
echo $response
exit 1
fi