Skip to main content
Coderweekend

Trigger a Netlify build every day using GitHub Actions

Normally, Netlify will trigger a build every time you make a new Git commit.

But you can also trigger the build periodically with the help of GitHub Actions.

By using GitHub Actions to schedule a build, you can update your content without making a new commit or manually triggering a Netlify build.

Benefits of setting up nightly build #

Setting up a build to run periodically can benefit you in many ways.

  • You can bulk upload your blog posts with the published date set in the future and let the system release it daily.
  • You can fetch a new update from API every day, e.g., Refresh top trending articles every day.

How to Trigger a Netlify build every day using GitHub Actions #

We need to do two things to schedule a nightly build using GitHub Actions.

  1. Getting build hook URL from Netlify.
  2. Trigger that build via that URL every day using GitHub Actions.

Getting Netlify Build hook URL #

A build hook is a URL that Netlify provides to lets you trigger a new build and deploy by making a POST request to that URL.

To get this URL:

  1. Go to the "Sites" page and select the site that you want.


    Sites.
    Sites.

  2. Select "Site Settings".


    Site Settings.
    Site Settings.

  3. Select "Build & deploy" and scroll down to "Build hooks".


    Build hooks.
    Build hooks.

  4. Click "Add build hook" and fill in all the information.


    New Build Hook.
    New Build Hook.

  5. Click "Save", and you will see the new build hook URL. That's the build hook URL that we will use to trigger the build.


    New Build Hook URL.
    New Build Hook URL.

You can test out this URL by making a POST request yourself. For example, you can run a curl command from your terminal:

curl -X POST -d '{}' https://api.netlify.com/build_hooks/YOUR_BUILD_HOOK_KEY

Create GitHub Actions workflow #

To create a new GitHub Actions workflow, you need to do the following steps:

  1. Create a .github/workflows directory in your project root directory.
  2. In the .github/workflows directory, create a yml file. You can name this file anything you want. I named it nightly-build.yml to indicate that it will run every night.
  3. Copy the following YAML contents into the nightly-build.yml file:
name: Nightly build
on:
schedule:
- cron: '0 0 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Trigger Netlify Build hook
run: curl -X POST -d '{}' https://api.netlify.com/build_hooks/YOUR_BUILD_HOOK_KEY

Save the file and push the changes to GitHub.

That's all you need to do to trigger the nightly build on Netlify.

Result #

When the specified time is met, you will see an action run in the GitHub Actions, which triggers the deployment in Netlify.


GitHub Action workflow run.
GitHub Action workflow run.

You can see the detail in Netlify that the deployment is initiated by a hook from the GitHub Actions.


Deploy triggered by hook: Github Actions.
Deploy triggered by hook: Github Actions.

Improvement #

In the above example, I put the build hook URL directly into the workflow file. I do that for the sake of brevity.

In reality, you better store sensitive information in an environment secrets.

So, your workflow should look something like this:

name: Nightly build
on:
schedule:
- cron: '0 0 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Trigger Netlify Build hook
run: curl -X POST -d '{}' https://api.netlify.com/build_hooks/${TOKEN}
env:
TOKEN: $