Skip to content

Commit

Permalink
Add CI clean cloudflare cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hientominh committed Jan 17, 2024
1 parent f5dca81 commit 9b34523
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/clean-cloudflare-page-preview-url.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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" | jq -c '.result[]' | while read -r deployment; do
# Extract creation date and deployment ID
created_on=$(echo "$deployment" | jq -r '.created_on')
deployment_id=$(echo "$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
3 changes: 1 addition & 2 deletions .github/workflows/jan-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
deploy:
name: Deploy to GitHub Pages
env:
CLOUDFLARE_ACCOUNT_ID: 9707100ef42a1a25bd70e3ee2137bd0e
CLOUDFLARE_PROJECT_NAME: jan
runs-on: ubuntu-latest
permissions:
Expand Down Expand Up @@ -66,7 +65,7 @@ jobs:
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ env.CLOUDFLARE_ACCOUNT_ID }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ env.CLOUDFLARE_PROJECT_NAME }}
directory: ./docs/build
branch: dev
Expand Down

0 comments on commit 9b34523

Please sign in to comment.