Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit eee6dba

Browse files
committed
Add support for restricted API Tokens. closes #1
1 parent 7a26e2b commit eee6dba

File tree

4 files changed

+85
-22
lines changed

4 files changed

+85
-22
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LABEL "com.github.actions.description"="Purge a zone's cache via the Cloudflare
55
LABEL "com.github.actions.icon"="trash-2"
66
LABEL "com.github.actions.color"="orange"
77

8-
LABEL version="0.2.0"
8+
LABEL version="0.3.0"
99
LABEL repository="https://github.com/jakejarvis/cloudflare-purge-action"
1010
LABEL homepage="https://jarv.is/"
1111
LABEL maintainer="Jake Jarvis <[email protected]>"

README.md

+38-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,44 @@ This simple action calls the [Cloudflare API](https://api.cloudflare.com/#zone-p
77

88
## Usage
99

10-
### Configuration
11-
1210
All sensitive variables should be [set as encrypted secrets](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables) in the action's configuration.
1311

12+
13+
### Configuration Variables
14+
1415
| Key | Value | Suggested Type | Required |
1516
| ------------- | ------------- | ------------- | ------------- |
16-
| `CLOUDFLARE_ZONE` | The Zone ID of your domain, which can be found in the right sidebar of your domain's overview page on the Cloudflare dashboard. For example, `xyz321xyz321xyz321xyz321xyz321xy`. | `secret` | **Yes** |
17-
| `CLOUDFLARE_EMAIL` | The email address you registered your Cloudflare account with. For example, `[email protected]`. | `secret` | **Yes** |
18-
| `CLOUDFLARE_KEY` | Your Cloudflare API key, which can be generated using [these instructions](https://support.cloudflare.com/hc/en-us/articles/200167836-Where-do-I-find-my-Cloudflare-API-key-). For example, `abc123abc123abc123abc123abc123abc123abc123abc`. | `secret` | **Yes** |
19-
| `PURGE_URLS` | **Optional.** An array of **fully qualified URLs** to purge. For example: `["https://jarv.is/style.css", "https://jarv.is/favicon.ico"]`. If unset, the action will purge everything (which is [suggested](#purging-specific-files)). | `env` | No |
17+
| `CLOUDFLARE_ZONE` | **Required for both methods below.** The Zone ID of your domain, which can be found in the right sidebar of your domain's overview page on the Cloudflare dashboard. For example, `xyz321xyz321xyz321xyz321xyz321xy`. | `secret` | **Yes** |
18+
| `PURGE_URLS` | **Optional.** An array of **fully qualified URLs** to purge. For example: `["https://jarv.is/style.css", "https://jarv.is/favicon.ico"]`. If unset, the action will purge everything (which is suggested — [more info below](#purging-specific-files)). | `env` | No |
19+
20+
21+
### Authentication Variables
22+
23+
Both authentication methods below require you to grab information from the [API Tokens page in the dashboard](https://dash.cloudflare.com/profile/api-tokens). Details on the inner workings of each method can be found [in Cloudflare's API docs](https://api.cloudflare.com/#getting-started-requests).
24+
25+
26+
#### Option 1: Restricted API Token
27+
28+
API Tokens are [a new feature](https://blog.cloudflare.com/api-tokens-general-availability/) as of August 2019. They allow you to restrict the scope of this action to only purging the cache of zones you specify. In other words, this is much safer than allowing this action complete control of your entire Cloudflare account. (I'm not evil though, I promise. 😊)
29+
30+
| Key | Value | Type |
31+
| ------------- | ------------- | ------------- |
32+
| `CLOUDFLARE_TOKEN` | The restricted API Token with permissions to purge the cache of one or more zones. | `secret` |
33+
34+
Creating a token can be tricky, so here's what you should enter [on this page](https://dash.cloudflare.com/profile/api-tokens) to create a token for purging the cache of a single domain on your account:
35+
36+
![Creating an API Token for purging](tokens.png)
37+
38+
39+
#### Option 2: Global API Key
40+
41+
This is the "traditional" method of authenticating — simply grab your "Global API Key" from [the dashboard](https://dash.cloudflare.com/profile/api-tokens). Using this method also **requires a second environment variable** with the email address linked to your account.
42+
43+
| Key | Value | Type |
44+
| ------------- | ------------- | ------------- |
45+
| `CLOUDFLARE_EMAIL` | The email address you registered your Cloudflare account with. For example, `[email protected]`. | `secret` |
46+
| `CLOUDFLARE_KEY` | Your Cloudflare API key, which can be generated using [these instructions](https://support.cloudflare.com/hc/en-us/articles/200167836-Where-do-I-find-my-Cloudflare-API-key-). | `secret` |
47+
2048

2149
### `workflow.yml` Example
2250

@@ -36,7 +64,11 @@ jobs:
3664
- name: Purge cache
3765
uses: jakejarvis/cloudflare-purge-action@master
3866
env:
67+
# Zone is required by both authentication methods
3968
CLOUDFLARE_ZONE: ${{ secrets.CLOUDFLARE_ZONE }}
69+
70+
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
71+
# ...or:
4072
CLOUDFLARE_EMAIL: ${{ secrets.CLOUDFLARE_EMAIL }}
4173
CLOUDFLARE_KEY: ${{ secrets.CLOUDFLARE_KEY }}
4274
```

entrypoint.sh

+46-15
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,33 @@
22

33
set -e
44

5-
if [ -z "$CLOUDFLARE_ZONE" ]; then
6-
echo "CLOUDFLARE_ZONE is not set. Quitting."
7-
exit 1
8-
fi
5+
######## Check for required/optional inputs. ########
96

10-
if [ -z "$CLOUDFLARE_EMAIL" ]; then
11-
echo "CLOUDFLARE_EMAIL is not set. Quitting."
7+
# Determine whether using a Global API Key or a restricted API Token.
8+
if [ -n "$CLOUDFLARE_KEY" ]; then
9+
# If they've passed a key, the account email address is also required.
10+
if [ -n "$CLOUDFLARE_EMAIL" ]; then
11+
API_METHOD=1
12+
else
13+
echo "CLOUDFLARE_EMAIL is required when using a Global API Key. Quitting."
14+
exit 1
15+
fi
16+
17+
# No key was entered, check if they're using a token.
18+
elif [ -n "$CLOUDFLARE_TOKEN" ]; then
19+
API_METHOD=2
20+
21+
# The user hasn't entered either a key or a token, can't do anything else.
22+
else
23+
echo "Looks like you haven't set the required authentication variables."
24+
echo "Check out the README for options: https://git.io/JeBbD"
1225
exit 1
1326
fi
1427

15-
if [ -z "$CLOUDFLARE_KEY" ]; then
16-
echo "CLOUDFLARE_KEY is not set. Quitting."
28+
29+
# Check if Zone ID is set.
30+
if [ -z "$CLOUDFLARE_ZONE" ]; then
31+
echo "CLOUDFLARE_ZONE is not set. Quitting."
1732
exit 1
1833
fi
1934

@@ -24,13 +39,29 @@ else
2439
set -- --data '{"purge_everything":true}'
2540
fi
2641

27-
# Call the API and store the response for later.
28-
HTTP_RESPONSE=$(curl -sS -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache" \
29-
-H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" \
30-
-H "X-Auth-Key: ${CLOUDFLARE_KEY}" \
31-
-H "Content-Type: application/json" \
32-
-w "HTTP_STATUS:%{http_code}" \
33-
"$@")
42+
43+
######## Call the API and store the response for later. ########
44+
45+
# Using a global API key:
46+
if [ "$API_METHOD" -eq 1 ]; then
47+
HTTP_RESPONSE=$(curl -sS "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache" \
48+
-H "Content-Type: application/json" \
49+
-H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" \
50+
-H "X-Auth-Key: ${CLOUDFLARE_KEY}" \
51+
-w "HTTP_STATUS:%{http_code}" \
52+
"$@")
53+
54+
# Using an API token:
55+
elif [ "$API_METHOD" -eq 2 ]; then
56+
HTTP_RESPONSE=$(curl -sS "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache" \
57+
-H "Content-Type: application/json" \
58+
-H "Authorization: Bearer ${CLOUDFLARE_TOKEN}" \
59+
-w "HTTP_STATUS:%{http_code}" \
60+
"$@")
61+
fi
62+
63+
64+
######## Format response for a pretty command line output. ########
3465

3566
# Store result and HTTP status code separately to appropriately throw CI errors.
3667
# https://gist.github.com/maxcnunes/9f77afdc32df354883df

tokens.png

55.6 KB
Loading

0 commit comments

Comments
 (0)