#!/bin/bash # Add safety measures set -e set -o pipefail # Check if all required environment variables are set if [ -z "$HF_TOKEN" ] || [ -z "$HF_SPACE_NAME" ] || [ -z "$BUILD_COMMAND" ] || [ -z "$OUTPUT_PATH" ]; then echo "Missing required environment variables" exit 1 fi # Set the Hugging Face token export HF_HUB_ACCESS_TOKEN=$HF_TOKEN # Download the space huggingface-cli download spaces/$HF_SPACE_NAME -d space # Change directory to the space's directory cd space # Determine which package manager to use if [ -f "package.json" ]; then if [ -f "pnpm-lock.yaml" ]; then pnpm install elif [ -f "yarn.lock" ]; then yarn install else npm install fi else echo "No package.json found. Assuming no package manager is needed." fi echo "Running the build command: $BUILD_COMMAND" # Run the build command eval $BUILD_COMMAND echo "Build completed successfully, uploading the build output..." # Upload the build output to the Hugging Face space huggingface-cli upload spaces/$HF_SPACE_NAME $OUTPUT_PATH / --revision refs/convert/build --token $HF_TOKEN echo "Build and upload completed successfully."