<# .SYNOPSIS Sets up GitHub Actions CI/CD autodeploy for a local repo targeting a Hugging Face Docker Space. .PARAMETER RepoPath Path to the local git repo. .PARAMETER HFSpaceOwner Hugging Face username or org that owns the Space. .PARAMETER HFSpaceName Hugging Face Space repository name. .PARAMETER Branch GitHub branch to watch for auto-deploy. Defaults to 'main'. #> param( [Parameter(Mandatory=$true)][string]$RepoPath, [Parameter(Mandatory=$true)][string]$HFSpaceOwner, [Parameter(Mandatory=$true)][string]$HFSpaceName, [string]$Branch = "main" ) $ErrorActionPreference = 'Stop' if (-not (Get-Command gh -ErrorAction SilentlyContinue)) { Write-Host "GitHub CLI (gh) is required but not found in PATH." -ForegroundColor Red Write-Host "Install it from https://cli.github.com/ and run 'gh auth login' first." -ForegroundColor Yellow exit 1 } if (-not (Get-Command git -ErrorAction SilentlyContinue)) { Write-Host "git is required but not found in PATH." -ForegroundColor Red exit 1 } $repoAbs = Resolve-Path $RepoPath Set-Location $repoAbs if (-not (Test-Path .git)) { Write-Host "'.git' not found in $repoAbs. Is this a git repo?" -ForegroundColor Red exit 1 } $workflowDir = Join-Path $repoAbs ".github/workflows" $workflowPath = Join-Path $workflowDir "hf-docker-space-deploy.yml" New-Item -ItemType Directory -Path $workflowDir -Force | Out-Null $hfGitUrl = "https://huggingface.co/spaces/$HFSpaceOwner/$HFSpaceName.git" $workflow = @" name: Deploy to Hugging Face Space on: push: branches: [ $Branch ] workflow_dispatch: jobs: validate-and-deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: 20 cache: npm - name: Install dependencies run: npm ci - name: Type check run: npm run lint - name: Test run: npm run test - name: Build run: npm run build - name: Deploy to Hugging Face Space env: HF_TOKEN: `${{ secrets.HF_TOKEN }} run: | git config --global user.email "$HFSpaceOwner@users.noreply.huggingface.co" git config --global user.name "$HFSpaceOwner" git remote add huggingface https://user:`$HF_TOKEN@$($HFSpaceOwner.ToLower())/$($HFSpaceName.ToLower())/git git push huggingface $Branch "@ Set-Content -Path $workflowPath -Value $workflow -Encoding UTF8 Write-Host "Created workflow: $workflowPath" -ForegroundColor Green $hfRemoteUrl = "https://huggingface.co/spaces/$HFSpaceOwner/$HFSpaceName.git" $existingRemote = git remote get-url huggingface 2>$null if ($existingRemote) { Write-Host "Updating existing 'huggingface' remote: $existingRemote -> $hfRemoteUrl" -ForegroundColor Yellow git remote set-url huggingface $hfRemoteUrl } else { Write-Host "Adding 'huggingface' remote: $hfRemoteUrl" -ForegroundColor Green git remote add huggingface $hfRemoteUrl } Write-Host "" Write-Host "=== Next steps ===" -ForegroundColor Cyan Write-Host "1. Create a Hugging Face token with 'write' permission:" Write-Host " https://huggingface.co/settings/tokens" -ForegroundColor Yellow Write-Host "2. Add it as a GitHub secret named 'HF_TOKEN':" Write-Host " gh secret set HF_TOKEN --repo $(git remote get-url origin | sed 's/.*github.com[\/:]//;s/\.git$//')" -ForegroundColor Yellow Write-Host "3. Commit and push the workflow to GitHub:" Write-Host " git add .github/workflows/hf-docker-space-deploy.yml" -ForegroundColor Yellow Write-Host " git commit -m 'ci: add HF Space autodeploy pipeline'" -ForegroundColor Yellow Write-Host " git push origin $Branch" -ForegroundColor Yellow Write-Host "" Write-Host "After that, every push to '$Branch' will auto-deploy to https://huggingface.co/spaces/$HFSpaceOwner/$HFSpaceName" -ForegroundColor Green