Skip to content

Add CI clean cloudflare cache #10

Add CI clean cloudflare cache

Add CI clean cloudflare cache #10

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 requests
run: |
python3 -m pip install requests
- name: Python Inline script
shell: python3
run: |
import requests
from datetime import datetime, timedelta
# Configuration
endpoint = f"https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/${{ matrix.project }}/deployments"
expiration_days = 15
headers = {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": f"Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}"
}
# Fetch the list of deployments
response = requests.get(endpoint, headers=headers)
deployments = response.json()
for deployment in deployments['result']:
# Calculate the age of the deployment
created_on = datetime.fromisoformat(deployment['created_on'].rstrip('Z'))
if (datetime.now() - created_on).days > expiration_days:
# Delete the deployment
delete_response = requests.delete(f"{endpoint}/{deployment['id']}", headers=headers)
if delete_response.status_code == 200:
print(f"Deleted deployment: {deployment['id']}")
else:
print(f"Failed to delete deployment: {deployment['id']}")