Skip to content

Add CI clean cloudflare cache #9

Add CI clean cloudflare cache

Add CI clean cloudflare cache #9

name: "Clean old cloudflare pages preview urls and nightly build"
on:
schedule:
- cron: "0 0 * * *" # every day at 00:00
workflow_dispatch:
push:
branches:
- feature/clean-cloudflare-page-and-r2
jobs:
clean-cloudflare-pages-preview-urls:
strategy:
matrix:
project: ["jan", "nitro"]
runs-on: ubuntu-latest
steps:
- name: Install jq
uses: dcarbone/[email protected]
- name: Delete Pages Preview URLs
run: |
# Configuration
CF_ACCOUNT_ID="${{ secrets.CLOUDFLARE_ACCOUNT_ID }}"
CF_API_TOKEN="${{ secrets.CLOUDFLARE_API_TOKEN }}"
PROJECT_NAME="${{ matrix.project }}"
ENDPOINT="https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT_ID/pages/projects/$PROJECT_NAME/deployments"
EXPIRATION_DAYS=15
# Fetch the list of deployments
response=$(curl -s "$ENDPOINT" -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $CF_API_TOKEN")
# Loop through each deployment
echo "$response" | tr '\r\n' ' ' | jq -c '.result[]' | while read -r deployment; do
# Extract creation date and deployment ID
created_on=$(echo -e "$deployment" | jq -r '.created_on')
deployment_id=$(echo -e "$deployment" | jq -r '.id')
# Calculate the age of the deployment
created_on_timestamp=$(date -d "$created_on" +%s)
current_timestamp=$(date +%s)
age_days=$(( (current_timestamp - created_on_timestamp) / 86400 ))
# Delete the deployment if it is older than EXPIRATION_DAYS
if [ "$age_days" -gt "$EXPIRATION_DAYS" ]; then
curl -X DELETE "$ENDPOINT/$deployment_id" \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer $CF_API_TOKEN"
echo "Deleted deployment: $deployment_id"
fi
done