Deploy to Netlify
Build your Netlify site on schedule using a request to a build hook
Topics
Pages
Content
This guide was inspired by this post.
This leverages following areas:
- Scheduling of a GH Action
- GH secret environment variables
- Netlify build hooks - see docs.
The problem case
A Netlify site will rebuild whenever you push commit to your main branches.
But what if your build process builds in external content such as from the GitHub API. Then you might want to build your site daily or weekly using an automated task, without you having to push a commit.
Netlify doesn’t support scheduled builds, but you can use GH Actions to trigger a Netlify build.
We are going to use GH Actions on a daily schedule to trigger that build.
How to setup
- In Netlify, go to site preferences and find an existing (secret) build hook URL for your site.
- A sample URL is like:
https://api.netlify.com/build_hooks/abcdef12345abcdef12345
- When you do a POST request that URL without any data in the payload, your site will build.
- If you want, you can test the snippet by running it locally and then checking your deploys in Netlify.
- A sample URL is like:
- Copy the ID portion of the snippet you get.
- e.g.
abcdef12345abcdef12345
- Note you will want to keep this ID and hook URL private. That will prevent strangers from triggering unnecessary rebuilds of your site. This privacy can be achieved by setting your URL as a secret environment variable which can be accessed within the workflow run. See the next step.
- e.g.
- Go to GitHub repo’s Settings.
- Then to Secrets.
- Click New repository secret.
- Create a secret with name
NETLIFY_HOOK_ID
and your copied ID as the value. - Create a new Actions workflow.
- Go to Actions tab of your repo and create a new workflow - at the top click set up an workflow yourself.
- Or create a file file with path
.github/workflows/netlify.yml
.
- Copy and paste the content from the workflow file below.
- Now wait for your workflow to run at the specified time.
- Find it logged on the Actions tab.
- Find it logged in your Netlify deploys - as “Deploy triggered by build hook: hook1”.
Workflow file
netlify.yml
name: Scheduled Netlify deploy on: schedule: - cron: "0 0 * * *" jobs: deploy: name: Deploy runs-on: ubuntu-latest steps: - name: Trigger hook run: curl -d '' https://api.netlify.com/build_hooks/${{ secrets.NETLIFY_HOOK_ID }}
Notes
- Here we run daily at midnight. The time is in UTC time, which is within 0.9 seconds of GMT +0 hours - read more.
- If hover of the cron time when editing with Actions tab, GitHub will show you the frequency on hover.
- Customize the schedule expression with the help crontab.guru.
- We turn the curl request into a POST by supplying an empty data payload with
-d
. - You secret will be kept private in your logs. The above will be logged as
curl -d '' https://api.netlify.com/build_hooks/***
.
Manually trigger
You can trigger your build manually at any time using the following approaches:
Workflow dispatch in GH Actions
Expand the workflow above to allow manual builds through GitHub. For example, if you want to check that your workflow and secret are set up correctly.
on:
schedule:
- cron: "0 0 * * *"
Then under the Actions tab for a workflow, you will see “This workflow has a workflow_dispatch event trigger.” and the “Run workflow” button.
Trigger in GH Actions
Under “Deploys”, click “Trigger deploy”.