Running scheduled build for Gatsby site on Netlify

April 26, 2020

For a static Gatsby site deployed to Netlify, by default the content only gets updated when we push code to master branch. But what if we have “semi-dynamic” data that needs to be updated periodically?

In my previous post, I added the gatsby-source-github-api plugin to fetch my 4 pined repoes from Github. If I change the pined item from Github, it won’t get updated on my website.

So a cron job that runs on schedule to fetch the update can be helpful here.

Netlify has the build hooks feature where a build can be triggered by sending a POST request to a given URL.

$ curl -X POST -d '{}'

Using Github Actions

We can use Github Actions’s scheduled events to run the curl command from above with a cron expression.

Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you’d like, including CI/CD, and combine actions in a completely customized workflow.

Create a .github/workfolws/main.yml in the repo.

name: Trigger Netlify Build
    # Run at 0815 daily
    - cron: '15 8 * * *'
    name: Request Netlify Webhook
    runs-on: ubuntu-latest
      - name: Curl request
        shell: bash
          NETLIFY_DEPLOY_HOOK: ${{ secrets.NetlifyDeployHook }}
        run: curl -X POST -d {} "$NETLIFY_DEPLOY_HOOK"

Note that we are reading the Netlify deploy hook url from secrets to avoid expose it to the public. The doc for how to manage secrets from Github can be found here.

One thing I noticed that is when adding a secret, the secret name can not have _ in it otherwise it will not work.