File size: 2,367 Bytes
532a13a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
name: Deploy ke Hugging Face Space

env:
  # Ganti dengan username dan nama Static Space milikmu. Contoh: namauser/chat-pgvector-praktikum
  HF_SPACE_ID: "GANTI_USERNAME/GANTI_NAMA_SPACE"

on:
  push:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: deploy-hugging-face-space
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Ambil kode dari GitHub
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Siapkan Python
        uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
        with:
          python-version: "3.12"

      - name: Pasang pustaka Hugging Face
        run: python -m pip install "huggingface_hub==1.19.0"

      - name: Unggah enam berkas ke Space
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        run: |
          python - <<'PY'
          import os
          from pathlib import Path

          from huggingface_hub import HfApi

          repo_id = os.environ["HF_SPACE_ID"].strip()
          if "GANTI_" in repo_id or repo_id.count("/") != 1:
              raise SystemExit("Isi HF_SPACE_ID dengan username/nama-space milikmu.")
          if not os.environ.get("HF_TOKEN"):
              raise SystemExit("Buat repository secret bernama HF_TOKEN di GitHub.")

          files = ["README.md", "index.html", "style.css", "app.js",
                   "engine-worker.js", "data.json"]
          missing = [name for name in files if not Path(name).is_file()]
          if missing:
              raise SystemExit("Berkas belum ada di root repo: " + ", ".join(missing))

          api = HfApi()
          if api.space_info(repo_id=repo_id).sdk != "static":
              raise SystemExit("HF_SPACE_ID harus mengarah ke Static Space yang sudah dibuat.")

          commit = api.upload_folder(
              repo_id=repo_id,
              repo_type="space",
              folder_path=".",
              revision="main",
              allow_patterns=files,
              commit_message="Deploy dari GitHub " + os.environ["GITHUB_SHA"][:12],
          )
          print("Berkas terkirim:", commit.commit_url)
          print("Buka tab App:", f"https://huggingface.co/spaces/{repo_id}")
          PY