GitHub Actions

以下示例展示了如何将 Turborepo 与 GitHub Actions 结合使用。

对于给定的根目录 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

远程缓存

要将远程缓存与 GitHub Actions 结合使用,请将以下环境变量添加到你的 GitHub Actions 工作流程中,以便 turbo 命令可以使用它们。

  • TURBO_TOKEN - 用于访问远程缓存的 Bearer 令牌
  • TURBO_TEAM - 仓库所属的帐户

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

Vercel 仪表板 中为你的帐户创建一个作用域访问令牌

Vercel Access Tokens

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

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

GitHub Secrets GitHub Secrets Create

创建一个新的仓库变量(单击 Variables(变量) 选项卡),名为 TURBO_TEAM,并输入 你的团队 URL

须知: 

使用仓库变量而不是密钥可以防止 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 actions/cache 进行缓存

以下步骤示例说明了如何使用 actions/cache 在 GitHub 上缓存你的 monorepo 工件。

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

带有 build 脚本的 package.json 示例

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

配置你的 GitHub 管道,使其包含一个步骤,该步骤在你的 CI 文件的构建步骤之前使用 actions/cache@v4 操作。

  • 确保在 actions/cache 操作中设置的 path 属性与上面的输出位置匹配。在下面的示例中,path 设置为 .turbo
  • key 属性下声明当前运行的缓存键。在下面的示例中,我们使用了 runner os 和 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