Skip to content

Instantly share code, notes, and snippets.

@oxagast
Last active November 1, 2023 12:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save oxagast/b2ab83af521239b379859cb28f51168a to your computer and use it in GitHub Desktop.
Push a jekyll website to webserver.
#!/bin/bash
# pujack
# a Jekyll static site pusher
#
# Designed by Marshall Whittaker / oxagast
# marshall@oxasploits.com
procs=4
clvl=32
function check_ok()
{
echo "Checking if script requriements are met..."
if [[ $(npm list minify | grep minify -c) -eq 0 ]]; then
echo "Jekyll project pusher requires minify out of npm!"
exit 1
fi
if [[ $(test -f /usr/bin/mogrify) -eq 1 ]]; then
echo "Jekyll project pusher requires ImageMagick to compress images!"
exit 1
fi
if [[ $(test -f /usr/local/bin/jekyll) -eq 1 ]]; then
echo "Jekyll project pusher requires jekyll to work!"
exit 1
fi
new_site=0
if [[ $(ls _site 2>/dev/null | wc -l) -ne 0 ]]; then
new_site=1
fi
cd ${buildroot}
echo "Envionment:"
echo "Threads: ${procs}"
echo "Compression Level: ${clvl}"
echo "Domain: ${domain}"
echo "Webroot: ${webroot}"
echo "Buildroot: ${buildroot}"
echo "Upload user: ${user}"
}
function build_site()
{
echo "Building website..."
rm -r -f ${buildroot}/_site/assets/img/*
JEKYLL_ENV=production bundle exec jekyll build > /dev/null
if [ "$?" -ne 0 ]; then exit 1; fi
BUILT=$?
rm push.err 2>/dev/null
echo "Generation complete."
}
function compress_img()
{
echo "Compressing jpg to $clvl ..."
rm ${new_images}/*.jpg ${new_images}/*.png rm ${new_images}/*.webp 2>/dev/null
cp -r ${original_images}/* ${new_images}
mogrify -format webp -resize "770x540!" -quality ${clvl} "$new_images/*.jpg"
mogrify -format webp -resize "770x540!" -quality ${clvl} "$new_images/*.png"
}
function minify_html()
{
echo "Minifying HTML..."
MHTMLLEN=$(find _site -type f -name "*.html" | wc -l)
MJSLEN=$(find _site -type f -name "*.js" | wc -l)
MCSSLEN=$(find _site -type f -name "*.css" | wc -l)
find _site -type f -name "*.html" | xargs -P ${procs} -I {} sh -c 'npx minify "$1" > "$1.out" && mv "$1.out" "$1"; echo "$1"' -- {} | pv -lep -s ${MHTMLLEN} -N "Minify HTML" > /dev/null
echo "Minifying Javascript..."
find _site -type f -name "*.js" | xargs -P ${procs} -I {} sh -c 'npx minify "$1" > "$1.out" && mv "$1.out" "$1"; echo "$1"' -- {} | pv -lep -s ${MJSLEN} -N "Minify JS" > /dev/null
echo "Minifying CSS..."
find _site -type f -name "*.css" | xargs -P ${procs} -I {} sh -c 'npx minify "$1" > "$1.out" && mv "$1.out" "$1"; echo "$1"' -- {} | pv -lep -s ${MCSSLEN} -N "Minify CSS" > /dev/null
echo "Done minifying."
}
function push_site()
{
echo "Preparing to rsync..."
branch=$(git branch | tail -n 1 | cut -d ' ' -f 2)
pushes=$(git rev-list HEAD --count)
git rev-list HEAD --count | sed -e 's/^/v/' >version.txt
echo "Pushing your" ${pushes} "commit of" ${branch} "to" ${domain} "!!!"
RSYNCLEN=$(find _site/ | wc -l)
rsync -vrX --perms --chmod=Fu=rw,Fo=r,Fg=r,Dug=rxw,Do=rx _site/ ${user}@${server}:${webroot} | tee rsync.txt | pv -lep -s ${RSYNCLEN} -N "Rsyncing" > /dev/null
if [ "$?" -ne 0 ]; then exit 1; fi
grep -E '.xml|.html|.jpg|.png|.js|.css|.txt' rsync.txt | sed -e 's|^|https://oxasploits.com/|' >modified.txt
echo "https://oxasploits.com/" | tee -a modified.txt >/dev/null
echo "Website is live on staging server!"
}
function cleanup()
{
rm cache_clear.txt rsync.txt modified.txt 2>/dev/null
find _site -type f -name "*.html.bak" -delete
find _site -type f -name "*.html.2" -delete
find _site -type f -name "*.html.3" -delete
}
function help()
{
echo "Streamlines pushing Jekyll sites to production."
echo "useage bash $0 -d oxasploits.com -s staging.oxasploits.com -w /var/www/oxasploits.com -b ~/Work/site/oxasploits.com -u webmaster -i -m -c -x"
echo
echo " -L string Makes this version live on the primary domain"
echo " -m Minifies HTML"
echo " -x int Compresses images into JPG"
echo " -w string Webserver webroot"
echo " -d string Domain"
echo " -s string Server"
echo " -b string Build root environment dir"
echo " -u string Username of who you want to upload as"
echo " -p int Number of threads to use"
echo
}
while getopts ":hc:imx:w:s:b:u:p:d:L:" option; do
case ${option} in
h)
help
exit
;;
m)
mini=1
;;
w)
webroot=${OPTARG}
;;
s)
server=${OPTARG}
;;
d)
domain=${OPTARG}
;;
b)
buildroot=${OPTARG}
;;
u)
user=${OPTARG}
;;
p)
procs=${OPTARG}
;;
x)
comp=1
clvl=${OPTARG}
;;
\?)
echo "Invalid opt"
help
exit 1
;;
esac
done
shift $((OPTIND - 1))
original_images="$buildroot/orig_img"
new_images="$buildroot/_site/assets/img/"
: ${domain:?Missing -d}
: ${server:?Missing -s}
: ${webroot:?Missing -w}
: ${buildroot:?Missing -b}
: ${user:?Missing -u}
check_ok
if [[ ${live} -eq 1 ]]; then
: ${liveroot:?Missing -L}
ssh ${user}@${server} cp -r ${webroot}/* ${liveroot}/ && echo "Pushed ${liveroot} live!"
cleanup
exit 0
fi
build_site
if [[ ${comp} -eq 1 ]]; then
compress_img
fi
for f in $(find _site/ -type f -name '*.html'); do
cp "$f" "$f.bak"
done
if [[ ${built} -eq 0 ]]; then
if [[ ${mini} -eq 1 ]]; then
minify_html
fi
find _site/ -type f -name "*.bak" -exec rm -f {} \;
push_site
echo "Fixing sitewide perms..."
PFIXLEN=$(($RSYNCLEN * 4))
cat tools/permfix.sh | ssh -t $server '/bin/bash' | pv -lep -s ${PFIXLEN} -N "Fixing Permissions" > /dev/null
echo "Finished."
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment