Spaces:
Sleeping
Sleeping
| name: Sync to Hugging Face Hub | |
| on: | |
| push: | |
| branches: [ main ] | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| env: | |
| HF_REPO_TYPE: space | |
| jobs: | |
| sync: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.12' | |
| - name: Install huggingface_hub | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install huggingface_hub | |
| - name: Validate secrets | |
| env: | |
| HF_TOKEN: ${{ secrets.HF_TOKEN }} | |
| HF_REPO: ${{ secrets.HF_REPO }} | |
| run: | | |
| if [ -z "$HF_TOKEN" ]; then | |
| echo "HF_TOKEN secret is not set" | |
| exit 1 | |
| fi | |
| if [ -z "$HF_REPO" ]; then | |
| echo "HF_REPO secret is not set (should be like: username/repo-name)" | |
| exit 1 | |
| fi | |
| - name: Create or verify Hugging Face repo | |
| env: | |
| HF_TOKEN: ${{ secrets.HF_TOKEN }} | |
| HF_REPO: ${{ secrets.HF_REPO }} | |
| HF_REPO_TYPE: ${{ env.HF_REPO_TYPE }} | |
| run: | | |
| python3 -c ' | |
| import os | |
| from huggingface_hub import HfApi, create_repo | |
| api = HfApi() | |
| repo_id = os.environ["HF_REPO"] | |
| repo_type = os.environ.get("HF_REPO_TYPE", "model") | |
| token = os.environ["HF_TOKEN"] | |
| try: | |
| api.repo_info(repo_id=repo_id, repo_type=repo_type, token=token) | |
| print(f"{repo_type.capitalize()} {repo_id} exists.") | |
| except: | |
| print(f"Creating {repo_type} {repo_id}...") | |
| create_repo(repo_id=repo_id, token=token, repo_type=repo_type, private=False, exist_ok=True) | |
| print(f"{repo_type.capitalize()} {repo_id} created.") | |
| ' | |
| - name: Mirror to Hugging Face | |
| env: | |
| HF_TOKEN: ${{ secrets.HF_TOKEN }} | |
| HF_REPO: ${{ secrets.HF_REPO }} | |
| HF_REPO_TYPE: ${{ env.HF_REPO_TYPE }} | |
| run: | | |
| set -e | |
| # Create temp directory | |
| TMP_DIR=$(mktemp -d) | |
| echo "Using temp directory: $TMP_DIR" | |
| # Copy files (excluding .git) | |
| cp -r . "$TMP_DIR" | |
| rm -rf "$TMP_DIR/.git" | |
| cd "$TMP_DIR" | |
| # Initialize git | |
| git init | |
| git config user.name "github-actions" | |
| git config user.email "github-actions@users.noreply.github.com" | |
| # Set remote URL based on repo type | |
| if [ "$HF_REPO_TYPE" = "space" ]; then | |
| REMOTE_URL="https://user:${HF_TOKEN}@huggingface.co/spaces/${HF_REPO}" | |
| else | |
| REMOTE_URL="https://user:${HF_TOKEN}@huggingface.co/${HF_REPO}" | |
| fi | |
| git remote add origin "$REMOTE_URL" | |
| git add . | |
| git commit -m "Sync from GitHub $(date -u)" | |
| git branch -M main | |
| git push origin main --force | |
| echo "Successfully synced to Hugging Face!" | |
| - name: Summary | |
| run: echo "Sync completed successfully" |