Quicker Netlify Deploys

I use Netlify to host small static sites like this one. It does its job well and is very convenient to use.

But I noticed that since the standard way of setting things up is to link Netlify directly with your Git repo, deploys could take a while. When you push your code, Netlify builds the site on their own infrastructure before deploying it.

I recently came across a very neat alternative where you can build your site locally and deploy it to Netlify directly with a single HTTP request. This is really convenient when you don’t need the overhead that comes with more careful deploy management.

The link above does a good job of describing the process, but the upshot is that the following command is all you need to have a deploy running live just a few seconds later.

curl -H "Content-Type: application/zip" \
     -H "Authorization: Bearer PERSONAL_ACCESS_TOKEN_FROM_STEP_1" \
     --data-binary "@FILE_NAME.zip" \
     https://api.netlify.com/api/v1/sites/SITE_NAME.netlify.com/deploys

Right now I use it like this (these build commands go in a justfile) to ensure that there’s a corresponding commit for every deployed version:

pub: no-uncommitted-changes build
  @rm -f dist.zip
  @zip -q -r dist.zip dist;
  @curl -H "Content-Type: application/zip" \
       -H "Authorization: Bearer $(cat .netlify-token)" \
        --data-binary "@dist.zip" \
       https://api.netlify.com/api/v1/sites/what.yuri.is/deploys | jq
  @rm -f dist.zip
  @echo ""
  @echo "Deployed!"

# Halts with an error if the repository contains uncommitted changes
no-uncommitted-changes:
  @ git diff --exit-code > /dev/null || (echo "Please commit changes to the following files before proceeding:" && git status --short)

build:
  zola build