Home

Awesome

gatsby-s3-action

Deploy a Gatsby site to an AWS S3 bucket and optionally invalidate a CloudFront distribution

Please read the notes on the AWS Setup below.

For a full step by step guide for setting up from scratch please take a look at GitHub Actions powered Gatsby AWS how-to guide.

QUICK RECIPE: S3 Static Hosting, no CloudFront

name: Deploy

on:
  push:
    branches:
      - main # could be 'master' on older repos
jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
      - name: Build
        run: |
          npm ci
          npm run build
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: eu-west-2
      - name: Deploy
        uses: jonelantha/gatsby-s3-action@v3
        with:
          dest-s3-bucket: your_bucket

QUICK RECIPE: With CloudFront

Add the cloudfront-id-to-invalidate parameter to specify the ID of a distribution to be invalidated after deployment.

     - name: Deploy
        uses: jonelantha/gatsby-s3-action@v3
        with:
          dest-s3-bucket: your_bucket
          cloudfront-id-to-invalidate: CLOUDFRONTID

QUICK RECIPE: Deploy to a sub-directory on S3

Add the dest-s3-path parameter to specify a sub-directory to copy to in your bucket.

     - name: Deploy
        uses: jonelantha/gatsby-s3-action@v3
        with:
          dest-s3-bucket: your_bucket
          dest-s3-path: blog/files

QUICK RECIPE: With a non-standard Gatsby build directory (default is ./public/):

Gatsby builds to ./public by default. If you've changed the build directory to something else then use public-source-path to specify that directory:

     - name: Deploy
        uses: jonelantha/gatsby-s3-action@v3
        with:
          dest-s3-bucket: your_bucket
          public-source-path: ./build/

AWS Setup

AWS IAM Secrets

You'll need to setup an AWS IAM user with Programmatic Access and then configure the AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY in the Settings/Secrets area of the repo. Ideally you should follow Amazon IAM best practices and grant least privileges to the user:

For a complete walkthrough of setting up the user, please see the first section of the associated Setting up Github Actions for Gatsby guide

S3

CloudFront

Your CloudFront distribution will need read access to your S3 bucket. More information in the AWS Gatsby S3 CloudFront guide but in short this is easy to configure when you create your CloudFront distribution, just make sure you use the following settings on the Create Distribution screen:

Also, you'll need to setup a CloudFront Function on the CloudFront distribution to properly handle serving up index.html files. More information in the Gatsby CloudFront Function guide. Alternatively if you'd prefer to set up index handling using a lambda@edge function, please see the Gatsby CloudFront Lambda guide

Redirects

If you plan to use Gatsby redirects you'll need to use a Gatsby redirect plugin such as one of the following:

Parameters Reference

ArgumentStatusDescription
dest-s3-bucketRequiredThe destination S3 Bucket
dest-s3-pathOptionalThe destination S3 Path (defaults to root)
cloudfront-id-to-invalidateOptionalThe ID of the CloudFront distribution to invalidate.
cloudfront-path-to-invalidateOptionalThe path to invalidate on the CloudFront distribution. See the CloudFront Invalidation guide for information on the format (default: /*)
public-source-pathOptionalThe path to your gatsby ./public directory. Default: is ./public/
sync-deleteOptionalBoolean: delete files on S3 not in the latest Gatsby build (defaults to 'true')
only-size-changedOptionalBoolean, sync only files where size has changed since the last deployment. Can speed up deployments for larger sites, however in rare cases could miss files where the content changed without the file size changing (one character corrections for example). Defaults to 'false'
browser-cache-durationOptionalThe cache duration (in seconds) to instruct browsers to cache files for. This is only for files which should be cached as per Gatsby caching recommendations. Default is 31536000 (1 year)
cdn-cache-durationOptionalThe cache duration (in seconds) to instruct a CDN (if there is one) to cache files for. If on a development environment and you want to avoid issuing CloudFront invalidations you could set this to 0. Default is 31536000 (1 year)
debugOptionalBoolean: enable debug logging (defaults to 'false')