When Hugo Meets OpenShift

Learn how to run Hugo on OpenShift DIY cartridge

ta-ching chen

3 minute read

 Table of Contents

Hexo is good, but generating speed 

I used Hexo, which is written in Node.js, to generate static web files and used OpenShift Node.js cartridge to host the files. It took almost 10 sec to generate for just 5 posts. On the contrary, Hugo, written in Go, generates all static files within 1 sec. That’s 10x fast than Hexo! In the following paragraph, we’ll go through the steps to create our own OpenShift DIY cartridge and run Hugo on it!

Prerequisite 

Create An OpenShift Cartridge 

  • OpenShift does not provide the cartridge for running binary, we have to create our own cartridge by OpenShift DIY .
  • Cartridge name MUST NOT contain any pattern that matches “hugo”, I’ll explain it later.
$ rhc app-create <cartridge name> diy

GIT CLONE 

  • Once the cartridge is created, it will automatically clone git repo from OpenShift.
$ git rm *
$ git commit -m "Remove DIY stuff"

Download Hugo Binary 

We need to download Hugo binary that runs on OpenShift.

$ wget https://github.com/spf13/hugo/releases/download/v0.16/hugo_0.16_linux-64bit.tgz
$ tar -zxvf hugo_0.16_linux-64bit.tgz hugo
$ rm -rf hugo_0.16_linux-64bit.tgz
$ mkdir bin/ && mv hugo bin/

Create Hugo Site 

hugo new site .
echo "/public" > .gitignore
git add *
git commit -m "Adding hugo site"

OpenShift Cartridge * Script 

  • .openshift/action_hooks/build
#!/bin/bash
rm -rf $OPENSHIFT_REPO_DIR/public
  • .openshift/action_hooks/start

Don’t forget to modify --baseURL="http://my-base-url/" to domain name you want

#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_DIY_IP:$OPENSHIFT_DIY_PORT
nohup $OPENSHIFT_REPO_DIR/bin/hugo \
    --bind=$OPENSHIFT_DIY_IP \
    --port=$OPENSHIFT_DIY_PORT \
    --source=$OPENSHIFT_REPO_DIR \
    --destination=$OPENSHIFT_REPO_DIR/public \
    --watch=false \
    --appendPort=false \
    --disableLiveReload=true \
    --baseURL="http://my-base-url/" \
    server |& /usr/bin/logshifter -tag diy &
  • .openshift/action_hooks/stop

We use ps -ef | grep hugo to get pid of hugo and kill it and then start the deployment process.

#!/bin/bash
source $OPENSHIFT_CARTRIDGE_SDK_BASH

# The logic to stop your application should be put in this script.
if [ -z "$(ps -ef | grep hugo | grep -v grep)" ]
then
    client_result "Application is already stopped"
else
    kill `ps -ef | grep hugo | grep -v grep | awk '{ print $2 }'` > /dev/null 2>&1
fi

When we deploy new web files, OpenShift dispatches the stop script to stop cartridge first.

# OpenShift log when we command git push
$ git push
...
Writing objects: 100% (5/5), 455 bytes | 0 bytes/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Stopping DIY cartridge
remote: Building git ref 'master', commit b17eca3
remote: Preparing build for deployment
remote: Deployment id is e3f3adf0
remote: Activating deployment
remote: Starting DIY cartridge
...

If cartridge naming with pattern of hugo, the process that is redeploying the application will also be killed. The only way to solve it is to create a new cartridge again.

  • Apply 755 permission
$ chmod 755 .openshift/action_hooks/*
$ git add .openshift/action_hooks/*
$ git commit -m "updating action_hooks"

Deploy Hugo to OpenShift! 

  • Make sure that every step is doing right before you deploy it
$ git push

Thanks 

Thanks to the post on sub-pop, that’s really help a lot!

Reference 

See Also

To reproduce, republish or re-use the content, please attach with link: https://tachingchen.com/
comments powered by Disqus