2022-04-05 19:12:32 +00:00
|
|
|
name: Synchronize with CDN
|
|
|
|
|
|
|
|
on:
|
|
|
|
push:
|
|
|
|
branches:
|
|
|
|
- "master"
|
|
|
|
|
|
|
|
env:
|
|
|
|
REGISTRY: ghcr.io
|
|
|
|
IMAGE_NAME: ${{ github.repository }}
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
sync_with_cdn:
|
2022-04-05 19:14:09 +00:00
|
|
|
name: Synchronize with CDN
|
2022-04-05 19:12:32 +00:00
|
|
|
runs-on: ubuntu-latest
|
|
|
|
permissions:
|
|
|
|
contents: read
|
|
|
|
steps:
|
|
|
|
- name: Checkout Repo
|
|
|
|
uses: actions/checkout@v3
|
|
|
|
|
|
|
|
- name: Synchronize with CDN
|
|
|
|
env:
|
|
|
|
AWS_ACCESS_KEY: ${{ secrets.CDN_ACCESS_KEY }}
|
|
|
|
AWS_SECRET_KEY: ${{ secrets.CDN_SECRET_KEY }}
|
|
|
|
CDN_BUCKET: ${{ secrets.CDN_BUCKET }}
|
2022-04-06 09:52:57 +00:00
|
|
|
DO_TOKEN: ${{ secrets.DO_TOKEN }}
|
|
|
|
CDN_ID: ${{ secrets.CDN_ID }}
|
2022-04-05 19:12:32 +00:00
|
|
|
run: |
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
set -o pipefail
|
|
|
|
|
2022-04-05 19:26:40 +00:00
|
|
|
CURRENT_DIR=$(pwd)
|
|
|
|
|
2022-04-05 19:12:32 +00:00
|
|
|
# Install rclone
|
2022-04-05 19:26:40 +00:00
|
|
|
TMPDIR=$(mktemp -d)
|
|
|
|
cd $TMPDIR
|
2022-04-05 19:20:46 +00:00
|
|
|
curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip
|
|
|
|
unzip rclone-current-linux-amd64.zip
|
|
|
|
cd rclone-*-linux-amd64
|
|
|
|
sudo cp rclone /usr/bin/
|
|
|
|
sudo chown root:root /usr/bin/rclone
|
|
|
|
sudo chmod 755 /usr/bin/rclone
|
2022-04-05 19:12:32 +00:00
|
|
|
|
2022-04-05 19:26:40 +00:00
|
|
|
cd $CURRENT_DIR
|
|
|
|
|
2022-04-05 19:12:32 +00:00
|
|
|
# Create a S3 target
|
|
|
|
rclone config create s3 s3 provider=DigitalOcean env_auth=true region=fra1 endpoint=fra1.digitaloceanspaces.com acl=public-read
|
|
|
|
|
|
|
|
# remove `.github` and any other paths we do not want in the CDN
|
2022-04-05 19:26:40 +00:00
|
|
|
rm -rf .github .git
|
2022-04-05 19:12:32 +00:00
|
|
|
|
2023-04-05 16:23:36 +00:00
|
|
|
# Note all the files that are missing in cdn, missing in repo or they are different
|
|
|
|
echo "Checking files"
|
|
|
|
(rclone check s3:$CDN_BUCKET/icons icons --download --combined /tmp/diff || true)
|
|
|
|
|
|
|
|
# List all files that are different
|
|
|
|
echo "Listing all differences"
|
|
|
|
cat /tmp/diff | grep --quiet --invert-match '^=' || echo "No changes"
|
|
|
|
|
2022-06-21 13:21:51 +00:00
|
|
|
# Synchronize the `icons` directory
|
|
|
|
# Note: This command should be added for any new directories in this repo
|
|
|
|
# otherwise, they'll not be synced
|
2023-04-05 16:23:36 +00:00
|
|
|
echo "Syncing files"
|
2022-06-21 13:21:51 +00:00
|
|
|
rclone sync icons s3:$CDN_BUCKET/icons
|
2022-04-05 19:12:32 +00:00
|
|
|
|
2022-04-05 19:33:52 +00:00
|
|
|
rclone config delete s3
|
2022-04-06 09:52:57 +00:00
|
|
|
|
|
|
|
# Wipe out DigitalOcean CDN cache
|
2022-06-21 13:21:51 +00:00
|
|
|
# Note: This command should be added for any new directories in this repo
|
|
|
|
# otherwise, cache for those directories will not be cleared
|
|
|
|
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer $DO_TOKEN" -d '{"files": ["icons/*"]}' https://api.digitalocean.com/v2/cdn/endpoints/$CDN_ID/cache
|
2023-04-05 16:23:36 +00:00
|
|
|
|
|
|
|
# Purge each entry in /tmp/diff from cloudflare's cache
|
|
|
|
cat /tmp/diff | grep --quiet --invert-match '^=' || true | while read line;
|
|
|
|
do
|
|
|
|
entry=$(echo $line | awk '{print $2}');
|
|
|
|
url="https://cdn.instadapp.io/icons/${entry}"
|
|
|
|
echo "Clearing $url from cache"
|
|
|
|
curl -X POST "https://api.cloudflare.com/client/v4/zones/${{ secrets.CLOUDFLARE_ZONE_ID }}/purge_cache" -H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_KEY }}" -H "Content-Type: application/json" --data "{\"files\": [\"$url\"]}"
|
|
|
|
done;
|