Delete public folder before every build?

I’ve set up a Hugo web site and am hosting it on a Github Pages personal site. I’m using a single repo with two branches: source and master. The source branch is all folders except public. Master branch is only the public branch. So far so good.

My question is, after making changes to my site, do I need to delete public before running ‘hugo’? I did that once and it completely messed up my git folder, in turn messing up my hosting. For the last few changes I have NOT deleted the public folder and it seems to work fine. But I’m wondering if I am going to run into a ‘gotcha’ at some point by doing this. If I need to delete my public folder, how can I do it without messing up git?

Put /public into .gitignore.

1 Like

But I have to have a git repository in master or the files will not get to GitHub.

@caseydm Are you using a Mac? If so, I can probably help you write a really simple bash script to do this from the command line with a really short alias (eg, hugb). If you can point me to your GH repo so that I can see the folder structure and let me know where the local copy is located on your machine, that would help.

Hi rdwatters. That would be very helpful! Yes I am using a Mac. My github repo is https://github.com/caseydm/caseydm.github.io. The files are on my local machine at Users/casey/blog, with the generated HTML files at Users/casey/blog/public.

Hi guys. I wrote a deployment script that is working well. However, the only piece I am missing is that I do not delete the public folder. So if I remove a post and run the script, that post will still be there. Any tips on how to resolve this? My script is below:

#!/bin/bash

echo 'deploying'

# commit source and push to github
git add -A
git commit -m 'update'
git push origin source

# build public folder
hugo

# change to public directory
cd public

# commit master and push to github
git add -A
git commit -m 'update'
git push origin master

echo 'deployment complete'

Add a rm -rf public and you should be home fee.

@bep the problem is running rm -rf will delete the .git folder that contains the master branch of the repository, and also delete the cname file that is needed for github pages.

@caseydm A few things, and I hope I’m not overstepping my bounds, but your new site (looks great, btw, keep at it!) says you are just getting into web dev. Me too, so maybe I can save you some headaches, but keep in mind this is just my approach to the workflow.

  1. Move your CNAME and robots.txt to your static folder. This will make sure it copies to your public/ folder on each build. The rule is that anything that ends up in static/ is copied exactly as-is into your public folder.

  2. It looks like you’re using your GH Pages repo just for the build. Have you considered putting everything under source control, or are you keeping your source files versioned elsewhere? If you want to keep everything in the same repo, I believe GH pages will assume a gh-pages/ branch is actually your distribution folder. If you want to change it from the command line using git, here’s a good article that will help you push to gh-pages from master.

  3. Keep in mind that your commit messages should contain enough information that you can look back on them; otherwise, you’re degrading the value of the versioning. I would change your bash script to a separate file titled build-and-deploy-hugo.sh and keep it in the root of your project (note: not the root of your public website), then add variables ($1 $2) so that you can still add proper commits at the command line before pushing it to the live site. Then you can add an alias if you really want to keep your fingers lazy. This will come in handy if you’re making a lot of content-related changes and want to look back at what you’ve written (think of it like comments in a Word doc). Let me know if you need further help.

Thanks for the feedback!

  1. I added CNAME to the static folder and that is working great.

  2. Yes, I’m tracking the source files as a source branch in the same repository.

  3. I added a prompt to the deploy script to ask for a commit message (read -p "Commit message: " commit_msg). That’s working and adds more detail to each commit. Yes, the deploy script is in the source folder and is not published with the main web site.

# delete everything in public except .git
find public -path public/.git -prune -o -exec rm -rf {} \; 2> /dev/null

You can test it with this:

find public -path public/.git -prune -o -print
1 Like

If anyone is interested, I came up with this zero downtime solution:

HUGO_PUBLISHDIR=public_tmp hugo && mv public_tmp public

Yes, you have to delete /publish before build new one and deploy
I’m using hugo and GitPage, so I wrote this batch to help me deploy getting more easier!

(Just create a .bat and put it near by the config file. One click, then input your git commit message)

hugo-publish-git-page.bat

	@echo off
	rem This script is help to deploy public from Hugo to GitPage
	rem Please make sure your files structure like

	rem /Sites/xxx.com
	rem | --------- public (git submodule as your xxx-github-io)

	rem Put this script under your hugo site's root (near by config)
	rem For more info you can reference here https://ycchaos.github.io/posts/hugo-and-git-page-publisher/
	rem -------------------------------------------------------------

	rem Input commit message
	set /P commit-message=Please input your git commit message:
	echo Your commit message "%commit-message%"
	echo %CD%

	rem cd to /public
	cd public
	echo %CD%

	rem Keep .git and .github workflow alive
	set "keep1=.git"
	set "keep2=.github"
	echo keep %keep1%
	echo keep %keep2%

	rem Delete others
	FOR /d %%a IN ("%CD%\*") DO IF /i NOT "%%~nxa"=="%keep2%" RD /S /Q "%%a"
	FOR %%a IN ("%CD%\*") DO IF /i NOT "%%~nxa"=="%keep1%" DEL "%%a"

	rem Back to root folder
	cd..
	echo Back to... %CD%

	rem Build hugo
	hugo

	rem Push public to GitPage repository
	cd public
	git add --all
	git commit -m "%commit-message%"
	git push
	cd..

	rem Push Hugo to source repository
	git add --all
	git commit -m "%commit-message%"
	git push

	pause
	rem Enjoy it :)