Date: 2020.02.10 | cicd | gitlab | hugo | netlify |
DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)
My sites are built with Hugo and run on Netlify. I write a lot of posts (in 2019, I wrote 66) but I like to spread them out so that they don't all drop at once. To do this I use the date field on my content to schedule publish at a future date. However, Hugo only re-publishes on rebuild so in order for that scheduled content to go out, I need my site to rebuild on a recurring schedule otherwise my content won't be published until the next time I push to my repo which could be days.
Because I host my repos on GitLab, how can I use the built-in GitLab CI to schedule rebuilds on Netlify?
GitLab CI is full featured, so making this happen won't be a problem - I used to use GitLab CI to build and publish my containers when I ran my sites on Kubernetes. Netlify also makes publishing super easy with their publish web hooks, we just need to make sure we keep that hook secret so that bad actors don't DOS (or perhaps worse rack up a large bill) for our accounts.
I'll paste the code I use to push my code to Netlify from GitLab CI below and walk through what's going on and additional steps you'll need to take to get it working for you.
This is my project's .gitlab-ci.yml - it must live in the root of your repo for GitLab to use it and basically it tells GitLab CI what you want it to do.
image: docker:stable
services:
- docker:dind
stages:
- deploy
deploy_site:
stage: deploy
before_script:
- apk add --update curl && rm -rf /var/cache/apk/*
script:
- curl -X POST -d '{}' $NETLIFY_BUILD_WEBHOOK
only:
- schedules
From top to bottom, this says:
docker:stable image to run this GitLab CI pipelinedocker:dind service - this gives us access to curl later ondeploy - for more advanced pipelines, there could be multiple stages with multiple steps in each, but this one is v simple so only needs one stage with one stepdeploy_site step
deploystagecurlcurl to HTTP post an empty blob to our Netlify web hook which will tell Netlify to rebuild our site - note that we use a GitLab CI variable here so that we can keep that secret out of our code and away from the prying eyes of bad actorsschedules - we do this because Netlify has built-in push webhooks, so running this on things other than schedules would result in duplicate builds which is suboptimalYou can plop this in your repo and we're almost ready to go. The last things we need to do are set your $NETLIFY_BUILD_WEBHOOK so our pipeline knows what url to post to and create a cron job (a job that runs on a schedule) to run our pipeline as often as we want.
$NETLIFY_BUILD_WEBHOOKTo set your $NETLIFY_BUILD_WEBHOOK variable:
First find your webhook on Netlify.
Deploys > Deploy SettingsBuild Hooks sectionNow we need to set your variable in your GitLab repo
Settings > CI/CDvariables section and click expandvariableNETLIFY_BUILD_WEBHOOKprotected - we want to protect this because bad actors could use it to do mean thingsSave variable at the bottomOkay now we have our pipeline all set up but we need something to actually call it. We'll run our pipeline on a schedule using GitLab CI's built-in cron scheduler. To do so:
CI / CD > SchedulesNew ScheduleScheduled deploy)Interval Pattern select a default pattern or create your own using Cron Syntax - there's a link there for cron syntax so I wont' explain it here
That's it, you now have scheduled builds on Netlify using GitLab CI!
The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.