Max Schmitt

August 20 2020

How to deploy Sanity from GitHub Actions

Sanity is an amazing CMS that has found the perfect balance between customizability and development speed.

In this post I will show you how to deploy your Sanity CMS from GitHub Actions.

First, we will need to generate an auth token that allows us to deploy our Sanity project. This way we won't have to run sanity login in CI.

1. Generate SANITY_AUTH_TOKEN

To generate the auth token, head over to sanity.io, log in and then go to your project's settings. There, go to API > Tokens > Add new token. A modal will show up. Make sure to select "Deploy studio" under "Rights".

A modal showing configuration options for auth tokens on sanity.io

2. Configure GitHub Secrets

Next, head over to your GitHub repository. Go to Settings > Secrets and click New secret. Add here the auth token that you generated in the first step and call it SANITY_AUTH_TOKEN.

You can also add other environment variables that you might need for your build, just make sure to prefix them with SANITY_STUDIO_.

GitHub secrets configuration screen showing environment variables

3. Add GitHub Actions workflow for deploying Sanity

Now, just copy the following file into your repository. I've highlighted the section that creates the environment variables for your build. Make sure to customize it as you need it.

.github/workflows/main.yml

name: CI
on:
push:
branches: [master]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js environment
uses: actions/setup-node@v1.4.2
with:
node-version: 12.x
- name: Install dependencies
run: |
yarn
- name: Deploy Sanity
run: |
set -e
# Put your environment variables here. Don't forget
# to create secrets for them on GitHub:
# https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
cat << EOF > .env.production
SANITY_STUDIO_FRONTEND_PREVIEW_SECRET=${{ secrets.SANITY_STUDIO_FRONTEND_PREVIEW_SECRET }}
SANITY_STUDIO_FRONTEND_BASE_URL=${{ secrets.SANITY_STUDIO_FRONTEND_BASE_URL }}
EOF
# Make sure to add a secret for SANITY_AUTH_TOKEN
SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" yarn sanity deploy

And that's all it takes to add Sanity deployment to your CI on GitHub!