From fff07872bfe8314f7cb5ccfeb616a09a7e8fb87f 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 | 49 +++++++++++++++++++ .github/workflows/jan-docs.yml | 3 +- 2 files changed, 50 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..40bd6bc1a2 --- /dev/null +++ b/.github/workflows/clean-cloudflare-page-preview-url.yml @@ -0,0 +1,49 @@ +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: Execute JavaScript inline + uses: satackey/action-js-inline@v0.0.2 + with: + required-packages: node-fetch + script: | + const endpoint = "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/${{ matrix.project }}/deployments"; + const expirationDays = 15; + import fetch from 'node-fetch'; + + const init = { + headers: { + "Content-Type": "application/json;charset=UTF-8", + // We recommend you store the API token as a secret using the Workers dashboard or using Wrangler as documented here: https://developers.cloudflare.com/workers/wrangler/commands/#secret + "Authorization": "Bearer ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}", + }, + }; + + const response = await fetch(endpoint, init); + const deployments = await response.json(); + + for (const deployment of deployments.result) { + // Check if the deployment was created within the last x days (as defined by `expirationDays` above) + if ((Date.now() - new Date(deployment.created_on)) / 86400000 > expirationDays) { + // Delete the deployment + await fetch(`${endpoint}/${deployment.id}`, { + method: "DELETE", + headers: { + "Content-Type": "application/json;charset=UTF-8", + "Authorization": "Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}", + }, + }); + } + } + \ 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