Scheduling Netlify Builds with GitHub Actions

Christian Emmer
Christian Emmer
Apr 24, 2020 · 3 min read

Netlify is a popular service to host JAMstack websites with, and updating those websites automatically on a schedule is easy with GitHub Actions .

See "What is the 'JAMstack'?" for a complete explanation, but JAMstack websites are completely static, pre-rendered websites hosted with a Content Delivery Network (CDN) .

What is the "JAMstack"?
What is the "JAMstack"?
Apr 26, 2020 · 5 min read

Simply, it stands for: JavaScript, APIs, and Markup.

Netlify

Netlify is a great option for hosting small, static sites for free. As of writing they offer unlimited sites with 100 GB bandwidth and 300 build minutes per month, completely for free, all on subdomains you can choose (or with your own domain), complete with an SSL certificate you don't have to manage. This very website is hosted on Netlify for free right now.

Netlify has a feature called Build Hooks that lets you trigger builds (and deploys) by sending an empty POST request to private URLs they generate per site. This is useful for automating a build when some kind of action happens - an email is received, a message is sent in Slack, or from an external scheduling service. That last one being the focus of this article.

GitHub Actions

GitHub Actions left beta relatively recently on Mar 24, 2020 and is a promising alternative to other CI/CD services such as Jenkins and CircleCI . It's not as mature as other options out there, but it's likely to get there with the power of Microsoft behind them .

Setup

  1. Generate a new build hook URL for your site in the Netlify Dashboard .

    Go to Settings > Build & deploy > Build hooks > Add build hook and give it a unique name such as "GitHub Actions."

  2. Go to your repository's secrets page ) and add a new secret with the name NetlifyBuildHook and the value of the build hook URL you just generated.

    We're using Actions secrets because the URL is sensitive enough we don't want to commit it directly to our repository.

  3. In your repository, make a new YAML file at .github/workflows/cron.yml. The exact filename can be changed, just as long as it's in that directory.

    Fill in the file with the contents:

    name: Build Cron Schedule
    
    on:
      schedule:
        # At noon GMT every day
        - cron: '0 12 * * *'
    
    jobs:
      build:
        name: Netlify Build Hook
        runs-on: ubuntu-latest
        steps:
          - name: cURL Request
            env:
              NETLIFY_BUILD_HOOK: ${{ secrets.NetlifyBuildHook }}
            run: curl -X POST -d {} "${NETLIFY_BUILD_HOOK}"

    Note that the cron timing is in GMT/UTC, not your local timezone.

    I find cron schedules difficult to understand, so I recommend websites such as crontab.guru to help translate to plain English.

  4. Wait for the next time your site is supposed to build and watch!

    I've found the timing is not always exact, sometimes the build will start minutes after it's supposed to, but that shouldn't be a big issue.