Skip to content

Instantly share code, notes, and snippets.

@oxagast
Last active March 22, 2023 15:21
  • 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
Embed
What would you like to do?
Push a jekyll website to webserver.
#!/bin/bash
# pujack
# a Jekyll static site pusher
#
# Designed by Marshall Whittaker / oxagast
# marshall@oxagast.org
procs=4
clvl=32
function check_ok()
{
echo "Checking if script requriements are met..."
if [[ $(npm list inline-critical | grep inline-critical -c) -eq 0 ]]; then
echo "Jekyll project pusher requires inline-critical out of npm!"
exit 1
fi
if [[ $(npm list html-minifier | grep html-minifier -c) -eq 0 ]]; then
echo "Jekyll project pusher requires html-minifier out of npm!"
exit 1
fi
if [[ $(npm list prepack | grep prepack -c) -eq 0 ]]; then
echo "Jekyll project pusher requires prepack 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/bin/jo) -eq 1 ]]; then
echo "Jekyll project pusher requires jo to buil json!"
exit 1
fi
if [[ $(test -f /usr/local/bin/jekyll) -eq 1 ]]; then
echo "Jekyll project pusher requires jekyll to work!"
exit 1
fi
if [[ $(pwd) != ${buildroot} ]]; then
echo "Sorry you need to be in ${buildroot}"
exit 1
fi
new_site=0
if [[ $(ls _site 2>/dev/null | wc -l) -ne 0 ]]; then
new_site=1
fi
echo "Envionment:"
echo "Ezoic API Key: ${apikey}"
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
if [ "$?" -ne 0 ]; then exit 1; fi
BUILT=$?
rm push.err 2>/dev/null
}
function compress_img()
{
echo "Compressing jpg to $clvl ..."
rm ${new_images}/*.jpg ${new_images}/*.png 2>/dev/null
cp -r ${original_images}/* ${new_images}
mogrify -format jpg ${new_images}/*.png
mogrify -resize "650^x500" -quality ${clvl} -path "$new_images" "$new_images/*.jpg"
}
function inline_css()
{
echo "Fixing CSS..."
npx sass _sass/jekyll-theme-chirpy.scss -I _sass 2>/dev/null >css/style.css
echo "Done merging css from scss..."
(
for f in $(find _site/ -type f -name '*.html'); do
((p = p % procs))
((p++ == 0)) && wait
echo "Inlining .css to .html: $f"
npx inline-critical "$f" -c css/style.css -b _site/ >"$f.2" 2>/dev/null || mv "$f.bak" "$f" &
done
)
for f in $(find _site/ -type f -name '*.html.2'); do
mv "$f" $(echo "$f" | sed -e 's/.2$//')
done
}
function minify_html()
{
echo "Minifying HTML"
(
for f in $(find _site/ -type f -name '*.html'); do
((p = p % procs))
((p++ == 0)) && wait
echo "Minifying .html: $f"
npx html-minifier --max-line-length 120 --remove-empty-elements --remove-attribute-quotes --use-short-doctype --collapse-whitespace --minify-css --minify-js --minify-html --minify-urls --remove-comments --output "$f.3" "$f" || mv "$f.bak" "$f" &
if [ "$?" -ne 0 ]; then exit 1; fi
done
) && for f in $(find _site/ -type f -name '*.html.3'); do
mv "$f" $(echo "$f" | sed -e 's/.3$//')
done
find _site -type f -name "*.html.bak" -delete
echo
echo "Done minifying..."
}
function fix_js() {
for fn in $(find -name '*.js' |grep _site/assets/); do
prepack $fn --out $fn
if [ "$?" -ne 0 ]; then exit 1; fi
done
}
function push_site()
{
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
rm _site/push.sh _site/push.err
echo "Pushing your" ${pushes} "commit of" ${branch} "to" ${domain} "!!!"
rsync -vrX _site/ ${user}@${server}:${webroot} | tee rsync.txt;
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
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 " -i Inlines CSS into HTML"
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
;;
i)
inline_it=1
;;
m)
mini=1
;;
w)
webroot=${OPTARG}
;;
s)
server=${OPTARG}
;;
d)
domain=${OPTARG}
;;
b)
buildroot=${OPTARG}
;;
u)
user=${OPTARG}
;;
p)
procs=${OPTARG}
;;
L)
live=1
liveroot=${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
fi
build_site
compress_img
for f in $(find _site/ -type f -name '*.html'); do
cp "$f" "$f.bak"
done
if [[ ${built} -eq 0 ]]; then
if [[ ${inline_it} -eq 1 ]]; then
inline_css
fi
if [[ ${mini} -eq 1 ]]; then
minify_html
fi
fix_js
find _site -type f -name "*.html.bak" -delete
# fix err pages
mkdir -p _site/error/403
mkdir -p _site/error/404
mv _site/403.html _site/error/403/index.html
mv _site/404.html _site/error/404/index.html
push_site
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment