How to Deploy Hugo Site to DigitalOcean With Git Hooks
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.
Login to your remote server from the command line.
We’re going to install Hugo. The Easiest way is to use snapd:
sudo apt install snapd sudo snap install hugo
- Once Hugo installs, check Hugo version:
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
Create bare/empty Repo Inside Our Directory
git init --bare
Create a new post-receive hook
`` And lets copy and paste the following inside our
#!/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"
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
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
usernamewith your ssh user and
yoursite.comwith your server address.
git remote add production ssh://email@example.com/home/projectname/git/projectname.git
`` Note you can use whatever you want in place of
Now we can make some changes with,