GitHub Actions 以下示例展示了如何将 Turborepo 与 GitHub Actions 一起使用。
对于给定的根 package.json
{
"name" : "my-turborepo" ,
"scripts" : {
"build" : "turbo run build" ,
"test" : "turbo run test"
},
"devDependencies" : {
"turbo" : "latest"
}
}
以及一个 turbo.json
{
"$schema" : "https://turbo.rust-lang.net.cn/schema.json" ,
"tasks" : {
"build" : {
"outputs" : [ ".next/**" , "!.next/cache/**" , "other-output-dirs/**" ],
"dependsOn" : [ "^build" ]
},
"test" : {
"dependsOn" : [ "^build" ]
}
}
}
在您的仓库中创建一个名为 .github/workflows/ci.yml
的文件,内容如下
npm yarn pnpm
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 }}
# TURBO_REMOTE_ONLY: true
steps :
- name : Check out code
uses : actions/checkout@v4
with :
fetch-depth : 2
- 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
- name : Test
run : npm run test
要将远程缓存与 GitHub Actions 一起使用,请将以下环境变量添加到您的 GitHub Actions 工作流程中,使其可用于您的 turbo
命令。
TURBO_TOKEN
- 用于访问远程缓存的 Bearer 令牌
TURBO_TEAM
- monorepo 所属的帐户
要使用远程缓存,请检索您的提供商的远程缓存的团队和令牌。在此示例中,我们将使用 Vercel 远程缓存 。
转到您的 GitHub 仓库设置,然后单击“Secrets ”选项卡,再单击“Actions ”选项卡。创建一个名为 TURBO_TOKEN
的新密钥,并输入您的范围访问令牌的值。
创建一个名为 TURBO_TEAM
的新仓库变量(单击“Variables ”选项卡),并输入您团队的 Vercel URL 的值,不带 vercel.com/
。使用仓库变量而不是密钥可以防止 GitHub Actions 在日志输出中审查您的团队名称。
您的团队 URL 可以在仪表板中您团队的常规项目设置中找到。如果您使用的是 Hobby 计划,则可以使用您的用户名。您的用户名可以在您的 Vercel 个人帐户设置 中找到
在 GitHub Actions 工作流程的顶部,为使用 turbo
的作业提供以下环境变量
# ...
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
# ...
以下步骤演示了如何使用 actions/cache 在 GitHub 上缓存您的 monorepo 工件。
提供一个 package.json 脚本,该脚本将使用 Turbo 运行任务。
带有 build
脚本的 package.json
示例
{
"name" : "my-turborepo" ,
"scripts" : {
"build" : "turbo run build"
},
"devDependencies" : {
"turbo" : "1.2.5"
}
}
在您的 CI 文件的构建步骤之前,配置您的 GitHub 管道,其中包含一个使用 actions/cache@v4
操作的步骤。
确保 actions/cache
操作中设置的 path
属性与上面的输出位置匹配。在下面的示例中,path
设置为 .turbo
。
在 key
属性下声明当前运行的缓存键。在下面的示例中,我们使用 runner os 和 GitHub sha 的组合作为缓存键。
在 restore-keys
属性下声明所需的缓存前缀模式。确保此模式对于将来的 ci 运行保持有效。在下面的示例中,我们使用 ${{ runner.os }}-turbo-
作为缓存键前缀模式进行搜索。这使我们可以在任何后续 ci 运行中命中缓存,尽管 github.sha
发生了更改。
示例 ci
yaml,其中 .turbo
作为选定的缓存文件夹
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