Blog Continuous Deployment

I use hugo to generate a static HTML/JavaScript representation from markdown sources. Deploying has always been a burden. I had posts sitting around, cause I just had no time to deploy. So I automated it.

In my current setup I have two scripts, one that triggers the hugo build into folder called /public, which is a git submodule that points to the migor-blog-production repository on gitlab. A second script updates the blog to the latest revision of the production repository..

Build Script

The build script accomplishes three things

  • hugo build
  • push to the production repository
  • build and deployment validation
echo "* hugo build"
hugo -b
cp scripts/server/ public/

if [ ! -f public/posts/2018-techology-radar/index.html ]; then
    echo "File not found!"
    exit 1

# -- Pushing submodule
cd public
if [[ -z `git remote -v | grep production` ]]; then
    echo "Submodule not installed"
    exit 1
git checkout master
git add .
git commit -m "new blog posts"
git push

echo "* Waiting for deployment"

# credits
while [ ${waitFor} -gt 0 ]; do
   echo -ne "${waitFor}\033[0K\r"
   sleep 1
   : $((waitFor--))

echo "* Validating"
# -- Validating deployment
siteContent=`curl -v --silent 2>&1 | grep 'Technology'`
if [[ -z ${siteContent} ]]; then
    echo "Deployment failed"
    exit 1
    echo "Done :)"

Continuous Deployment Script

On my server I have a clone of the production repository, which I serve directly. A cron job that runs every minute calls the script that will get the latest master. I prefer the abstraction of git pull in a script, its a bit premature I know, although I could simply call pull directly. Now I have all pieces managed by git, except the cron job.


git pull
# cron

* * * * * www cd /folder/of/checkout && ./


This automation simplifies my live enormously. For a later version I could do a couple of things that are not so nice at the moment.

  • Greedy 1 minute polling
  • Replace the dummy commit message in the submodule by the last one of the source repo
  • The deployment validation could check if the latest non-draft post is available


361 Words

2019-01-28 16:03 +0000