-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cloudflare-CLI-Purge-Cache.sh
99 lines (80 loc) · 3.59 KB
/
Cloudflare-CLI-Purge-Cache.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
###############################################################################
# Cloudflare-CLI #
# https://github.com/cvc90/Cloudflare-CLI/ #
# #
# CLI utility that manages Cloudflare services through the Cloudflare API #
# #
# Script file Cloudflare-CLI-Purge-Cache.sh #
# A simple script to purge your domain cache in your Cloudflare account #
# #
# Usage: ./Cloudflare-CLI-Purge-Cache.sh #
###############################################################################
set -e
######## Required/Optional inputs. ########
CLOUDFLARE_ZONE="your_ZONE"
CLOUDFLARE_TOKEN="your_TOKEN"
CLOUDFLARE_EMAIL="your_EMAIL"
CLOUDFLARE_KEY="your_KEY"
#PURGE_URLS='["https://domain.com/style.css", "https://domain.com/favicon.ico"]'
######## Check for required/optional inputs. ########
# Determine whether using a Global API Key or a restricted API Token.
if [ -n "$CLOUDFLARE_KEY" ]; then
# If they've passed a key, the account email address is also required.
if [ -n "$CLOUDFLARE_EMAIL" ]; then
API_METHOD=1
else
echo "CLOUDFLARE_EMAIL is required when using a Global API Key. Quitting."
exit 1
fi
# No key was entered, check if they're using a token.
elif [ -n "$CLOUDFLARE_TOKEN" ]; then
API_METHOD=2
# The user hasn't entered either a key or a token, can't do anything else.
else
echo "Looks like you haven't set the required authentication variables."
echo "Check out the README for options: https://git.io/JeBbD"
exit 1
fi
# Check if Zone ID is set.
if [ -z "$CLOUDFLARE_ZONE" ]; then
echo "CLOUDFLARE_ZONE is not set. Quitting."
exit 1
fi
# If URL array is passed, only purge those. Otherwise, purge everything.
if [ -n "$PURGE_URLS" ]; then
set -- --data '{"files":'"${PURGE_URLS}"'}'
else
set -- --data '{"purge_everything":true}'
fi
######## Call the API and store the response for later. ########
# Using a global API key:
if [ "$API_METHOD" -eq 1 ]; then
HTTP_RESPONSE=$(curl -sS "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache" \
-H "Content-Type: application/json" \
-H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" \
-H "X-Auth-Key: ${CLOUDFLARE_KEY}" \
-w "HTTP_STATUS:%{http_code}" \
"$@")
# Using an API token:
elif [ "$API_METHOD" -eq 2 ]; then
HTTP_RESPONSE=$(curl -sS "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${CLOUDFLARE_TOKEN}" \
-w "HTTP_STATUS:%{http_code}" \
"$@")
fi
######## Format response for a pretty command line output. ########
# Store result and HTTP status code separately to appropriately throw CI errors.
# https://gist.github.com/maxcnunes/9f77afdc32df354883df
HTTP_BODY=$(echo "${HTTP_RESPONSE}" | sed -E 's/HTTP_STATUS\:[0-9]{3}$//')
HTTP_STATUS=$(echo "${HTTP_RESPONSE}" | tr -d '\n' | sed -E 's/.*HTTP_STATUS:([0-9]{3})$/\1/')
# Fail pipeline and print errors if API doesn't return an OK status.
if [ "${HTTP_STATUS}" -eq "200" ]; then
echo "Successfully purged!"
exit 0
else
echo "Purge failed. API response was: "
echo "${HTTP_BODY}"
exit 1
fi