Skip to content

Commit

Permalink
Merge pull request #21 from NathanVaughn/dev
Browse files Browse the repository at this point in the history
Add support for conditional purging
  • Loading branch information
NathanVaughn authored Jun 29, 2021
2 parents 714677a + befedd3 commit 882aaf3
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 46 deletions.
8 changes: 3 additions & 5 deletions Dockerfile
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"]
76 changes: 67 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,104 @@
# Cloudflare Cache Purge Action

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/816b3dfe8bb34c9eb922a638ff6fa3bb)](https://www.codacy.com/manual/NathanVaughn/actions-cloudflare-purge?utm_source=github.com&utm_medium=referral&utm_content=NathanVaughn/actions-cloudflare-purge&utm_campaign=Badge_Grade)

This action uses Cloudflare's API to purge their
[entire cache](https://api.cloudflare.com/#zone-purge-all-files) of your site.
[cache](https://api.cloudflare.com/#zone-purge-all-files) of your site.

## Inputs

All inputs are pulled from environment variables for security and are required.
You can set these in the [secrets](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables)
section of your repository information.
You can mix and match the various inputs however you want
(other than the zone and auth key). If you don't provide
`urls` or `tags` or `hosts` or `prefixes`, then all files will be purged.

### `CLOUDFLARE_ZONE`
### `cf_zone` or `CLOUDFLARE_ZONE` environment variable

The zone ID of your Cloudflare site. Example:

```bash
```text
023e105f4ecef8ad9ca31a8372d0c353
```

### `CLOUDFLARE_AUTH_KEY`
### `cf_auth` or `CLOUDFLARE_AUTH_KEY` environment variable

The Cloudflare API key you've generated for your zone. Example:

```bash
```text
c2547eb745079dac9320b638f5e225cf483cc5cfdda41
```

### `urls` (optional)

A space seperated list of URLs to purge. Example:

```text
"https://nathanv.me/assets/images/profile.png https://nathanv.me/assets/images/favicons/apple-touch-icon.png"
```

### `tags` (optional)

A space seperated list of tags to purge. Example:

```text
"some-tag another-tag"
```

### `hosts` (optional)

A space seperated list of hosts to purge. Example:

```text
"nathanv.me blog.nathanv.me"
```

### `prefixes` (optional)

A space seperated list of prefixes to purge. Example:

```text
"nathanv.me/assets/ blog.nathanv.me/assets"
```

## Outputs

None

## Example Usage
## Example Usages

```yml
- name: Purge cache
uses: nathanvaughn/actions-cloudflare-purge@master
if: success()
# preferred
with:
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }}
```
```yml
- name: Purge cache
uses: nathanvaughn/actions-cloudflare-purge@master
if: success()
# legacy
env:
CLOUDFLARE_ZONE: ${{ secrets.CLOUDFLARE_ZONE }}
CLOUDFLARE_AUTH_KEY: ${{ secrets.CLOUDFLARE_AUTH_KEY }}
```
```yml
- name: Purge cache
uses: nathanvaughn/actions-cloudflare-purge@master
if: success()
with:
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }}
urls: "https://nathanv.me/assets/images/profile.png https://nathanv.me/assets/images/favicons/apple-touch-icon.png"
tags: "some-tag another-tag"
hosts: "nathanv.me blog.nathanv.me"
prefixes: "nathanv.me/assets/ blog.nathanv.me/assets"
```
## Getting Cloudflare Info
1. First, go to the [API tokens page](https://dash.cloudflare.com/profile/api-tokens)
Expand Down
21 changes: 21 additions & 0 deletions action.yml
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'
32 changes: 0 additions & 32 deletions entrypoint.sh

This file was deleted.

84 changes: 84 additions & 0 deletions main.py
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()

0 comments on commit 882aaf3

Please sign in to comment.