How to Deploy Hugo Site to DigitalOcean With Git Hooks

Posted on 2 mins read

Deploying a Hugo Site to DigitalOcean with Git Hooks

The following steps will show you how to deploy a Hugo site from your local server (e.g. Development, localhost, etc) to DigitalOcean with a Git Hook.

  1. Login to your remote server from the command line.

    ssh username@your-ip-address
    

    ``

  2. We’re going to install Hugo. The Easiest way is to use snapd:

    sudo apt install snapd
    sudo snap install hugo
    

    ``

  1. Once Hugo installs, check Hugo version:
    hugo version
    
    ``
  1. Make new directory for our git repo. The following will be done on your remote server (Production).

    mkdir -p /home/git/projectname.git
    cd /home/git/projectname.git
    

    ``

  2. Create bare/empty Repo Inside Our Directory

    git init --bare
    

    ``

  3. Create a new post-receive hook

    nano hooks/post-receive
    

    `` And lets copy and paste the following inside our post-recieve

    #!/bin/bash
    
    export GIT_WORK_TREE=/home/projectname/git/web-compiled
    
    echo `pwd`
    echo "Generating site with Hugo for hugo-public"
    
    # Makes a directory for us to leave the latest HTML files generated by Hugo in /home/projectname/git/web-compiled
    mkdir -p $GIT_WORK_TREE
    chmod 755 $GIT_WORK_TREE
    
    git checkout -f master
    
    # Remove public folder created by Hugo
    rm -rf $GIT_WORK_TREE/public
    cd $GIT_WORK_TREE && /snap/bin/hugo --disableKinds=taxonomy,taxonomyTerm
    
    find $GIT_WORK_TREE/public -type f -print | xargs -d '\n' chmod 644
    find $GIT_WORK_TREE/public -type d -print | xargs -d '\n' chmod 755
    # Copy built site to /var/www/public
    cp -r $GIT_WORK_TREE/public /var/www/public
    
    echo "All done! 0 problems"
    

    ``

  4. Make Our post-receive Hook Executable

    chmod +x hooks/post-receive
    

    ``

    The code form above makes a directory for us to leave the latest HTML files generated by Hugo in /home/projectname/git/web-compiled

  5. The following applies to your local server (e.g. Development), we are adding our remote source that references our production server and repo that we setup previously. Replace username with your ssh user and yoursite.com with your server address.

    git remote add production ssh://username@yoursite.com/home/projectname/git/projectname.git
    

    `` Note you can use whatever you want in place of production (staging, etc).

  6. Now we can make some changes with, git commit and git push.