Fixing My Embargo: Setting up a Cron Job

stephencweiss profile image Stephen Charles Weiss Originally published at stephencharlesweiss.com on ・8 min read

I wrote previously about setting up a simple embargo on my Gatsby site in the past. Even when I first implemented it, however, I knew that there’d be a better solution in the future.

As a reminder: Gatsby sites are static. In my case, the builds included all of the posts for my blog. Consequently, when I wrote a post and added it to my site - when the site built it was visible.

The purpose of the embargo was to create space between when I wrote a new post and when it would appear. The original solution solved this by simply hiding the posts on the front end. I wanted a better solution however. For that, I reached for an old standby: the cron job.

What Is A Cron Job

Cron is a utility that is used for scheduling commands or programs to execute at a specific time. Named for Chronos, the diety of time, these scheduled commands are referred to as “Cron Jobs”.

Common uses of cron jobs include backups, monitoring, and maintenance.

The syntax for cron jobs is a little peculiar, but there’s a logic to it that just takes some familiarity:

 ┏━━━━━━━━━━━━ minute (0 - 59)
 ┃ ┏━━━━━━━━━━ hour (0 - 23)
 ┃ ┃ ┏━━━━━━━━ day of month (1 - 31)
 ┃ ┃ ┃ ┏━━━━━━ month (1-12)
 ┃ ┃ ┃ ┃ ┏━━━━ day of week (0 - 6) or use names;
 ┃ ┃ ┃ ┃ ┃ 0 and 7 are Sunday, 1 is Monday,
 ┃ ┃ ┃ ┃ ┃ 2 is Tuesday, etc.
 ┃ ┃ ┃ ┃ ┃
 * * * * * <command to execute>

Some great examples of different schedules can be found on ostechnix.com.

Netlify Build Hooks

Now that we know a bit about cron jobs, we want to talk about which command to execute.

In this particular case, we’ll be using Netlify Build Hooks as I’m using Netlify to build and deploy my site.

Triggering a build requires only a POST request sent to the URL specified by a token.

To add a build hook, we log into Netlify, select the site and choose Settings.

Then, on the left, select “Build & Deploy” and scroll down to “Build Hooks”. Select “Add build hook”. From here, we can name the hook.

Once saved, Netlify will produce a token we can use to trigger a build. The token should be considered sensitive and should be kept private (similar to an API key).

Now that we have a build hook, we need to schedule the invocation of it. For that, we’ll use a cron job via GitHub Actions.

GitHub Actions

Recently out of a beta, GitHub Actions are a new feature from Github to automate software workflows.

To get the action set up, we will need to:

  1. Add a workflow .yml
  2. Add the secret

Adding GitHub Workflows

Github actions are managed by the presence of a .yml file in a directory in your project .yml.

Here’s the my nightly-build.yml file:

name: Daily Build
    - cron: ‘0 8 * * *’ # 0 minute, 8th hour, every day of the month, every month, every day of the week (UTC)*
    runs-on: ubuntu-latest
      - name: Trigger Netlify Build Hook
        run: curl -s -X POST -d {} “https://api.netlify.com/build_hooks/${TOKEN}”
          TOKEN: ${{ secrets.NETLIFY_DAILY_CRON_HOOK }}

Remember that a cron job has two parts: a schedule and a command. In this yaml file, I’ve defined a schedule as the 0th minute of every 8th hour, every day of the month, every month, and every day of the week.1 The job is to run the curl and send the POST to Netlify.

Two things to note:

  1. Regarding yaml files generally: they’re space sensitive. My first attempt resulted in an error, “YAML mapping values are not allowed in this context”. Investigating it led me to this StackOverflow conversation and ultimately http://www.yamllint.com/ - a great tool for validating yaml files.
  2. The curl is the same as what Netlify suggested - though I spefcified a TOKEN which takes a value secrets.NETLIFY_DAILY_CRON_HOOK. That secret hasn’t yet been defined. In fact, that’s the final step.

Adding A Secret

Though we’ve referenced a variable in our .yml file, we now need to define it.

To do so, in the repository, select settings and then “Secrets” on the left.

github secrets

In this case, the secret is taken directly from Netlify as the string that follows /build_hooks/.


And with that, I have a new build process. Suddenly, my website builds every day at 8am which frees me to focus on the parts of writing and running this site that I enjoy and begin to automate the other stuff.


  • 1 Though it’s scheduled at 8am, it’s important to remember that the time is local to the computer running the command. In this case, that means wherever the Netlify servers are.


Additional resources that I found helpful include:


Editor guide