Feature add schedule clean cloudflare page and r2 (#1653)

* Add notify for manual build if publish to R2

* Add CI clean cloudflare cache

* Update jan-docs CI

* Add job clean cloudflare r2

---------

Co-authored-by: Hien To <tominhhien97@gmail.com>
This commit is contained in:
hiento09 2024-01-18 09:31:39 +07:00 committed by GitHub
parent 6a4ce91870
commit c0c4472c9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 108 additions and 9 deletions

View File

@ -0,0 +1,75 @@
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: ["jan", "nitro"]
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']}")
clean-cloudflare-r2:
runs-on: ubuntu-latest
environment: production
steps:
- name: install-aws-cli-action
uses: unfor19/install-aws-cli-action@v1
- name: Delete object older than 7 days
run: |
# Get the list of objects in the 'latest' folder
OBJECTS=$(aws s3api list-objects --bucket ${{ secrets.CLOUDFLARE_R2_BUCKET_NAME }} --query 'Contents[?LastModified<`'$(date -d "$current_date -30 days" -u +"%Y-%m-%dT%H:%M:%SZ")'`].{Key: Key}' --endpoint-url https://${{ secrets.CLOUDFLARE_ACCOUNT_ID }}.r2.cloudflarestorage.com | jq -c .)
# Create a JSON file for the delete operation
echo "{\"Objects\": $OBJECTS, \"Quiet\": false}" > delete.json
# Delete the objects
echo q | aws s3api delete-objects --bucket ${{ secrets.CLOUDFLARE_R2_BUCKET_NAME }} --delete file://delete.json --endpoint-url https://${{ secrets.CLOUDFLARE_ACCOUNT_ID }}.r2.cloudflarestorage.com
# Remove the JSON file
rm delete.json
env:
AWS_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: auto
AWS_EC2_METADATA_DISABLED: "true"

View File

@ -22,7 +22,6 @@ jobs:
deploy: deploy:
name: Deploy to GitHub Pages name: Deploy to GitHub Pages
env: env:
CLOUDFLARE_ACCOUNT_ID: 9707100ef42a1a25bd70e3ee2137bd0e
CLOUDFLARE_PROJECT_NAME: jan CLOUDFLARE_PROJECT_NAME: jan
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions: permissions:
@ -62,14 +61,25 @@ jobs:
working-directory: docs working-directory: docs
- name: Publish to Cloudflare Pages PR Preview and Staging - name: Publish to Cloudflare Pages PR Preview and Staging
if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || (github.event_name == 'pull_request' && github.event.pull_request.base.ref == 'main') || (github.event_name == 'push' && github.ref == 'refs/heads/dev') || (github.event_name == 'pull_request' && github.event.pull_request.base.ref == 'dev') if: github.event_name == 'push' && github.ref == 'refs/heads/dev'
uses: cloudflare/pages-action@v1 uses: cloudflare/pages-action@v1
with: with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ env.CLOUDFLARE_ACCOUNT_ID }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ env.CLOUDFLARE_PROJECT_NAME }}
directory: ./docs/build
branch: main
# Optional: Enable this if you want to have GitHub Deployments triggered
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
- name: Publish to Cloudflare Pages PR Preview and Staging
if: github.event_name == 'pull_request'
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ env.CLOUDFLARE_PROJECT_NAME }} projectName: ${{ env.CLOUDFLARE_PROJECT_NAME }}
directory: ./docs/build directory: ./docs/build
branch: dev
# Optional: Enable this if you want to have GitHub Deployments triggered # Optional: Enable this if you want to have GitHub Deployments triggered
gitHubToken: ${{ secrets.GITHUB_TOKEN }} gitHubToken: ${{ secrets.GITHUB_TOKEN }}
id: deployCloudflarePages id: deployCloudflarePages

View File

