File size: 3,897 Bytes
7fcc15d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<#
.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