@ryoppippi

TurborepoのCacheをGithub Actionsで使う、無料で

18 May 2023 ・ 3 min read


TL;DR

はじめに

Turborepo は​ Vercel が​開発した​モノレポ環境用の​ビルドツールです。

https://turborepo.org/docs/getting-started

Turborepo には、​モノレポ環境での​ビルドを​高速化する​ための​機能、​Remote Caching と​いう​ものが​あります。

https://turbo.build/repo/docs/core-concepts/remote-caching

これを​用いる​ことで​キャッシュが​効くようになり、​CI が​爆速で​実行されるようになります。

しかし、​この機能を​使おうと​すると​ Vercel の​アカウントが​必要に​なります。 個人利用(Hobby)では​無料ですが、​会社(Pro)で​使うと​すると​ $20 per user / month と​いう​価格に​なります キャッシュするだけならまあまあ​高いですね。

Self-hosted Cache Server

解決策と​しては、​Cache Server を​自前で​建てる​方​法が​あります。 これは​公式にも​記述が​あります。 https://turbo.build/repo/docs/core-concepts/remote-caching#custom-remote-caches

また、​これを​試した​記事も​既に​あります。 https://zenn.dev/silverbirder/articles/af8bf125bd33ad https://zenn.dev/aiji42/articles/7bc1b6df91dd76

しかし、​サーバーを​別で​建てるのは​面倒ですし、​お金も​かかります。

Github Actions で​ Cache Server を​自動的に​建てる

そこで、​冒頭で​紹介した​ Action を​導入します(は​い​そこ、​Star が​少ないから​不安とか​言わない)。

https://github.com/dtinth/setup-github-actions-caching-for-turbo

これを​使うと、​Github Actions を​実行する​ときに、​自動的に​ Cache Server を​建ててくれます。 また、​キャッシュは​ Github Actions の​キャッシュに​保存されるので、​(同じ​ブランチの​ CI ならば)​次回以降は​キャッシュを​使い回す​ことで​爆速で​ CI が​実行されるようになります。 やったね!

on:
  - push
  - pull_request

permissions: read-all

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Launch Turbo Remote Cache Server
        uses: dtinth/setup-github-actions-caching-for-turbo@v1.1.0

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 8

      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: pnpm

      - name: Install dependencies
        run: pnpm i

      - name: Lint
        run: pnpm run lint

      - name: Build
        run: pnpm run build

ちなみに

↓ 別の​ Action も​ありましたが、​なぜか​自分の​環境では​動かなかったのと、​環境変数を​設定する​必要が​あるので​上の​ Action を​用いる​方が​楽だと​思います。 https://github.com/felixmosh/turborepo-gh-artifacts

おわりに

Have a nice Turbo Life!

comment on bluesky / twitter
CC BY-NC-SA 4.0 2022-PRESENT © ryoppippi