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.
- Getting build hook URL from Netlify.
- 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:
-
Go to the "Sites" page and select the site that you want.
-
Select "Site Settings".
-
Select "Build & deploy" and scroll down to "Build hooks".
-
Click "Add build hook" and fill in all the information.
-
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.
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:
- Create a
.github/workflows
directory in your project root directory. - In the
.github/workflows
directory, create ayml
file. You can name this file anything you want. I named itnightly-build.yml
to indicate that it will run every night. - 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.
You can see the detail in Netlify that the deployment is initiated by a hook from the 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: $