-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpurge_cloudflare_cache.sh
95 lines (88 loc) · 2.51 KB
/
purge_cloudflare_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
#!/usr/bin/env bash
readonly DELAY_STEP_SECONDS=15
readonly INTERVAL_SECONDS=5
readonly TIMEOUT_SECONDS=120
readonly GITHUB_USER=schnerring
readonly GITHUB_REPO=schnerring.github.io
##################################################
# Poll status of latest GitHub Pages build every INTERVAL_SECONDS seconds for up
# to TIMEOUT_SECONDS seconds.
# Globals:
# GITHUB_REPO
# GITHUB_TOKEN
# GITHUB_USER
# INTERVAL_SECONDS
# TIMEOUT_SECONDS
# Arguments:
# None
# Outputs:
# Success message to stdout or error message to stderr.
# Returns:
# 0 on success, 1 otherwise.
##################################################
function poll_build_status() {
echo "Awaiting completion of latest GitHub Pages build ..."
local waited_seconds=0
while [[ "${waited_seconds}" -lt "${TIMEOUT_SECONDS}" ]]; do
if curl \
--silent \
--user "${GITHUB_USER}:${GITHUB_TOKEN}" \
--header "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${GITHUB_USER}/${GITHUB_REPO}/pages/builds/latest" \
| grep -q '"status": "built"'; then
echo "Success."
return 0
fi
echo " Sleeping ${INTERVAL_SECONDS} seconds until next status poll ..."
sleep "${INTERVAL_SECONDS}"
(( waited_seconds += INTERVAL_SECONDS ))
done
echo "Failure." >&2
return 1
}
##################################################
# Purge entire Cloudflare cache.
# Globals:
# CLOUDFLARE_API_TOKEN
# CLOUDFLARE_ZONE_ID
# Arguments:
# None
# Outputs:
# Success message to stdout or error message to stderr.
# Returns:
# 0 on success, 1 otherwise.
##################################################
function purge_cache() {
echo "Purging Cloudflare cache ..."
if curl \
--silent \
--request POST \
--header "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
--header "Content-Type: application/json" \
--data '{"purge_everything":true}' \
"https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/purge_cache" \
| grep -q '"success": true'; then
echo "Success."
return 0
else
echo "Failure." >&2
return 1
fi
}
##################################################
# Main function of script.
# Globals:
# DELAY_STEP_SECONDS
# Arguments:
# None
##################################################
function main() {
echo "Sleeping ${DELAY_STEP_SECONDS} seconds ..."
sleep "${DELAY_STEP_SECONDS}"
poll_build_status || exit 1
echo "Sleeping ${DELAY_STEP_SECONDS} seconds ..."
sleep "${DELAY_STEP_SECONDS}"
purge_cache || exit 1
}
# Entrypoint
main "$@"