Bash script checker to prevent drafts from being published accidentally

I accidentally removed draft = true line in the front matter which caused the unfinished post being built and deployed live. I couldn’t find a way to stop Hugo from publishing content that doesn’t have a draft parameter, as Hugo sees that as draft = false.

So I created a Bash script to check before running hugo and fails the build if there’s any content that does not have either draft = true (skip/default behaviour) or draft = false (pass/ok). It will triggers a fail + error message.

A relevant Discourse post I came across was this one but I couldn’t get the cascade to work (or it didn’t work for my setup) and the whitelisting method is not available by default.

The script can be found in my Codeberg Repo. I go into detail about how I use it in a blog post. Feedback welcome.

I hope this helps because I did not enjoy the mistake of publishing a draft and hope no one has to go through it either!

mmh,

but the cascade option works fine (as stated in the issue targets pages only

[[cascade]]
[cascade.params] 
draft = true
[cascade.target]   # _target deprecated in 156
kind = 'page'

of course only may need to adjust the targets depending on config and folders …

unless you have very special stuff, no need for external script

some details tl;tr
hugo new theme --themesdir drafts
cd drafts/content
# duplicate posts
cp -r posts blogs
  • Builds 23 pages
  • hugo list drafts returns none

all posts have set drafts to false, so: I removed three (shown just the drafts)

├───blogs
│   └───post-3
│           bryce-canyon.jpg
│           index.md                <-- draft
└───posts
    │   post-1.md                   <-- draft
    │   post-2.md                   <-- draft

added the cascade to sites config

  • build 20 Pages

  • hugo list drafts returns three

    content/blogs/post-3/index.md,,Post 3,2023-03-15T11:00:00-07:00,0001-01-01T00:00:00Z,2023-03-15T11:00:00-07:00,true,https://example.org/blogs/post-3/,page,blogs
    content/posts/post-2.md,,Post 2,2023-02-15T10:00:00-07:00,0001-01-01T00:00:00Z,2023-02-15T10:00:00-07:00,true,https://example.org/posts/post-2/,page,posts
    content/posts/post-1.md,,Post 1,2023-01-15T09:00:00-07:00,0001-01-01T00:00:00Z,2023-01-15T09:00:00-07:00,true,https://example.org/posts/post-1/,page,posts
    

a build guard could then be be wc -l respecing hugo settings like renderSegments, …

Thanks @irkode for explaining with your example on how to use the cascade. I tried using the snippet from issue in 2023 ( How to set all md files front matter draft false default ,in config file or other method? - #4 by jmooring ) before I created the script but I could not get it to work reliably (sometimes a .md will still be published when it didn’t have the draft parameter). I think I’ll start using the cascade snippet in my Hugo config and add a section to the blog post I wrote, and use the external script as a fallback for edge cases. Thank you so much!