Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	| # Add safety measures | |
| set -e | |
| set -o pipefail | |
| # Check if all required environment variables are set | |
| missing_vars=() | |
| [ -z "$HF_TOKEN" ] && missing_vars+=("HF_TOKEN") | |
| [ -z "$HF_SPACE_NAME" ] && missing_vars+=("HF_SPACE_NAME") | |
| [ -z "$BUILD_COMMAND" ] && missing_vars+=("BUILD_COMMAND") | |
| [ -z "$OUTPUT_PATH" ] && missing_vars+=("OUTPUT_PATH") | |
| if [ ${#missing_vars[@]} -ne 0 ]; then | |
| echo "Missing required environment variables: ${missing_vars[*]}" | |
| 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..." | |
| # Those commands use the HF_TOKEN in the environment | |
| hfjs branch create $HF_SPACE_NAME refs/tmp/build --repo-type space --empty --force | |
| hfjs upload $HF_SPACE_NAME $OUTPUT_PATH $OUTPUT_PATH --repo-type space --revision refs/tmp/build | |
| hfjs branch create $HF_SPACE_NAME refs/convert/build --repo-type space --revision refs/tmp/build --force | |
| hfjs branch delete $HF_SPACE_NAME refs/tmp/build --repo-type space | |
| echo "Build and upload completed successfully." | 
