#!/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 # Download the space # git clone --depth 1 https://:$HF_TOKEN@huggingface.co/spaces/$HF_SPACE_NAME space huggingface-cli download $HF_SPACE_NAME --repo-type space --local-dir space --token $HF_TOKEN # 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 $HF_SPACE_NAME $OUTPUT_PATH / --repo-type space --revision refs/convert/build --token $HF_TOKEN echo "Build and upload completed successfully."