Skip to content

Commit

Permalink
[CI] add cloudflare cache clear
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Jan 12, 2024
1 parent 276c250 commit 3a207c5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ jobs:
echo "S3_API_SECRET_KEY=${{ secrets.S3_API_SECRET_KEY }}" >> $GITHUB_ENV
echo "S3_REGION_NAME=${{ secrets.S3_REGION_NAME }}" >> $GITHUB_ENV
echo "S3_ENDPOINT_URL=${{ secrets.S3_ENDPOINT_URL }}" >> $GITHUB_ENV
echo "CLOUDFLARE_TOKEN=${{ secrets.CLOUDFLARE_TOKEN }}" >> $GITHUB_ENV
echo "CLOUDFLARE_ZONE=${{ secrets.CLOUDFLARE_ZONE }}" >> $GITHUB_ENV
TARGET_BRANCH=$([ "$GITHUB_HEAD_REF" == "" ] && echo ${GITHUB_REF##*/} || echo "$GITHUB_HEAD_REF")
echo "TARGET_BRANCH=${TARGET_BRANCH}" >> $GITHUB_ENV
Expand Down Expand Up @@ -145,6 +147,7 @@ jobs:
sed -i "s/VERSION_PLACEHOLDER/${TARGET_BRANCH#refs/*/}/g" metadata.yaml
cd OctoBot
python start.py tentacles -m "../metadata.yaml" -d "../new_tentacles" -p "../../any_platform.zip" -ite -ute ${{ secrets.TENTACLES_OFFICIAL_PATH }}/tentacles -upe ${{ secrets.TENTACLES_OFFICIAL_PATH }}/packages/full/${{ secrets.TENTACLES_REPOSITORY_NAME }}/
python ../scripts/clear_cloudflare_cache.py ${TARGET_BRANCH#refs/*/}
- name: Publish latest tentacles
if: github.ref == 'refs/heads/dev' && startsWith(github.ref, 'refs/tags') != true
Expand All @@ -154,6 +157,7 @@ jobs:
sed -i "s/VERSION_PLACEHOLDER/latest/g" metadata.yaml
cd OctoBot
python start.py tentacles -m "../metadata.yaml" -d "../new_tentacles" -p "../../any_platform.zip" -upe ${{ secrets.TENTACLES_OFFICIAL_PATH }}/packages/full/${{ secrets.TENTACLES_REPOSITORY_NAME }}/
python ../scripts/clear_cloudflare_cache.py latest
- name: Publish stable tentacles
if: github.ref == 'refs/heads/master'
Expand All @@ -163,6 +167,7 @@ jobs:
sed -i "s/VERSION_PLACEHOLDER/stable/g" metadata.yaml
cd OctoBot
python start.py tentacles -m "../metadata.yaml" -d "../new_tentacles" -p "../../any_platform.zip" -upe ${{ secrets.TENTACLES_OFFICIAL_PATH }}/packages/full/${{ secrets.TENTACLES_REPOSITORY_NAME }}/
python ../scripts/clear_cloudflare_cache.py stable
- name: Publish cleaned branch tentacles
if: startsWith(github.ref, 'refs/tags') != true && github.ref != 'refs/heads/master'
Expand All @@ -175,6 +180,7 @@ jobs:
sed -i "s/officials/dev/g" metadata.yaml
cd OctoBot
python start.py tentacles -m "../metadata.yaml" -d "../new_tentacles" -p "../../any_platform.zip" -upe ${{ secrets.TENTACLES_OFFICIAL_PATH }}/packages/full/${{ secrets.TENTACLES_REPOSITORY_NAME }}/
python ../scripts/clear_cloudflare_cache.py $branch
notify:
if: ${{ failure() }}
Expand Down
59 changes: 59 additions & 0 deletions scripts/clear_cloudflare_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import sys
import requests


CLOUDFLARE_ZONE = os.getenv("CLOUDFLARE_ZONE")
CLOUDFLARE_TOKEN = os.getenv("CLOUDFLARE_TOKEN")
S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME")


def _send_purge_cache_request(url: str, cloudflare_token: str, urls_to_purge: list):
with requests.post(
url=url,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {cloudflare_token}",
},
json={
"files": urls_to_purge
}
) as resp:
if resp.status_code == 200:
print(f"Cache purged for {', '.join(urls_to_purge)}")
else:
print(f"Error when purging cache, status: {resp.status_code}, body: {resp.text}")


def _get_tentacles_url(tentacle_url_identifier):
if not S3_BUCKET_NAME:
raise RuntimeError("Missing S3_BUCKET_NAME env variable")
return os.getenv(
"TENTACLES_URL",
f"https://{S3_BUCKET_NAME}."
f"{os.getenv('TENTACLES_OCTOBOT_ONLINE_URL', 'octobot.online')}/"
f"officials/packages/full/base/"
f"{tentacle_url_identifier}/"
f"any_platform.zip"
)


def clear_cache(tentacle_url_identifiers):
if not CLOUDFLARE_ZONE:
raise RuntimeError("Missing CLOUDFLARE_ZONE env variable")
if not CLOUDFLARE_TOKEN:
raise RuntimeError("Missing CLOUDFLARE_TOKEN env variable")
# https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-url
url = f"https://api.cloudflare.com/client/v4/zones/{CLOUDFLARE_ZONE}/purge_cache"
_send_purge_cache_request(
url,
CLOUDFLARE_TOKEN,
[
_get_tentacles_url(tentacle_url_identifier)
for tentacle_url_identifier in tentacle_url_identifiers
]
)


if __name__ == '__main__':
clear_cache(sys.argv[1:])

0 comments on commit 3a207c5

Please sign in to comment.