-
Notifications
You must be signed in to change notification settings - Fork 134
52 lines (47 loc) · 2 KB
/
clean-cloudflare-page-preview-url-and-r2.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
name: "Clean old cloudflare pages preview urls and nightly build"
on:
schedule:
- cron: "0 0 * * *" # every day at 00:00
workflow_dispatch:
jobs:
clean-cloudflare-pages-preview-urls:
strategy:
matrix:
project: ["cortex-docs"]
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: install requests
run: |
python3 -m pip install requests pytz tqdm
- name: Python Inline script
uses: jannekem/run-python-script-action@v1
with:
script: |
import requests
from datetime import datetime, UTC
from pytz import timezone
from tqdm import tqdm
# Configuration
endpoint = "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/${{ matrix.project }}/deployments"
expiration_days = 3
headers = {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": "Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}"
}
utc_tz = timezone('UTC')
# Fetch the list of deployments
response = requests.get(endpoint, headers=headers)
deployments = response.json()
for deployment in tqdm(deployments['result']):
# Calculate the age of the deployment
created_on = datetime.strptime(deployment['created_on'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=utc_tz)
if (datetime.now(UTC) - 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']}")