|
1 |
| -#!/bin/sh -l |
| 1 | +#!/bin/sh |
2 | 2 |
|
3 | 3 | set -e
|
4 | 4 |
|
|
19 | 19 |
|
20 | 20 | # If URL array is passed, only purge those. Otherwise, purge everything.
|
21 | 21 | if [ -n "$PURGE_URLS" ]; then
|
22 |
| - DATA_ARG='{"files":'"${PURGE_URLS}"'}' |
| 22 | + set -- --data '{"files":'"${PURGE_URLS}"'}' |
23 | 23 | else
|
24 |
| - DATA_ARG='{"purge_everything":true}' |
| 24 | + set -- --data '{"purge_everything":true}' |
25 | 25 | fi
|
26 | 26 |
|
27 | 27 | # Call the API and store the response for later.
|
28 | 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 |
| - --data ${DATA_ARG} \ |
33 |
| - --write-out "HTTP_STATUS:%{http_code}") |
| 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 | + "$@") |
34 | 34 |
|
35 | 35 | # Store result and HTTP status code separately to appropriately throw CI errors.
|
36 | 36 | # https://gist.github.com/maxcnunes/9f77afdc32df354883df
|
37 |
| -HTTP_BODY=$(echo $HTTP_RESPONSE | sed -E 's/HTTP_STATUS\:[0-9]{3}$//') |
38 |
| -HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -E 's/.*HTTP_STATUS:([0-9]{3})$/\1/') |
| 37 | +HTTP_BODY=$(echo "${HTTP_RESPONSE}" | sed -E 's/HTTP_STATUS\:[0-9]{3}$//') |
| 38 | +HTTP_STATUS=$(echo "${HTTP_RESPONSE}" | tr -d '\n' | sed -E 's/.*HTTP_STATUS:([0-9]{3})$/\1/') |
39 | 39 |
|
40 | 40 | # Fail pipeline and print errors if API doesn't return an OK status.
|
41 |
| -if [ $HTTP_STATUS -eq "200" ]; then |
| 41 | +if [ "${HTTP_STATUS}" -eq "200" ]; then |
42 | 42 | echo "Successfully purged!"
|
43 | 43 | exit 0
|
44 | 44 | else
|
45 |
| - echo "Purge failed. API response was:" |
46 |
| - echo $HTTP_BODY |
| 45 | + echo "Purge failed. API response was: " |
| 46 | + echo "${HTTP_BODY}" |
47 | 47 | exit 1
|
48 | 48 | fi
|
0 commit comments