TypeOfNaN

Scheduling Recurring Netlify Builds Using Github Actions

Nick Scialli
May 11, 2021

If you find yourself needing to rebuild a Netlify app at a regular interval, Github actions may be right for you. This presumes that you have integrated Github and Netlify and your site is already being built from Github.

The general steps to making this work are:

  • Create a Github Action in our repository
  • Use the schedule field in our Action yaml file to run actions on a cron-like schedule
  • In the action, send a POST request to a build hook URL that we get from Netlify

Let’s walk through how to set this all up.

Creating the Action

In our Github repository, any action will go in the .github/workflows directory. Let’s put the code in a main.yml file in that directory:

/.github/workflows/main.yml

name: Schedule Netlify Build
on:
  workflow_dispatch:
  schedule:
    - cron: '0 14 * * *' # Once a day around 10am ET
jobs:
  build:
    name: Request Netlify Webhook
    runs-on: ubuntu-latest
    steps:
      - name: POST to Build Hook
        env:
          BUILD_KEY: ${{ secrets.NETLIFY_BUILD_KEY }}
        run: curl -X POST -d {} https://api.netlify.com/build_hooks/$env:BUILD_KEY

We have named our action “Schedule Netlify Build” and we told it to trigger on two conditions: “workflow_dispatch”, which will allow us to trigger the action from the Github UI manually and, more importantly, “schedule”, which will tell Github to run the action on a cron-like schedule.

In this example, we’re running our cron job every day at the 14th hour UTC.

Our cron job will perform a curl POST request to https://api.netlify.com/build_hooks/$env:BUILD_KEY. Note that we have $env:BUILD_KEY in this URL—we don’t want to put the build key directly in our code since it’s sensitive. Instead, we’ll store it in Github Secrets as NETLIFY_BUILD_KEY.

This is all wired up now! We can push it up to our Github repository. If we tested it out immediately it would fail because we still have to obtain the build key from Netlify and save it to Github Secrets.

Getting a Netlify Build Key

In Netlify, you can follow the following general navigation path to get to the build hooks area:

  1. Click on your site
  2. Click Site Settings
  3. Click Build and Deploy
  4. There should be a Build hooks section on this page

In the build hooks section, you should be able to click Add build hook. You will have the option to name your hook (anything will do, perhaps go with “Scheduled github action build”) and you can select which branch to build.

creating a new build hook

Once you click Save, you should see a build hook link that looks like this:

https://api.netlify.com/build_hooks/some-random-string

You’ll want to copy the random string at the end of this URL—that’s what will go in Github Secrets.

Adding the Build Key to Github Secrets

In Github, navigate to Settings and then Secrets. There, you should see a button for New repository secret.

When adding your secret, be sure to name it NETLIFY_BUILD_KEY and put only the random string part of your key in the box:

adding a github secret

Testing Our Action

One way to test our action would be to wait until 10am tomorrow, but that seems like a long wait. Instead, we made sure to give ourselves an option to manually trigger our Github Action, so let’s give that a shot.

Click on Actions, click on your action in the menu on the left, click the Run workflow dropdown, and then click Run workflow:

triggering the action manually

You should be able to watch your build complete successfully from here. I also recommend confirming on the Netlify side that your site was rebuilt.

Nick Scialli

Nick Scialli is a senior UI engineer at Microsoft.

© 2024 Nick Scialli