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
# build.sh
echo "* hugo build"
hugo -b https://migor.org/blog
cp scripts/server/update-deployment.sh public/update-deployment.sh

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

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

echo "* Waiting for deployment"
waitFor=70

# credits http://serverfault.com/questions/532559/ddg#532564
while [ ${waitFor} -gt 0 ]; do
   echo -ne "${waitFor}\033[0K\r"
   sleep 1
   : $((waitFor--))
done

echo "* Validating"
# -- Validating deployment
siteContent=`curl -v --silent https://migor.org/blog/posts/2018-techology-radar/ 2>&1 | grep 'Technology'`
if [[ -z ${siteContent} ]]; then
    echo "Deployment failed"
    exit 1
else
    echo "Done :)"
fi

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 update-blog.sh 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.

# update-blog.sh

git pull
# cron

* * * * * www cd /folder/of/checkout && ./update-deployment.sh

Summary

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

cd

361 Words

2019-01-28 16:03 +0000