GitHub Actions

다음 예제는 Turborepo를 GitHub Actions와 함께 사용하는 방법을 보여줍니다.

주어진 루트 package.json의 경우:

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

And a turbo.json:

Turborepo logo
./turbo.json
{
  "$schema": "https://turborepo.com/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 Remote Cache를 사용한 Remote Caching

GitHub Actions와 함께 Remote Caching을 사용하려면 turbo 명령에서 사용할 수 있도록 GitHub Actions 워크플로에 다음 환경 변수를 추가합니다.

  • TURBO_TOKEN - Remote Cache에 액세스하기 위한 Bearer 토큰
  • TURBO_TEAM - 아티팩트를 공유할 Vercel 팀의 슬러그

Remote Caching을 사용하려면 제공자의 Remote Cache에 대한 팀과 토큰을 검색합니다. 이 예제에서는 Vercel Remote Cache를 사용합니다.

Vercel Dashboard에서 계정에 대한 Scoped Access Token을 생성합니다.

Vercel Access Tokens

값을 안전한 곳에 복사하세요. 잠시 후에 필요합니다.

GitHub 저장소 설정으로 이동하여 Secrets를 클릭한 다음 Actions 탭을 클릭합니다. TURBO_TOKEN이라는 새 비밀을 생성하고 Scoped Access Token의 값을 입력합니다.

GitHub Secrets GitHub Secrets Create

(Variables 탭을 클릭하여) TURBO_TEAM이라는 새 저장소 변수를 생성하고 팀 URL에서 vercel.com/ 뒤의 부분인 팀 슬러그로 설정합니다. 예를 들어, vercel.com/acme의 슬러그는 acme입니다.

Good to know: 

비밀 대신 저장소 변수를 사용하면 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
    # ...

재사용 가능한 워크플로와 함께 Remote Caching 사용

재사용 가능한 워크플로에 비밀을 전달하는 방법에 대한 자세한 내용은 중첩된 워크플로에 비밀 전달에 대한 GitHub 문서를 참조하세요.

GitHub actions/cache를 사용한 Remote Caching

다음 단계는 actions/cache를 사용하여 GitHub에서 모노레포 아티팩트를 캐시하는 방법을 보여줍니다.

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 속성 아래에 현재 실행에 대한 캐시 키를 명시하세요. 아래 예제에서는 러너 os와 GitHub sha의 조합을 캐시 키로 사용했습니다.
  • restore-keys 속성 아래에 원하는 캐시 접두사 패턴을 명시하세요. 이 패턴이 향후 CI 실행에 대해 유효하게 유지되는지 확인하세요. 아래 예제에서는 ${{ runner.os }}-turbo-를 검색할 캐시 키 접두사 패턴으로 사용했습니다. 이렇게 하면 github.sha가 변경되더라도 이후 CI 실행에서 캐시에 접근할 수 있습니다.

선택한 캐시 폴더로 .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