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.dev.org.tw/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
的新密碼,並輸入您的範圍存取權杖值。
建立一個新的儲存庫變數(按一下 Variables 索引標籤),名為 TURBO_TEAM
並輸入您團隊的 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 檔案的建置步驟之前,使用 actions/cache@v4
動作設定您的 GitHub 管線。
請確保 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