Talon/scripts/upload.sh
ThetaDev 44fc06cd4b
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
fix(script): detect CI commit SHA
2023-04-03 13:57:07 +02:00

75 lines
2.7 KiB
Bash
Executable file

#!/bin/sh
set -e
# Check for dependencies
which curl > /dev/null
which jq > /dev/null
# Assert required variables
if [ -z "$TALON_KEY" ]; then echo "TALON_KEY unset"; exit 1; fi
if [ -z "$TALON_URL" ]; then echo "TALON_URL unset"; exit 1; fi
if [ -z "$SUBDOMAIN" ]; then echo "SUBDOMAIN unset"; exit 1; fi
API_URL="$TALON_URL/api"
API_KEY_H="x-api-key: $TALON_KEY"
# Check if the website already exists
WEBSITE_STATUS=$(curl --head -o /dev/null -s -w "%{http_code}" "$API_URL/website/$SUBDOMAIN")
if [ "$WEBSITE_STATUS" = "200" ]; then
echo "Website '$SUBDOMAIN' found"
else
# Create the website if it does not exist
if [ -z "$WEBSITE_NAME" ]; then echo "WEBSITE_NAME unset"; exit 1; fi
CREATE_BODY=$(jq -c --null-input --arg name "$WEBSITE_NAME" --arg color "$WEBSITE_COLOR" \
--arg visibility "$WEBSITE_VISIBILITY" --arg source_url "$WEBSITE_SOURCE_URL" \
--arg source_icon "$WEBSITE_SOURCE_ICON" \
'{"name": $name, "color": $color, "visibility": $visibility, "source_url": $source_url, "source_icon": $source_icon} | delpaths([path(.[]| select(.==""))])')
echo "Creating website '$SUBDOMAIN': $CREATE_BODY"
curl -Ss --fail -X "PUT" -H "$API_KEY_H" -H "content-type: application/json" --data "$CREATE_BODY" "$API_URL/website/$SUBDOMAIN"
fi
# Check the upload directory
if [ ! -d "$1" ]; then echo "Upload directory does not exist"; exit 1; fi
if [ ! -f "$1/index.html" ]; then echo "Upload directory does not contain index.html"; exit 1; fi
# Validate fallback page param
if [ "$FALLBACK" ] && [ ! -f "$1/$FALLBACK" ]; then echo "fallback page $FALLBACK does not exist"; exit 1; fi
# Automatically detect fallback pages
if [ -z "$SPA" ] && [ -z "$FALLBACK" ]; then
if [ -f "$1/404.html" ]; then FALLBACK="404.html"; fi
if [ -f "$1/200.html" ]; then SPA=true; FALLBACK="200.html"; fi
fi
push_arg() {
if [ "$UPLOAD_ARGS" ]; then UPLOAD_ARGS="$UPLOAD_ARGS&"; fi
UPLOAD_ARGS="$UPLOAD_ARGS$1"
}
if [ "$FALLBACK" ]; then push_arg "fallback=$FALLBACK"; fi
if [ "$SPA" = "true" ]; then push_arg "spa=true"; fi
if [ "$UPLOAD_ARGS" ]; then UPLOAD_ARGS="?$UPLOAD_ARGS"; fi
if [ "$CI_COMMIT_SHA" ]; then
echo "Git commit: $CI_COMMIT_SHA"
push_arg "commit=$CI_COMMIT_SHA"
elif GIT_COMMIT=$(git rev-parse HEAD 2> /dev/null); then
echo "Git commit: $GIT_COMMIT"
push_arg "commit=$GIT_COMMIT"
fi
# Compress website
ARCHIVE=$(mktemp)
tar -cz --directory "$1" --file "$ARCHIVE" .
# Upload website
echo "Version params: $UPLOAD_ARGS"
echo "Uploading..."
curl --fail -X "POST" -H "$API_KEY_H" -H "content-type: application/octet-stream" --data-binary "@$ARCHIVE" "$API_URL/website/$SUBDOMAIN/upload$UPLOAD_ARGS"
rm "$ARCHIVE"
echo "Website uploaded ;-)"