Skip to content

Commit

Permalink
Feature add schedule clean cloudflare page and r2 (janhq#1653)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hiento09 and hiento09 authored Jan 18, 2024
1 parent 6a4ce91 commit c0c4472
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/clean-cloudflare-page-preview-url-and-r2.yml
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 14 additions & 4 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 @@ -62,14 +61,25 @@ jobs:
working-directory: docs

- 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
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: 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 }}
directory: ./docs/build
branch: dev
# Optional: Enable this if you want to have GitHub Deployments triggered
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
id: deployCloudflarePages
Expand Down
24 changes: 19 additions & 5 deletions .github/workflows/jan-electron-build-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ jobs:
if: github.event_name == 'workflow_dispatch'
outputs:
public_provider: ${{ steps.set-public-provider.outputs.public_provider }}
ref: ${{ steps.set-public-provider.outputs.ref }}
steps:
- name: Set public provider
id: set-public-provider
run: |
if [ ${{ github.event == 'workflow_dispatch' }} ]; then
echo "::set-output name=public_provider::${{ github.event.inputs.public_provider }}"
echo "::set-output name=ref::${{ github.ref }}"
else
echo "::set-output name=public_provider::cloudflare-r2"
echo "::set-output name=ref::refs/heads/dev"
fi
# Job create Update app version based on latest release tag with build number and save to output
get-update-version:
Expand All @@ -37,7 +40,7 @@ jobs:
needs: [get-update-version, set-public-provider]
secrets: inherit
with:
ref: refs/heads/dev
ref: ${{ needs.set-public-provider.outputs.ref }}
public_provider: ${{ needs.set-public-provider.outputs.public_provider }}
new_version: ${{ needs.get-update-version.outputs.new_version }}

Expand All @@ -46,7 +49,7 @@ jobs:
secrets: inherit
needs: [get-update-version, set-public-provider]
with:
ref: refs/heads/dev
ref: ${{ needs.set-public-provider.outputs.ref }}
public_provider: ${{ needs.set-public-provider.outputs.public_provider }}
new_version: ${{ needs.get-update-version.outputs.new_version }}

Expand All @@ -56,17 +59,28 @@ jobs:
secrets: inherit
needs: [get-update-version, set-public-provider]
with:
ref: refs/heads/dev
ref: ${{ needs.set-public-provider.outputs.ref }}
public_provider: ${{ needs.set-public-provider.outputs.public_provider }}
new_version: ${{ needs.get-update-version.outputs.new_version }}

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
if: github.event_name == 'schedule'
uses: ./.github/workflows/template-noti-discord-and-update-url-readme.yml
with:
ref: refs/heads/dev
ref: ${{ needs.set-public-provider.outputs.ref }}
build_reason: Nightly
push_to_branch: main
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 }}

0 comments on commit c0c4472

Please sign in to comment.