-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·35 lines (27 loc) · 1.29 KB
/
entrypoint.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
#!/bin/bash
# This script is pushing a specified directory to a CDN77 storage via rsync
# and purges the files afterwards via CDN77-API.
# The purge requests are executed in chunks so that the CDN77-API limits can be respected.
set -e
CDN77_BASE_PATH="/www"
DESTINATION="$INPUT_CDN77_STORAGE_USER@$INPUT_CDN77_STORAGE_HOST:$CDN77_BASE_PATH/$INPUT_DESTINATION_PATH"
# start ssh agent and add key
eval "$(ssh-agent)"
echo "$INPUT_CDN77_STORAGE_PRIVATE_KEY" | ssh-add -
# sync files to CDN77
FILES_TO_PURGE=($(rsync -arzO --out-format="%o %n" --delete -e "ssh -o StrictHostKeyChecking=no" \
"$INPUT_SOURCE_PATH" "$DESTINATION" | cut -d ' ' -f 2))
# close ssh agent
eval "$(ssh-agent -k)"
# process files in chunks of the defined limit
for((i=0; i < ${#FILES_TO_PURGE[@]}; i+=INPUT_CDN77_API_PURGE_LIMIT))
do
echo "Purge next $INPUT_CDN77_API_PURGE_LIMIT files."
chunk=( "${FILES_TO_PURGE[@]:i:INPUT_CDN77_API_PURGE_LIMIT}" )
# use jq to create JSON array
JSON_FILE_ARRAY=$(printf "$INPUT_DESTINATION_PATH%s\n" "${chunk[@]}" | jq -R . | jq -s .)
JSON_PAYLOAD="{\"paths\":$JSON_FILE_ARRAY}"
# send purge request to CDN77 API
curl "https://api.cdn77.com/v3/cdn/${INPUT_CDN77_RESOURCE_ID}/job/purge" -sS \
--header "Authorization: Bearer ${INPUT_CDN77_API_TOKEN}" --data "${JSON_PAYLOAD}"
done