name: Deploy to Hugging Face # Runs after a successful CI on main: only tested code reaches the Space. on: workflow_run: workflows: [CI] types: [completed] branches: [main] workflow_dispatch: concurrency: group: deploy cancel-in-progress: true permissions: contents: read jobs: deploy: if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' runs-on: ubuntu-latest timeout-minutes: 10 env: HF_TOKEN: ${{ secrets.HF_TOKEN }} HF_SPACE: ${{ vars.HF_SPACE }} # e.g. "your-username/park-copilot" steps: # Tolerates copy-paste accidents: spaces/newlines, or a full Space URL instead of # "user/space". The cleaned values are passed to the next steps. - name: Check configuration run: | token=$(printf '%s' "$HF_TOKEN" | tr -d '[:space:]') space=$(printf '%s' "$HF_SPACE" | tr -d '[:space:]') space=${space#https://huggingface.co/spaces/} space=${space%/} if [ -z "$token" ] || [ -z "$space" ]; then echo "::error::Set the HF_TOKEN secret and the HF_SPACE variable in the repo settings" exit 1 fi if ! printf '%s' "$space" | grep -Eq '^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$'; then echo "::error::HF_SPACE must look like 'username/space-name', got '$space'" exit 1 fi echo "::add-mask::$token" echo "DEPLOY_TOKEN=$token" >> "$GITHUB_ENV" echo "DEPLOY_SPACE=$space" >> "$GITHUB_ENV" echo "Deploying to https://huggingface.co/spaces/$space" - uses: actions/checkout@v4 with: ref: ${{ github.event.workflow_run.head_sha || github.sha }} # The Space reads its settings from a YAML header in README.md. It is added only in # the copy pushed to Hugging Face, so the GitHub README stays clean. - name: Add the Space header to README.md run: | { echo "---" echo "title: Park Copilot" echo "emoji: 🎢" echo "colorFrom: indigo" echo "colorTo: purple" echo "sdk: docker" echo "app_port: 7860" echo "pinned: false" echo "short_description: AI agent that plans your day at Disneyland Paris" echo "---" echo cat README.md } > README.space.md mv README.space.md README.md # A single fresh commit: the Space only needs the current code, not the history. - name: Push to the Space run: | git config user.name "github-actions" git config user.email "github-actions@users.noreply.github.com" git checkout --orphan space git add -A git commit -m "Deploy ${GITHUB_SHA::7} from GitHub" git push --force "https://user:${DEPLOY_TOKEN}@huggingface.co/spaces/${DEPLOY_SPACE}" space:main