DEV Community

Eric Jinks
Eric Jinks

Posted on • Updated on • Originally published at ericjinks.com

Schedule your Netlify build with GitHub Actions

If you host a site on Netlify, you may know about Build Hooks. By creating a build hook and sending a POST request to it, you trigger a new build & deploy for your site. If your build process is pulling in content from third-party services (e.g. Instagram, Youtube), scheduling a daily build & deploy can be an easy way to keep this content fresh.

The easiest way to setup a scheduled build hook trigger that I have come across is to use GitHub Actions. Setting this up is completely free if your repository if public. If your repository if private, you will most likely remain in their free tier, see pricing here.

Creating a scheduled Netlify build trigger GitHub Action

1 – Add a Build hook to your site using the Netlify Dashboard

(Settings > Build & Deploy > Continuous Deployment > Build hooks)

2 – In your GitHub repo, create a main.yml file in a .github/workflows directory:

   # .github/workflows/main.yml

   name: Trigger Netlify Build
   on:
     schedule:
       # Run at 0815 daily
       - cron: '15 8 * * *'
   jobs:
     build:
       name: Request Netlify Webhook
       runs-on: ubuntu-latest
       steps:
         - name: Curl request
           run: curl -X POST -d {} YOUR_BUILD_HOOK
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_BUILD_HOOK with the build hook url you just created.

You can use crontab.guru to easily generate your cron schedule.

3 – Open the Actions tab in you GitHub repo to watch the workflow trigger as per your cron schedule. 🎉

I hope this is useful to you! If you have any questions, I've created an example repository that uses GitHub Actions to build every 15mins:

GitHub logo Jinksi / netlify-build-github-actions

An example of triggering a Netlify build using Github Actions Scheduled Events

⏰ Trigger Netlify builds on a schedule using Github Actions

  1. Add a Build hook to your site using the Netlify Dashboard

    Settings > Build & Deploy > Continuous Deployment > Build hooks

  2. Create a .github/workflows/main.yml file in your Github repo, replacing YOUR_BUILD_HOOK with the build hook you just created.

    name: Trigger Netlify Build
    on:
      schedule:
        - cron: '*/15 * * * *' # every 15 mins
    jobs:
      build:
        name: Request Netlify Webhook
        runs-on: ubuntu-latest
        steps:
          - name: Curl request
            run: curl -X POST -d {} YOUR_BUILD_HOOK
    Enter fullscreen mode Exit fullscreen mode
  3. Adjust the cron settings to determine how often the build will be triggered.
    15 8 * * * would run every day at 0815
    0 0,12 * * * would run at midday and midnight every day
    crontab guru can help you generate the correct cron syntax.
    See the Github Actions

Top comments (3)

Collapse
 
programazing profile image
Christopher C. Johnson

I just want to add that you should take your UTC offset into account.

For example:

If you want your build to run every morning at 8:15 A.M. and you're in the Eastern Time Zone (UTC -5) then you wouldn't use '15 8 * * *',
you'd use '15 13 * * *' because you're five hours behind.

Collapse
 
dotnetcoreblog profile image
Jamie

This is amazing, thank you.

I've replaced an IFTTT trigger with this for some of my websites, and I'm super happy with it: it's all now committed to source control.

I feel like I need to point out that step 2 says:

2 – In your GitHub repo, create a main.yml file in a .github/workflow directory

Whereas it should read .github/workflows. The pluralisation of workflow is important.

Other than that, amazing!

Collapse
 
jinksi profile image
Eric Jinks

Massive thanks Jamie! 🙌

I've updated the post to fix the typo.

I was using the same method, using IFTTT. It's much nicer to use a method that is contained within the code.