Skip to content

Cache Invalidation

Cache Invalidation #11

Workflow file for this run

name: Cache Invalidation
on:
workflow_run:
workflows:
- "pages-build-deployment"
types: [completed]
jobs:
cloudflare:
name: "CloudFlare"
runs-on: ubuntu-latest
steps:
- name: Checkout Prod
uses: actions/checkout@v2
with:
ref: production
- name: "Invalidate the cache"
shell: bash
run: |
purgeaurl(){
HTTP_RESPONSE=$(curl --silent --show-error -X POST "https://api.cloudflare.com/client/v4/zones/${{ secrets.CF_ZONE }}/purge_cache" -H "Authorization: Bearer ${{ secrets.CF_API_TOKEN }}" -H "Content-Type:application/json" -w "HTTP_STATUS:%{http_code}" --data "{\"files\":[\"${1}\"]}")
HTTP_BODY=$(echo "${HTTP_RESPONSE}" | sed -E 's/HTTP_STATUS\:[0-9]{3}$//')
HTTP_STATUS=$(echo "${HTTP_RESPONSE}" | tr -d '\n' | sed -E 's/.*HTTP_STATUS:([0-9]{3})$/\1/')
if [ "${HTTP_STATUS}" -eq "200" ]; then
SUCCESS=$(echo ${HTTP_BODY} | python3 -c "import sys, json;print(json.load(sys.stdin)['success'])")
if [ "${SUCCESS}" = "True" ]; then
echo "Purged $1"
else
echo "Failed to purge $1 => ${HTTP_BODY}"
exit 1
fi
else
echo "Failed to purge $1 => ($HTTP_STATUS): ${HTTP_BODY}"
exit 1
fi
}
export -f purgeaurl
git ls-files | sed 's/^/https:\/\/buddhistuniversity.net\/patanjali-yoga-sutra\//g' | sed 's/^\(.*\)\/index.html/\1\n\1\/\n\1\/index.html/g' | sed 's/^\(.*\).html/\1.html\n\1/g' | xargs -P 100 -I {} bash -c 'purgeaurl "{}"'