@ -19,14 +19,17 @@ jobs:
if: github.event_name == 'workflow_dispatch' if: github.event_name == 'workflow_dispatch'
outputs: outputs:
public_provider: ${{ steps.set-public-provider.outputs.public_provider }} public_provider: ${{ steps.set-public-provider.outputs.public_provider }}
ref: ${{ steps.set-public-provider.outputs.ref }}
steps: steps:
- name: Set public provider - name: Set public provider
id: set-public-provider id: set-public-provider
run: | run: |
if [ ${{ github.event == 'workflow_dispatch' }} ]; then if [ ${{ github.event == 'workflow_dispatch' }} ]; then
echo "::set-output name=public_provider::${{ github.event.inputs.public_provider }}" echo "::set-output name=public_provider::${{ github.event.inputs.public_provider }}"
echo "::set-output name=ref::${{ github.ref }}"
else else
echo "::set-output name=public_provider::cloudflare-r2" echo "::set-output name=public_provider::cloudflare-r2"
echo "::set-output name=ref::refs/heads/dev"
fi fi
# Job create Update app version based on latest release tag with build number and save to output # Job create Update app version based on latest release tag with build number and save to output
get-update-version: get-update-version:
@ -37,7 +40,7 @@ jobs:
needs: [get-update-version, set-public-provider] needs: [get-update-version, set-public-provider]
secrets: inherit secrets: inherit
with: with:
ref: refs/heads/dev ref: ${{ needs.set-public-provider.outputs.ref }}
public_provider: ${{ needs.set-public-provider.outputs.public_provider }} public_provider: ${{ needs.set-public-provider.outputs.public_provider }}
new_version: ${{ needs.get-update-version.outputs.new_version }} new_version: ${{ needs.get-update-version.outputs.new_version }}
@ -46,7 +49,7 @@ jobs:
secrets: inherit secrets: inherit
needs: [get-update-version, set-public-provider] needs: [get-update-version, set-public-provider]
with: with:
ref: refs/heads/dev ref: ${{ needs.set-public-provider.outputs.ref }}
public_provider: ${{ needs.set-public-provider.outputs.public_provider }} public_provider: ${{ needs.set-public-provider.outputs.public_provider }}
new_version: ${{ needs.get-update-version.outputs.new_version }} new_version: ${{ needs.get-update-version.outputs.new_version }}
@ -56,17 +59,28 @@ jobs:
secrets: inherit secrets: inherit
needs: [get-update-version, set-public-provider] needs: [get-update-version, set-public-provider]
with: with:
ref: refs/heads/dev ref: ${{ needs.set-public-provider.outputs.ref }}
public_provider: ${{ needs.set-public-provider.outputs.public_provider }} public_provider: ${{ needs.set-public-provider.outputs.public_provider }}
new_version: ${{ needs.get-update-version.outputs.new_version }} new_version: ${{ needs.get-update-version.outputs.new_version }}
noti-discord-nightly-and-update-url-readme: noti-discord-nightly-and-update-url-readme:
needs: [build-macos, build-windows-x64, build-linux-x64, get-update-version] needs: [build-macos, build-windows-x64, build-linux-x64, get-update-version, set-public-provider]
secrets: inherit secrets: inherit
if: github.event_name == 'schedule' if: github.event_name == 'schedule'
uses: ./.github/workflows/template-noti-discord-and-update-url-readme.yml uses: ./.github/workflows/template-noti-discord-and-update-url-readme.yml
with: with:
ref: refs/heads/dev ref: ${{ needs.set-public-provider.outputs.ref }}
build_reason: Nightly build_reason: Nightly
push_to_branch: main push_to_branch: main
new_version: ${{ needs.get-update-version.outputs.new_version }} new_version: ${{ needs.get-update-version.outputs.new_version }}
noti-discord-manual-and-update-url-readme:
needs: [build-macos, build-windows-x64, build-linux-x64, get-update-version, set-public-provider]
secrets: inherit
if: github.event_name == 'workflow_dispatch' && github.event.inputs.public_provider == 'cloudflare-r2'
uses: ./.github/workflows/template-noti-discord-and-update-url-readme.yml
with:
ref: ${{ needs.set-public-provider.outputs.ref }}
build_reason: Manual
push_to_branch: main
new_version: ${{ needs.get-update-version.outputs.new_version }}