Skip to content

Instantly share code, notes, and snippets.

@mesuutt
Last active January 18, 2019 09:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mesuutt/2f27d78d32fc31d477293155a24b7e3f to your computer and use it in GitHub Desktop.
Save mesuutt/2f27d78d32fc31d477293155a24b7e3f to your computer and use it in GitHub Desktop.
bash script for deploying hugo site to github pages.
#!/bin/bash
# bash script for deploying hugo site to github pages.
# I have a two branch develop and master.
# develop branch keeps source files of my blog
# master branch keeps generated website files
# If you have unstaged or not tracked files, script adds these files to stash.
# So you unstash these changes after deploy.
if [[ $(git diff --name-only) ]]; then
echo 'Commit or stash changes before deploy.'
echo 'Untracked files will be stash automatically.'
exit 1
fi
_untracked_files_exist=0
if [[ $(git ls-files -o --exclude-standard) ]]; then
# stash all untracked files if exist
git stash save -u
_untracked_files_exist=1
fi
# Generate site
hugo -d public
# move generated files to /tmp
rm -rf /tmp/public
mv public /tmp
git checkout master
# Remove all files in master branch
rm -rf `pwd`/*
# Move all generated files from public directory to here
mv /tmp/public/* .
rm -r /tmp/public/
# Add new created files.
git add .
git commit -m 'Site updated'
git push
# Go back develop branch and pop changes from stash
git checkout -
# If stashed untracked files exist, pop them
if [[ $_untracked_files_exist -eq "1" ]]; then
git stash pop
git reset .
fi
@avimehenwal
Copy link

avimehenwal commented May 26, 2017

Excellent script.
I made temp path configurable. Hope this version helps

#!/bin/bash

# bash script for deploying hugo site to github pages.

# I have a two branch develop and master.
# develop branch keeps source files of my blog
# master branch keeps generated website files
# https://github.com/mesuutt/mesuutt.github.io/

SITE=~/Desktop/site
echo 'Temporary location : $SITE'

if [[ $(git diff --name-only) ]]; then
    echo 'Commit or stash changes before deploy.'
    echo 'Untracked files will be stash automatically.'
    exit 1
fi

_untracked_files_exist=0
if [[ $(git ls-files -o --exclude-standard) ]]; then
    # stash all untracked files if exist
    git stash save -u
    _untracked_files_exist=1
fi

# Generate site
hugo -d public

# move generated files to /tmp
if [-d $SITE ]; then
  rm -rvf $SITE
fi
mkdir -p $SITE
tree ~/Desktop
mv ./public/* $SITE/

git checkout master

# Remove all files in master branch
rm -vrf `pwd`/*

# Move all generated files from public directory to here
mv -v $SITE/* .
rm -rv $SITE

# Add new created files.
git add .
git commit -m 'Site updated'
git push master

# Go back develop branch and pop changes from stash
git checkout -

# If stashed untracked files exist, pop them
if [[ $_untracked_files_exist -eq "1" ]]; then
    git stash pop
    git reset .
fi
echo "DONE"

@akufta
Copy link

akufta commented Jun 22, 2017

I accidentally ran this script and I think It may have deleted some files! Is there a way to undo this?

@mesuutt
Copy link
Author

mesuutt commented Sep 15, 2017

@akufta the script stashes all changed files and not tracked files. So look at stash for files.
Are you have only master branch? If so this should be couse to error.
I am using master for keeping generated files and develop for keeping posts. I am running this script on develop branch.

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