|
| 1 | +name: Synchronize with CDN - Docs |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "master" |
| 7 | + paths: |
| 8 | + - "docs/**" |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: ${{ github.repository }} |
| 13 | + |
| 14 | +jobs: |
| 15 | + sync_with_cdn: |
| 16 | + name: Synchronize with CDN |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + steps: |
| 21 | + - name: Checkout Repo |
| 22 | + uses: actions/checkout@v3 |
| 23 | + |
| 24 | + - name: Synchronize with CDN |
| 25 | + env: |
| 26 | + AWS_ACCESS_KEY: ${{ secrets.CDN_ACCESS_KEY }} |
| 27 | + AWS_SECRET_KEY: ${{ secrets.CDN_SECRET_KEY }} |
| 28 | + CDN_BUCKET: ${{ secrets.CDN_BUCKET }} |
| 29 | + DO_TOKEN: ${{ secrets.DO_TOKEN }} |
| 30 | + CDN_ID: ${{ secrets.CDN_ID }} |
| 31 | + run: | |
| 32 | + #!/bin/bash |
| 33 | +
|
| 34 | + set -e |
| 35 | + set -o pipefail |
| 36 | +
|
| 37 | + CURRENT_DIR=$(pwd) |
| 38 | +
|
| 39 | + # Install rclone |
| 40 | + TMPDIR=$(mktemp -d) |
| 41 | + cd $TMPDIR |
| 42 | + curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip |
| 43 | + unzip rclone-current-linux-amd64.zip |
| 44 | + cd rclone-*-linux-amd64 |
| 45 | + sudo cp rclone /usr/bin/ |
| 46 | + sudo chown root:root /usr/bin/rclone |
| 47 | + sudo chmod 755 /usr/bin/rclone |
| 48 | +
|
| 49 | + cd $CURRENT_DIR |
| 50 | +
|
| 51 | + # Create a S3 target |
| 52 | + rclone config create s3 s3 provider=DigitalOcean env_auth=true region=fra1 endpoint=fra1.digitaloceanspaces.com acl=public-read |
| 53 | +
|
| 54 | + # remove `.github` and any other paths we do not want in the CDN |
| 55 | + rm -rf .github .git |
| 56 | +
|
| 57 | + # Note all the files that are missing in cdn, missing in repo or they are different |
| 58 | + echo "Checking files" |
| 59 | + (rclone check s3:$CDN_BUCKET/docs docs --download --combined /tmp/diff || true) |
| 60 | +
|
| 61 | + # List all files that are different |
| 62 | + echo "Listing all differences" |
| 63 | + cat /tmp/diff | grep --quiet --invert-match '^=' || echo "No changes" |
| 64 | +
|
| 65 | + # Synchronize the `docs` directory |
| 66 | + # Note: This command should be added for any new directories in this repo |
| 67 | + # otherwise, they'll not be synced |
| 68 | + echo "Syncing files" |
| 69 | + rclone sync docs s3:$CDN_BUCKET/docs |
| 70 | +
|
| 71 | + rclone config delete s3 |
| 72 | +
|
| 73 | + # Wipe out DigitalOcean CDN cache |
| 74 | + # Note: This command should be added for any new directories in this repo |
| 75 | + # otherwise, cache for those directories will not be cleared |
| 76 | + echo "Wiping out DigitalOcean CDN cache" |
| 77 | + curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer $DO_TOKEN" -d '{"files": ["docs/*"]}' https://api.digitalocean.com/v2/cdn/endpoints/$CDN_ID/cache |
| 78 | +
|
| 79 | + # Purge each entry in /tmp/diff from cloudflare's cache |
| 80 | + echo "Purging Cloudflare cache" |
| 81 | + cat /tmp/diff | grep --quiet --invert-match '^=' || true | while read line; |
| 82 | + do |
| 83 | + entry=$(echo $line | awk '{print $2}'); |
| 84 | + url="https://cdn.instadapp.io/docs/${entry}" |
| 85 | + echo "Clearing $url from cache" |
| 86 | + 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\"]}" |
| 87 | + done; |
0 commit comments