Skip to content

Instantly share code, notes, and snippets.

@victoriadrake
Created May 22, 2018 15:42
Show Gist options
  • Save victoriadrake/7d45a8f2ac5cc53743ad34eec8950182 to your computer and use it in GitHub Desktop.
Save victoriadrake/7d45a8f2ac5cc53743ad34eec8950182 to your computer and use it in GitHub Desktop.
A makefile that regenerates the public/ folder for Hugo sites, optimizes images and prepares the git commit for GitHub Pages.
SHELL:=/bin/bash
BASEDIR=$(CURDIR)
OUTPUTDIR=$(BASEDIR)/public
optipng = find $(OUTPUTDIR) -name '*.png' -print0 | xargs -0 -P8 -n2 optipng -o5 -strip all -quiet
optijpg = find $(OUTPUTDIR) -name '*.jpg' -print0 | xargs -0 -P8 -n2 jpegoptim --strip-all -m30 -q
site:
@echo "Deleting files from old publication"
rm -rf $(OUTPUTDIR)/*
@echo "Generating site"
hugo
@echo "Optimizing images"
$(call optijpg)
$(call optipng)
@echo "Preparing commit"
cd $(OUTPUTDIR)/ && git add . && git status && git commit -m "$(m)"
@victoriadrake
Copy link
Author

victoriadrake commented May 22, 2018

Prerequisites:

What this Makefile does:

  1. Removes all files and folders from public/, leaving hidden files (like .git) intact
  2. Generates your Hugo site with the hugo command
  3. Optimizes png and jpg images according to the flags passed (see manpages for optipng and jpegoptim to understand these)
  4. In public/, creates a new commit with your message

Usage

Do make site m="<your message>"

This Makefile does not do git push for you. If everything looks ok after the Makefile runs, do this manually.

@victoriadrake
Copy link
Author

If you use a custom domain with GitHub Pages, ensure that your Hugo site contains the CNAME file inside root/static/. You can also put your README.md there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment