renminwansui1976 commited on
Commit
6f503f1
·
unverified ·
1 Parent(s): 56f1ebb

更新 deploy-hf-space.yml

Browse files
.github/workflows/deploy-hf-space.yml CHANGED
@@ -20,15 +20,73 @@ jobs:
20
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
21
  HF_SPACE_ID: ${{ secrets.HF_SPACE_ID }}
22
  run: |
23
- test -n "$HF_TOKEN" || (echo "Missing secret HF_TOKEN" && exit 1)
24
- test -n "$HF_SPACE_ID" || (echo "Missing secret HF_SPACE_ID" && exit 1)
25
 
26
  - name: Push repository to Hugging Face Space
27
  env:
28
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
29
  HF_SPACE_ID: ${{ secrets.HF_SPACE_ID }}
30
  run: |
31
- git config --global user.name "github-actions[bot]"
32
  git config --global user.email "github-actions[bot]@users.noreply.github.com"
33
  git remote add hf "https://oauth2:${HF_TOKEN}@huggingface.co/spaces/${HF_SPACE_ID}"
34
- git push hf HEAD:main --force
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
21
  HF_SPACE_ID: ${{ secrets.HF_SPACE_ID }}
22
  run: |
23
+ test -n "$HF_TOKEN" || (echo "Missing secret: HF_TOKEN" && exit 1)
24
+ test -n "$HF_SPACE_ID" || (echo "Missing secret: HF_SPACE_ID" && exit 1)
25
 
26
  - name: Push repository to Hugging Face Space
27
  env:
28
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
29
  HF_SPACE_ID: ${{ secrets.HF_SPACE_ID }}
30
  run: |
31
+ git config --global user.name "github-actions[bot]"
32
  git config --global user.email "github-actions[bot]@users.noreply.github.com"
33
  git remote add hf "https://oauth2:${HF_TOKEN}@huggingface.co/spaces/${HF_SPACE_ID}"
34
+
35
+ # Push and retry once on transient failure
36
+ git push hf HEAD:main --force || (sleep 5 && git push hf HEAD:main --force)
37
+
38
+ - name: Wake up Space (resume if paused)
39
+ env:
40
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
41
+ HF_SPACE_ID: ${{ secrets.HF_SPACE_ID }}
42
+ run: |
43
+ # Resume the space if it is paused/sleeping
44
+ RESUME_RESP=$(curl -s -o /dev/null -w "%{http_code}" \
45
+ -X POST "https://huggingface.co/api/spaces/${HF_SPACE_ID}/resume" \
46
+ -H "Authorization: Bearer ${HF_TOKEN}")
47
+ echo "Resume response: $RESUME_RESP"
48
+
49
+ - name: Trigger rebuild via HF Spaces API
50
+ env:
51
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
52
+ HF_SPACE_ID: ${{ secrets.HF_SPACE_ID }}
53
+ run: |
54
+ # Explicitly request a factory-reboot so HF always triggers a fresh build
55
+ RESTART_RESP=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
56
+ -X POST "https://huggingface.co/api/spaces/${HF_SPACE_ID}/restart?factory=true" \
57
+ -H "Authorization: Bearer ${HF_TOKEN}" \
58
+ -H "Content-Type: application/json")
59
+
60
+ HTTP_STATUS=$(echo "$RESTART_RESP" | grep "HTTP_STATUS" | cut -d: -f2)
61
+ BODY=$(echo "$RESTART_RESP" | grep -v "HTTP_STATUS")
62
+
63
+ echo "Restart response ($HTTP_STATUS): $BODY"
64
+
65
+ # 200 = restarted, 401/403 = token lacks write permission
66
+ if [ "$HTTP_STATUS" != "200" ]; then
67
+ echo "⚠️ Restart API returned $HTTP_STATUS — build may still start from the git push alone."
68
+ fi
69
+
70
+ - name: Wait for build to start and report status
71
+ env:
72
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
73
+ HF_SPACE_ID: ${{ secrets.HF_SPACE_ID }}
74
+ run: |
75
+ echo "Waiting for Space build to start..."
76
+ sleep 10
77
+
78
+ STATUS_JSON=$(curl -s \
79
+ "https://huggingface.co/api/spaces/${HF_SPACE_ID}/runtime" \
80
+ -H "Authorization: Bearer ${HF_TOKEN}")
81
+
82
+ STAGE=$(echo "$STATUS_JSON" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('stage','unknown'))" 2>/dev/null || echo "unknown")
83
+ echo "Space stage: $STAGE"
84
+
85
+ case "$STAGE" in
86
+ BUILDING|RUNNING|RUNNING_BUILDING)
87
+ echo "✅ Build triggered successfully — stage: $STAGE" ;;
88
+ PAUSED|STOPPED|ERROR|RUNTIME_ERROR)
89
+ echo "⚠️ Unexpected stage: $STAGE — check the Space logs on HuggingFace." ;;
90
+ *)
91
+ echo "ℹ️ Stage: $STAGE" ;;
92
+ esac