Speed Up Your Vite Builds with Caching in GitHub Actions

@fakhrulnugrohoAugust 28, 2024

🚀 Speed Up Your Vite Builds with Caching in GitHub Actions

Hey there! If you’re using GitHub Actions to build your project, caching can be a game-changer. It helps speed up your builds by saving and reusing previous build outputs. Let’s walk through how you can set up caching for Vite builds with Yarn in a fun and easy way!

🛠️ Setting Up Your GitHub Actions Workflow

  1. Create or Update Your Workflow File

    First things first, you need a GitHub Actions workflow file. This file lives in .github/workflows/. Don’t worry if you’re new to this—just follow this example and you’ll be up and running in no time!

    YAML
    name: Build and Cache Vite
    
    on:
      push:
        branches:
          - main
      pull_request:
        branches:
          - main
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - name: Checkout code
            uses: actions/checkout@v3
    
          - name: Set up Node.js
            uses: actions/setup-node@v3
            with:
              node-version: "18" # Specify your Node.js version
    
          - name: Cache Yarn modules
            uses: actions/cache@v3
            with:
              path: |
                ~/.cache/yarn
                ~/.yarn/cache
              key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
              restore-keys: |
                ${{ runner.os }}-yarn-
    
          - name: Install dependencies
            run: yarn install
    
          - name: Cache Vite build output
            uses: actions/cache@v3
            with:
              path: |
                dist
              key: ${{ runner.os }}-vite-${{ hashFiles('**/vite.config.js') }}
              restore-keys: |
                ${{ runner.os }}-vite-
    
          - name: Build project
            run: yarn build
    
          - name: Deploy or run tests
            run: echo "Deploy or run tests here"
    
  2. Understand Each Step

    Let’s break down what each step does so you know exactly what’s happening:

    • Checkout code: This gets your project files from GitHub. Ready to dive into your code? This step makes it happen!
    • Set up Node.js: Sets up the version of Node.js you need. Pick your version and let’s get started!
    • Cache Yarn modules: Saves Yarn’s cache to make installs faster. Think of it as a secret stash of your project’s goodies!
    • Install dependencies: Runs yarn install to get all the packages you need. It’s like shopping for your project!
    • Cache Vite build output: Saves the results of your build process. This way, you don’t have to rebuild everything from scratch each time.
    • Build project: Executes the build command with Vite. Watch your project come to life!
    • Deploy or run tests: This is where you can add steps to deploy your project or run tests. Time to see your project in action or make sure everything is running smoothly!
  3. Pro Tips

    • Cache Key: The cache key helps GitHub Actions know when to use or update the cache. Think of it as your project’s unique ID for caching.
    • Restore Keys: If an exact match isn’t found, restore keys help GitHub Actions find the closest cache. It’s like having a backup plan!

🎉 You’re All Set!

By setting up caching for your Vite builds, you’ll make your builds faster and more efficient. Give it a try and see how much time you save!

Got any questions or need more help? Drop them below or reach out—I’m here to help!