Turborepo Remote Cache (TRRC)

repository·main·Indexed 23 days ago

https://github.com/ducktors/turborepo-remote-cache

An open-source, self-hosted alternative to Vercel's remote cache server for Turborepo. Version 2.11.5 supports multiple storage providers including local, S3, Google Cloud Storage, and Azure Blob Storage. It features three authentication modes (static, JWT, and none), artifact integrity verification via signing, and a read-only mode for CI-populated caches. Deployment options include Docker, DigitalOcean, Vercel, AWS Lambda, and GCP Cloud Run.

Tokens
10.1K
Snippets
14
Records
67
Agent score
78%

What's inside turborepo-remote-cache

  1. What is Turborepo Remote Cache

    main
    Turborepo Remote Cache (TRRC) is an open-source implementation of the Turborepo custom remote cache server. It serves as a self-hosted alternative to Vercel's official cache server, allowing developers to host their own remote cache for Turborepo monorepos. The project supports multiple storage providers and various deployment environments.
  2. Overview of Turborepo Remote Cache (TRRC)

    main
    Turborepo Remote Cache (TRRC) is an open-source implementation of the Turborepo custom remote cache server. It serves as a self-hosted alternative to Vercel's official cache server, allowing developers to host their own remote caching infrastructure. The project supports multiple storage providers and various deployment environments, often providing one-click deployment options.
  3. Configure HTTP/2 for large artifact support in Cloud Run

    main

    Cloud Run imposes a 32 MiB request size limit if HTTP/2 is not enabled. To support Turborepo artifacts larger than 32 MiB, you must enable HTTP/2 support in your deployment.

    To do this:

    1. Set the environment variable HTTP2 to true.
    2. In the Cloud Run Networking tab, enable the Use HTTP/2 end-to-end option.

    If you choose not to use HTTP/2, you must remove the HTTP2 environment variable and disable the Use HTTP/2 end-to-end option in the Networking tab, but be aware that artifacts will be limited to 32 MiB.

  4. Configure Authentication Modes

    main

    The server supports three authentication modes via the AUTH_MODE environment variable. Choose the mode that fits your security requirements:

    • static: Uses a secret token for authentication. You must provide one or more tokens via TURBO_TOKEN (comma-separated). This is the default mode.
    • jwt: Uses JSON Web Tokens for authentication. Requires JWKS_URL to retrieve public keys for verification. You can optionally configure JWT_ISSUER, JWT_AUDIENCE, and scope requirements using JWT_READ_SCOPES and JWT_WRITE_SCOPES.
    • none: Disables authentication (not recommended for production).

    If using static mode, ensure the TURBO_TOKEN matches the token parameter provided in your Turborepo build script.

  5. Enable custom remote caching in your Turborepo project

    main

    To use a custom turborepo-remote-cache server instead of Vercel's, you must manually configure the connection. Note that turbo login and turbo link only work with Vercel's remote cache.

    1. Create a configuration file

    Create a .turbo/config.json file at the root of your repository and specify the apiurl:

    {
      "apiurl": "http://cache.ducktors.dev"
    }

    2. Provide credentials

    You must provide TURBO_TEAM and TURBO_TOKEN using one of the following three methods. Prefer using environment variables for security.

    Set TURBO_TEAM and TURBO_TOKEN as environment variables in your local shell or CI pipeline. These will override any values found in .turbo/config.json.

    Method B: Config File

    Add teamslug and token directly to .turbo/config.json.

    Warning: Including the token in this file is less secure if you commit the file to version control.

    {
      "apiurl": "http://cache.ducktors.dev",
      "teamslug": "ducktors",
      "token": "myGeneratedToken"
    }

    Pass --team and --token as flags to your turbo commands in package.json.

    Warning: This is insecure as the token is committed to the repository.

    {
      "scripts": {
        "build": "turbo run build --team=\"ducktors\" --token=\"myGeneratedToken\""
      }
    }
    {
      "apiurl": "http://cache.ducktors.dev"
    }
  6. Configure DigitalOcean Spaces

    main

    DigitalOcean Spaces is supported as an S3-compatible provider. Follow these steps:

    1. Create a Space.
    2. Generate a new spaces access key.
    3. Set the following environment variables:
      • STORAGE_PROVIDER=s3
      • AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from your access key.
      • AWS_REGION=us-east-1 (Note: DigitalOcean uses us-east-1 for SDK compatibility).
      • STORAGE_PATH: The name of your Space.
      • S3_ENDPOINT: The endpoint of your Space (e.g., https://nyc3.digitaloceanspaces.com).
  7. Deploy the cache server as a Google Cloud Run service

    main

    This guide outlines how to deploy turborepo-remote-cache to Google Cloud using a Cloud Run service and a Cloud Storage bucket for persistence.

    Step 1: Create a Cloud Storage Bucket

    Create a bucket in the Cloud Storage console with these settings:

    • Location type: Regional (closest to you).
    • Storage class: Standard.
    • Prevent public access: Enabled with Uniform access control.
    • Data protection: Retention duration set to 0 days.
    • Lifecycle rule (Recommended): Set a rule to automatically delete objects older than a few weeks to manage stale cache data.

    Step 2: Deploy the Cloud Run Service

    Deploy a new service in the Cloud Run console using the following configuration:

    • Container Image: ducktors/turborepo-remote-cache:latest
    • Region: Your closest region.
    • Authentication: Disable Use Cloud IAM to authenticate incoming requests.
    • Billing: Select Request-based billing.
    • Scaling:
      • Minimum instances: 0
      • Maximum instances: 1 (The service uses a shared bucket and has not been tested with multiple concurrent instances).
    • Ingress: Select All.
    • Networking: Enable Use HTTP/2 end-to-end.

    Container Settings

    • Port: 3000
    • Volumes:
      • Add a volume of type Cloud Storage bucket using the bucket created in Step 1.
      • Mount the volume at path: /turbo-cache
    • Environment Variables: | Variable | Value | |---|---| | STORAGE_PROVIDER | local | | STORAGE_PATH | /turbo-cache | | STORAGE_PATH_USE_TMP_FOLDER | false | | TURBO_TOKEN | your_secret_key (a strong, random secret) | | HTTP2 | true |

    Step 3: Configure Turborepo

    Copy the Cloud Run service URL (ending in .run.app). Use this URL and your TURBO_TOKEN to configure your Turborepo project as described in the Enable custom remote caching guide.

  8. Configure Google Cloud Storage (GCS)

    main

    To use Google Cloud Storage, set STORAGE_PROVIDER=google-cloud-storage and provide a bucket name via STORAGE_PATH. You must grant the Storage Object Admin role to your service account for the target bucket.

    Option 1: Using static Service Account credentials

    Use this method by providing the details from your service account JSON key file directly in your environment variables.

    Option 2: Using Application Default Credentials (ADC)

    Leave the GCS_* environment variables empty to allow the SDK to discover credentials automatically (e.g., via IAM roles or local ADC setup).

    # Option 1: Static Credentials
    STORAGE_PROVIDER=google-cloud-storage
    STORAGE_PATH=<name-of-the-bucket>
    GCS_PROJECT_ID=<project_id>
    GCS_CLIENT_EMAIL=<client_email>
    GCS_PRIVATE_KEY=<private_key>
    
    # Option 2: Application Default Credentials (ADC)
    STORAGE_PROVIDER=google-cloud-storage
    STORAGE_PATH=<name-of-the-bucket>
    GCS_PROJECT_ID=
    GCS_CLIENT_EMAIL=
    GCS_PRIVATE_KEY=
  9. Access TRRC Documentation

    main

    Detailed documentation for TRRC is hosted externally. Key topics include:

    • Supported Storage Providers: Information on which backends can be used to store cache artifacts.
    • Environment Variables: A complete list of configuration keys required for the server.
    • Deployment Instructions: Guides for various deployment environments.
    • Custom Remote Caching: How to enable and configure custom remote caching within your Turborepo monorepo.

    Full documentation is available here