How to schedule JAMstack deploys with Netlify and GitHub
Tags: netlify, jamstack, webdev
Published: 2020-09-15 14:26:57

---
title: How to schedule JAMstack deploys with Netlify and GitHub
published: true
description: A GitHub action workflow template to schedule Netlify site rebuilds.
tags: netlify, jamstack, webdev
canonical_url: https://www.stefanjudis.com/snippets/how-to-schedule-jamstack-deploys-with-netlify-and-github/
---

One thing that got me excited about the JAMstack movement is the idea of a content mesh. I heard the term first when [the Gatsby folks talked about combining multiple data sources into static JAMstack sites](https://www.gatsbyjs.com/blog/2018-10-04-journey-to-the-content-mesh/), which became one of Gatsby's core strengths.

The idea behind a content mesh is that you use APIs to collect and display third-party-data during build time. 

[My site](https://www.stefanjudis.com) runs on Netlify and is currently a content mesh of:

- [Buttondown](https://buttondown.email/)'s API to show a [newsletter count in the footer](#newsletterForm)
- [Feedly](https://developer.feedly.com/) to display [a webring of RSS feeds that I'm subscribed to](/webring/)
- [Contentful](https://www.contentful.com) to write and render [my blog posts](https://www.stefanjudis.com/blog/)
- [Netlify Analytics](https://www.netlify.com) to fetch and render [my "popular articles" page](https://www.stefanjudis.com/popular-posts/)
- [GitHub](https://docs.github.com/en/rest) to automatically enrich [my projects with stargazer data](https://www.stefanjudis.com/projects/)

Honestly, I think that's pretty cool. But suppose you're running a JAMstack site fed by many data sources; when and how do you rebuild the site and refresh the data? 

## Trigger deploys with Netlify build hooks

To trigger deploys on Netlify, you can define [build hooks](https://docs.netlify.com/configure-builds/build-hooks/). These are HTTP endpoints that will start a new deploy whenever an HTTP request comes in.

![Netlify build hooks for stefanjudis.com on Netlify](//images.ctfassets.net/f20lfrunubsq/6tYzOoyGW7WOGiIVOF48cs/ea46afea4c68777eb0286a3b531d490f/Screenshot_2020-09-15_at_14.24.14.png)

I'm currently running with four different hooks starting Netlify builds. To make sure you can analyze what's starting your builds, I recommend defining each build hook depending on its functionality or trigger. 

You see that I defined a `REBUILD_CRON` hook. This URL endpoint is supposed to be called within a time interval – via a cronjob, so to say. 

If you wonder how do you set up a cronjob in the cloud, you're not alone. Many people asked for scheduled deploys and cronjobs on Netlify's platform. The recommendation so far has been [to use Zapier](https://zapier.com/apps/netlify/integrations/schedule/29330/start-deploys-of-netlify-sites-on-a-daily-schedule) or another third-party tool. 

__I never liked this approach because it adds another service dependency to my setup.__

But now that we have [GitHub actions](https://github.com/features/actions), and these turned out to be schedulable, I finally found the perfect solution to keep my website data fresh without bringing in another tool.

## A scheduled GitHub action to build and deploy your Netlify site

To dive into GitHub actions, I recommend reading [the documentation](https://docs.github.com/en/actions). If you're only looking for a snippet that works, here's the tl;dr:

- add a workflow file to your repository (`/.github/worfklows/main.yml`)
- paste the snippet below and update it to use your build hook URL
- add, commit and push the new file to GitHub

```yml
# /.github/worfklows/main.yml
name: Trigger Netlify Build at Midnight
on:
  schedule:
    # Every day at midnight
    # If you're also puzzled by 
    # the cron syntax check
    # -> https://crontab.guru
    - cron: '0 0 * * *'
jobs:
  build:
    name: Netlify Midnight Cron
    runs-on: ubuntu-latest
    steps:
      - name: Curl request
        run: curl -X POST -d {} https://api.netlify.com/build_hooks/xxx
```

`main.yml` defines a scheduled GitHub workflow that `curls` a Netlify build hook URL every midnight. 

That's beautiful! And setting up GitHub actions feels like a perfect solution to refresh my site data because it's just another way GitHub interacts with Netlify; I love that!