GitHub Actions

以下示例展示了如何在 GitHub Actions 中使用 Turborepo。

对于给定的根目录 package.json

./package.json
{
  "name": "my-turborepo",
  "scripts": {
    "build": "turbo run build",
    "test": "turbo run test"
  },
  "devDependencies": {
    "turbo": "latest"
  }
}

以及 turbo.json

Turborepo logo
./turbo.json
{
  "$schema": "https://turbo.net.cn/schema.json",
  "tasks": {
    "build": {
      "outputs": [".next/**", "!.next/cache/**", "other-output-dirs/**"],
      "dependsOn": ["^build"]
    },
    "test": {
      "dependsOn": ["^build"]
    }
  }
}

在您的仓库中创建一个名为 .github/workflows/ci.yml 的文件,并添加以下内容

.github/workflows/ci.yml
name: CI
 
on:
  push:
    branches: ["main"]
  pull_request:
    types: [opened, synchronize]
 
jobs:
  build:
    name: Build and Test
    timeout-minutes: 15
    runs-on: ubuntu-latest
    # To use Remote Caching, uncomment the next lines and follow the steps below.
    # env:
    #  TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
    #  TURBO_TEAM: ${{ vars.TURBO_TEAM }}
 
    steps:
      - name: Check out code
        uses: actions/checkout@v4
        with:
          fetch-depth: 2
 
      - uses: pnpm/action-setup@v3
        with:
          version: 8
 
      - name: Setup Node.js environment
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'pnpm'
 
      - name: Install dependencies
        run: pnpm install
 
      - name: Build
        run: pnpm build
 
      - name: Test
        run: pnpm test

使用 Vercel 远程缓存进行远程缓存

要使用 GitHub Actions 进行远程缓存,请在您的 GitHub Actions 工作流中添加以下环境变量,以便您的 turbo 命令可以使用它们。

  • TURBO_TOKEN - 用于访问远程缓存的 Bearer 令牌
  • TURBO_TEAM - 与其共享构件的 Vercel 团队的 slug

要使用远程缓存,请检索您的提供商的远程缓存的团队和令牌。在此示例中,我们将使用 Vercel 远程缓存

Vercel Dashboard 中为您的帐户创建作用域访问令牌。

Vercel Access Tokens

将该值复制到安全的地方。您稍后会用到它。

转到您的 GitHub 仓库设置,然后点击 Secrets (密钥) 再点击 Actions (操作) 选项卡。创建一个名为 TURBO_TOKEN 的新密钥,然后输入您的作用域访问令牌的值。

GitHub Secrets GitHub Secrets Create

创建一个新的仓库变量 (点击 Variables (变量) 选项卡),名为 TURBO_TEAM,并将其设置为您的团队 slug - 即 您的团队 URLvercel.com/ 后面的部分。例如,vercel.com/acme 的 slug 是 acme

须知: 

使用仓库变量而不是密钥可以防止 GitHub Actions 在日志输出中审查您的团队名称。

GitHub Repository Variables

在您的 GitHub Actions 工作流的顶部,为使用 turbo 的作业提供以下环境变量

.github/workflows/ci.yml
# ...
 
jobs:
  build:
    name: Build and Test
    timeout-minutes: 15
    runs-on: ubuntu-latest
    # To use Turborepo Remote Caching, set the following environment variables for the job.
    env:
      TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
      TURBO_TEAM: ${{ vars.TURBO_TEAM }}
 
    steps:
      - name: Check out code
        uses: actions/checkout@v4
        with:
          fetch-depth: 2
    # ...

在可重用的工作流中使用远程缓存

有关将密钥传递给可重用工作流的信息,请参阅 GitHub 关于将密钥传递给嵌套工作流的文档

使用 GitHub actions/cache 进行远程缓存

以下步骤展示了如何使用 actions/cache 在 GitHub 上缓存您的 monorepo 构件。

提供一个将使用 Turborepo 运行任务的 package.json 脚本。

带有 build 脚本的示例 package.json

./package.json
{
  "name": "my-turborepo",
  "scripts": {
    "build": "turbo run build"
  },
  "devDependencies": {
    "turbo": "1.2.5"
  }
}

在 CI 文件的构建步骤之前,使用 actions/cache@v4 操作配置您的 GitHub 流水线,使其包含一个步骤。

  • 确保 actions/cache 操作中设置的 path 属性与上面输出的位置匹配。在下面的示例中,path 被设置为 .turbo
  • key 属性下为当前运行指定缓存键。在下面的示例中,我们使用了运行器操作系统和 GitHub sha 的组合作为缓存键。
  • restore-keys 属性下指定所需的缓存前缀模式。确保此模式在未来的 CI 运行中保持有效。在下面的示例中,我们使用了 ${{ runner.os }}-turbo- 作为缓存键前缀模式进行搜索。这使得我们可以在后续的 CI 运行中命中缓存,即使 github.sha 发生变化。

选择 .turbo 作为缓存文件夹的示例 ci yaml

.github/workflows/ci.yml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v4
 
      - name: Cache turbo build setup
        uses: actions/cache@v4
        with:
          path: .turbo
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
 
      - name: Setup Node.js environment
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'
 
      - name: Install dependencies
        run: npm install
 
      - name: Build
        run: npm run build