From 9b34523e25c6993c90401a2f77266cf3708cad5a Mon Sep 17 00:00:00 2001 From: Hien To Date: Thu, 18 Jan 2024 00:27:22 +0700 Subject: [PATCH] Add CI clean cloudflare cache --- .../clean-cloudflare-page-preview-url.yml | 50 +++++++++++++++++++ .github/workflows/jan-docs.yml | 3 +- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/clean-cloudflare-page-preview-url.yml diff --git a/.github/workflows/clean-cloudflare-page-preview-url.yml b/.github/workflows/clean-cloudflare-page-preview-url.yml new file mode 100644 index 0000000000..aa34e9a137 --- /dev/null +++ b/.github/workflows/clean-cloudflare-page-preview-url.yml @@ -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/install-jq-action@v2.0.1 + - 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 \ No newline at end of file diff --git a/.github/workflows/jan-docs.yml b/.github/workflows/jan-docs.yml index 12d5992dd0..9c34fafc3b 100644 --- a/.github/workflows/jan-docs.yml +++ b/.github/workflows/jan-docs.yml @@ -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: @@ -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