github actions & netlify build hooks

2020-09-10

 | 

~3 min read

 | 

408 words

I previously wrote about using Netlify Build Hooks in the context of fixing my daily embargo problem for this site. I found myself referencing that document for many other purposes whenever I wanted to remember how I could use Netlify’s build hooks with Github Actions.

To simplify things, I wanted to pull out the relevant details:

  1. Create A Build Hook on Netlify
  2. Add Secrets To Github
  3. Using Secrets In Github Action Workflows

Netlify Build Hooks

To create a Build Hook, navigate to the desired site Within the Netlify UI. Then use the menus until you reach Build Hooks. The path is: Settings > Build & deploy > Continuous Deployment > Build hooks.

Name the hook whatever you’d like (in this example, I’m using the hook as part of a daily cron job to build the site, so I’m naming it daily-cron):

Build Hook Configure

On saving, you will see the result of the hook:

Build Hook Result

It should begin https://api.entlify.com/build_hooks/.

Copy the API endpoint as we will use it as a secret referenced within our Github workflow.

Adding Secrets To Github

To add a secret within Github’s UI, navigate to the repository that is linked to the Netlify site and with which we want to trigger a build. Then use the menus until you reach Secrets. The path is: Settings > Secrets.

Github Secrets

Github Actions Workflows

Github recently released Actions. These are workflows that can be run based on a number of triggers - e.g., a new merge can trigger a CI/CD pipeline. In our case, we’ll model a daily build process based on a schedule:

.github/workflows/daily-build.yml
name: Daily Build
on:
    schedule:
        # 0 minute, 8th hour, every day of the month, every month, every day of the week (UTC)
        - cron: "0 8 * * *"
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Trigger Netlify Build Hook
              run: curl -s -X POST -d {} "https://api.netlify.com/build_hooks/${TOKEN}"
              env:
                  TOKEN: ${{ secrets.NETLIFY_DAILY_CRON_HOOK }}

Workflows live in a hidden file in the repository, .github. (This is the same directory where pull request and issue templates are often stored.)

Learning the appropriate syntax for cron jobs can be challenging. I find crontab.guru an excellent tool for helping customize schedules.

Conclusion

With that, our repository is configured. Every morning at 8am UTC the action will run. Assuming we put in the correct secret, we should see a build kicked off as a result.


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!