← blog blog / gatsby-incremental-faster-builds-github-actions.md

How to cache your Gatsby site for faster builds in Github Actions?

We all start small, but once our content grows, soon we'll be finding ourselves with 300 pages at hand. This severly impacts the build time of the blog, unless you host it in Gatsby Cloud, which I hear has 100x faster build times than normal conventional builds. Click to read more...

How to cache your Gatsby site for faster builds in Github Actions?

We all start small, but once our content grows, soon we’ll be finding ourselves with 300 pages at hand. This severly impacts the build time of the blog, unless you host it in Gatsby Cloud, which I hear has 100x faster build times than normal conventional builds.

My workflow

I use Github actions for my CI/CD pipeline. It’s pretty cool and awesome but I’m planning to use Netlify as well. So, my workflow is like this,

  1. I write a post or update content
  2. I push to github
  3. An action runs that takes care of the build
  4. It does this from the scratch everytime
  5. Takes 3 to 4 mins to complete

We can optimize this build time by caching our assets and other folders that do not change often. For now, we will be caching our public and the .cache folder.

Finally, some code

This is the code that can be used to cache and do an incremental build using Github Actions. This goes into my action.yml file.

name: Gatsby Publish

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Caching Gatsby
        id: gatsby-cache-build
        uses: actions/cache@v2
        with:
          path: |
            public
            .cache
          key: ${{ runner.os }}-gatsby-build-${{ github.run_id }}
          restore-keys: |
            ${{ runner.os }}-gatsby-build-
      - name: Build
        run: npm install --legacy-peer-deps && npm run build --log-pages
        env:
          GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES: true
          CI: true

As you can see, GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES: true this needs to be set to true for this to work properly. Hope this helps, do follow my blog for more updates and contents like this.

Thanks for reading!

I write about frontend craft, React, TypeScript, and the web. Found this useful? Let me know.

@samuellawrentz →

$ echo "enjoyed this post?" · subscribe via rss ↗

$ git log --oneline --grep="gatsby"

More articles

cd ../blog →
  1. 35668b9 Reviving My Website: A Tale of Tech and Determination

    Oct 28, 2023 3 min read tag: gatsbytag: static site

    Reviving My Website: A Tale of Tech and Determination
  2. a2e5f33 Dynamic OG images for your Gatsby blog

    Jul 27, 2022 5 min read tag: gatsbytag: node

    Dynamic OG images for your Gatsby blog
  3. 27baba9 How I started a tech blog?

    Jul 15, 2022 3 min read tag: gatsby

    How I started a tech blog?
  4. 4a412f4 The Five Ways to Bring Your Boss a Problem

    Jul 07, 2026 3 min read tag: careertag: leadership

    The Five Ways to Bring Your Boss a Problem

$ giscus --load ./comments

00:00

This helps me increase the session time of my site. Thank you!

Can you stay a bit longer?