Hub documentation

GitHub Actions

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

GitHub Actions

您可以使用 GitHub Actions 将您的 GitHub 仓库自动同步到 Hugging Face Hub。官方的 huggingface/hub-sync action 支持同步 Models、Datasets 和 Spaces。

对于无密钥发布——无需存储或轮换 HF_TOKEN 密钥——请参阅 Trusted Publishers,它会在每次运行开始时,将 GitHub Actions 内置的 OIDC 令牌交换为短时效、仓库范围的 Hub 令牌。

设置

  1. 创建一个具有目标仓库write权限的 Hugging Face 访问令牌。为了获得更好的安全性,请使用仅限定于您正在同步到的仓库的细粒度令牌。
  2. 在您的仓库设置中,将该令牌添加为名为 HF_TOKEN 的 GitHub 机密。
  3. 在您的仓库中添加一个工作流文件(例如 .github/workflows/sync-to-hub.yml)。

基本用法

name: Sync to Hugging Face Hub
on:
  push:
    branches: [main]

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: huggingface/[email protected]
        with:
          github_repo_id: ${{ github.repository }}
          huggingface_repo_id: username/repo-name
          hf_token: ${{ secrets.HF_TOKEN }}

默认情况下,这会同步到 Space。若要同步模型或数据集,请设置 repo_type 参数:

      - uses: huggingface/[email protected]
        with:
          github_repo_id: ${{ github.repository }}
          huggingface_repo_id: username/my-dataset
          hf_token: ${{ secrets.HF_TOKEN }}
          repo_type: dataset

参数

参数必需默认描述
github_repo_id是的—GitHub 仓库(使用 ${{ github.repository }})
huggingface_repo_id是的—Hub 上的目标仓库 (username/repo-name)
hf_token是的—Hugging Face 访问令牌
repo_typeNospacespace、model 或 dataset
space_sdkNogradiogradio、streamlit、docker 或 static
privateNofalse是否将仓库创建为私有
subdirectoryNo.同步特定子目录(适用于 monorepo)

该操作使用 hf CLI 将您的文件镜像到 Hub——它并非 git 到 git 的同步。它会自动排除 .github/ 和 .git/ 目录,并镜像删除操作(从 GitHub 中删除的文件也会从 Hub 中删除)。

对于更复杂的工作流(例如构建步骤、自定义上传逻辑),您可以直接在您的工作流中安装并使用 hf CLI。

有关 Spaces 的特定指南(文件大小限制、LFS 处理),请参阅 使用 GitHub Actions 管理 Spaces。

Update on GitHub