-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from NathanVaughn/dev
Add support for conditional purging
- Loading branch information
Showing
5 changed files
with
175 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
FROM alpine:3.14.0 | ||
FROM python:3.9-alpine | ||
|
||
RUN apk add --no-cache bash curl | ||
|
||
COPY entrypoint.sh /entrypoint.sh | ||
ENTRYPOINT ["/entrypoint.sh"] | ||
COPY main.py /app/main.py | ||
ENTRYPOINT ["python", "/app/main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
name: 'Cloudflare Cache Purge Action' | ||
description: 'A GitHub Action to purge Cloudflare''s entire cache of your site' | ||
author: 'Nathan Vaughn' | ||
inputs: | ||
cf_zone: | ||
description: 'Cloudflare Zone' | ||
required: false | ||
cf_auth: | ||
description: 'Cloudflare Authentication Key' | ||
required: false | ||
urls: | ||
description: 'URLs to purge' | ||
required: true | ||
tags: | ||
description: 'Cache-Tags to purge' | ||
required: true | ||
hosts: | ||
description: 'Hosts to purge' | ||
required: true | ||
prefixes: | ||
description: 'Prefixes to purge' | ||
required: true | ||
runs: | ||
using: 'docker' | ||
image: 'Dockerfile' | ||
args: | ||
- '--cf-zone ${{ inputs.cf_zone }} --cf-auth ${{ inputs.cf_auth }} --urls ${{ inputs.urls }} --tags ${{ inputs.tags }} --hosts ${{ inputs.hosts }} --prefixes ${{ inputs.prefixes }}' | ||
branding: | ||
icon: 'cloud' | ||
color: 'orange' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import argparse | ||
import json | ||
import pprint | ||
import os | ||
import sys | ||
from urllib import request | ||
|
||
|
||
def main(): | ||
# parse the arguments | ||
parser = argparse.ArgumentParser() | ||
|
||
# load values from environment first, by default | ||
parser.add_argument("--cf-zone", nargs="?", type=str) | ||
parser.add_argument("--cf-auth", nargs="?", type=str) | ||
|
||
# caching objects | ||
parser.add_argument("--urls", nargs="*", type=str) | ||
parser.add_argument("--tags", nargs="*", type=str) | ||
parser.add_argument("--hosts", nargs="*", type=str) | ||
parser.add_argument("--prefixes", nargs="*", type=str) | ||
|
||
args = parser.parse_args(sys.argv[1].split()) | ||
|
||
# if no argument given, pull from environment | ||
if not args.cf_zone: | ||
args.cf_zone = os.getenv("CLOUDFLARE_ZONE") | ||
|
||
if not args.cf_auth: | ||
args.cf_auth = os.getenv("CLOUDFLARE_AUTH_KEY") | ||
|
||
# see if anything was set | ||
if not args.cf_zone: | ||
parser.error("Cloudflare Zone required") | ||
|
||
if not args.cf_auth: | ||
parser.error("Cloudflare Auth required") | ||
|
||
# prepare the request data | ||
|
||
data = {} | ||
|
||
if args.urls: | ||
data["files"] = args.urls | ||
|
||
if args.tags: | ||
data["tags"] = args.tags | ||
|
||
if args.hosts: | ||
data["hosts"] = args.hosts | ||
|
||
if args.prefixes: | ||
data["prefixes"] = args.prefixes | ||
|
||
if not args.urls and not args.tags and not args.hosts and not args.prefixes: | ||
data["purge_everything"] = True | ||
|
||
# create the request | ||
url = f"https://api.cloudflare.com/client/v4/zones/{args.cf_zone}/purge_cache" | ||
encoded_data = json.dumps(data).encode("utf-8") | ||
headers = { | ||
"Authorization": f"Bearer {args.cf_auth}", | ||
"Content-Type": "application/json", | ||
"Content-Length": len(encoded_data), | ||
} | ||
|
||
# send it | ||
print(f"Making POST request to {url} with {data}") | ||
|
||
req = request.Request(url, data=encoded_data, headers=headers) | ||
resp = request.urlopen(req) | ||
|
||
# process response | ||
resp_data = json.loads(resp.read()) | ||
|
||
print("Response:") | ||
print(pprint.pprint(resp_data)) | ||
|
||
if resp_data["success"] != True: | ||
raise Exception("Success NOT True") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |