Files
dropshell-templates/publish.sh
Your Name ddeb37a636
All checks were successful
Test and Publish Templates / test-and-publish (push) Successful in 30s
Update publish.sh
2025-09-02 21:38:22 +12:00

170 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
set -e
SCRIPT_DIR=$(dirname "$0")
cd "$SCRIPT_DIR"
# Detect architecture
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
ARCH="x86_64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
ARCH="aarch64"
else
echo "WARNING: Unknown architecture: $ARCH, defaulting to x86_64" >&2
ARCH="x86_64"
fi
function die() {
echo "ERROR: $1" >&2
exit 1
}
function info() {
echo "INFO: $1"
}
function get_version() {
local template="$1"
if [ -f "versions.json" ]; then
# Use jq if available, otherwise use python
if which jq >/dev/null 2>&1; then
jq -r ".\"$template\" // \"1.0.0\"" versions.json
elif which python3 >/dev/null 2>&1; then
python3 -c "import json; data=json.load(open('versions.json')); print(data.get('$template', '1.0.0'))"
else
echo "1.0.0"
fi
else
echo "1.0.0"
fi
}
# Parse arguments
PUBLISH_ALL=false
TEMPLATES_TO_PUBLISH=""
while [[ $# -gt 0 ]]; do
case $1 in
--all)
PUBLISH_ALL=true
shift
;;
--changed-only)
# Detect changed templates
if [ -x "./detect-changes.sh" ]; then
TEMPLATES_TO_PUBLISH=$(./detect-changes.sh)
if [ -z "$TEMPLATES_TO_PUBLISH" ]; then
info "No templates have changed, nothing to publish"
exit 0
fi
else
die "detect-changes.sh not found or not executable"
fi
shift
;;
*)
# Assume it's a specific template name
TEMPLATES_TO_PUBLISH="$TEMPLATES_TO_PUBLISH $1"
shift
;;
esac
done
# Check if SOS_WRITE_TOKEN is set
[[ -n $SOS_WRITE_TOKEN ]] || die "SOS_WRITE_TOKEN environment variable not set"
# Create temporary directory for packaging
TMPDIR=$(mktemp -d)
trap "rm -rf $TMPDIR" EXIT
# Always download the latest sos to ensure compatibility
info "Downloading latest sos utility..."
SOS_BIN="$TMPDIR/sos"
curl -L -o "$SOS_BIN" "https://getbin.xyz/sos:latest-$ARCH" 2>/dev/null || die "Failed to download sos"
chmod +x "$SOS_BIN"
info "sos downloaded successfully"
# If no specific templates specified and not --all, default to changed templates
if [ -z "$TEMPLATES_TO_PUBLISH" ] && [ "$PUBLISH_ALL" = false ]; then
if [ -x "./detect-changes.sh" ]; then
TEMPLATES_TO_PUBLISH=$(./detect-changes.sh)
if [ -z "$TEMPLATES_TO_PUBLISH" ]; then
info "No templates have changed, nothing to publish"
exit 0
fi
info "Publishing changed templates: $(echo $TEMPLATES_TO_PUBLISH | tr '\n' ' ')"
else
# Fallback to all templates if detect-changes.sh doesn't exist
PUBLISH_ALL=true
fi
fi
# Get list of templates to publish
if [ "$PUBLISH_ALL" = true ]; then
TEMPLATES_TO_PUBLISH=$(find . -maxdepth 1 -type d ! -name ".*" ! -name "." | sed 's|^\./||' | grep -v ".gitea")
info "Publishing all templates: $(echo $TEMPLATES_TO_PUBLISH | tr '\n' ' ')"
fi
if [ -z "$TEMPLATES_TO_PUBLISH" ]; then
die "No templates found to publish"
fi
# Download dshash if not available
if ! which dshash >/dev/null 2>&1; then
info "Downloading dshash utility..."
DSHASH_BIN="$TMPDIR/dshash"
curl -L -o "$DSHASH_BIN" "https://getbin.xyz/dshash:latest-$ARCH" 2>/dev/null || die "Failed to download dshash"
chmod +x "$DSHASH_BIN"
info "dshash downloaded successfully"
else
DSHASH_BIN="dshash"
fi
# Package and upload each template
for TEMPLATE in $TEMPLATES_TO_PUBLISH; do
if [ ! -f "$TEMPLATE/install.sh" ]; then
info "Skipping $TEMPLATE (no install.sh found)"
continue
fi
# Get the version for this template
VERSION=$(get_version "$TEMPLATE")
info "Packaging template: $TEMPLATE (version: $VERSION)"
# Create tarball of the template
TARBALL="$TMPDIR/${TEMPLATE}.tgz"
tar -czf "$TARBALL" -C . "$TEMPLATE"
# Calculate file size for info
SIZE=$(du -h "$TARBALL" | cut -f1)
info "Created $TEMPLATE.tgz ($SIZE)"
# Calculate SHA256 hash of the template directory
info "Calculating SHA256 for $TEMPLATE"
SHA256_HASH=$("$DSHASH_BIN" "$TEMPLATE")
info "SHA256 for $TEMPLATE: $SHA256_HASH"
# Upload to templates.dropshell.app using sos
info "Uploading $TEMPLATE to templates.dropshell.app"
# Always upload with :latest tag
"$SOS_BIN" upload templates.dropshell.app "$TARBALL" "${TEMPLATE}:latest" --metadata "unpackedhash=$SHA256_HASH" || die "Failed to upload $TEMPLATE:latest"
info "Uploaded ${TEMPLATE}:latest"
# Also upload with specific version tag
"$SOS_BIN" upload templates.dropshell.app "$TARBALL" "${TEMPLATE}:${VERSION}" --metadata "unpackedhash=$SHA256_HASH" || die "Failed to upload $TEMPLATE:${VERSION}"
info "Uploaded ${TEMPLATE}:${VERSION}"
# If we have a major version, also tag with major version only (e.g., v1)
MAJOR_VERSION=$(echo "$VERSION" | cut -d. -f1)
if [ -n "$MAJOR_VERSION" ]; then
"$SOS_BIN" upload templates.dropshell.app "$TARBALL" "${TEMPLATE}:v${MAJOR_VERSION}" --metadata "unpackedhash=$SHA256_HASH" || die "Failed to upload $TEMPLATE:v${MAJOR_VERSION}"
info "Uploaded ${TEMPLATE}:v${MAJOR_VERSION}"
fi
info "Successfully uploaded $TEMPLATE"
done
info "All selected templates published successfully!"