Last active
June 7, 2017 11:01
-
-
Save apropox/7a59710e8aaf2a1d489ca036bd7c43e6 to your computer and use it in GitHub Desktop.
some simple & basic tools to deploy sage-basesd wordpress themes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configuration file for deploy-tools.sh | |
SITE="name.here.dingdong.space"; | |
THEME="theme-here"; | |
REPO="[email protected]:ding-dong/repo-here.git"; | |
BRANCH="branch-here"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# some simple & basic tools to deploy sage-basesd wordpress themes | |
# v0.4.1 | |
# by Dominik Martin | [email protected] | |
# check if current user is in the sudoers file | |
groups | grep -qw "sudo" || { echo "Sorry $LOGNAME, you need to have sudo-rights to run this script" && exit 1; } | |
# halt if any env is not set or anything throws an uncaught error | |
set -u; | |
set -e; | |
# check if .deployc is present & load it | |
[ -f .deployc ] || { echo "No .deployc config file found – aborting." && exit 1; } | |
source .deployc | |
# some more envs | |
RELEASE_DIR="/var/www/$SITE/theme-releases"; | |
THEME_DIR="/var/www/$SITE/htdocs/wp-content/themes/$THEME"; | |
RELEASE="release_`date +%Y%m%d%H%M%s`"; | |
# welcome | |
[ -d $THEME_DIR/.git ] && cd $THEME_DIR && export BRANCH="$(git rev-parse --abbrev-ref HEAD)"; | |
echo ""; | |
echo "Hej $LOGNAME, you are editing $(tput bold && tput setaf 4)$SITE$(tput sgr0) on branch $(tput bold && tput setaf 1)$BRANCH$(tput sgr0)."; | |
echo "How can I help you today?"; | |
echo ""; | |
echo "DEPLOY: Deploy updates found on remote repository."; | |
echo " Can also be used for inital deployment."; | |
echo ""; | |
echo "UNDO: Revert theme folder to previous release,"; | |
echo " e.g., if something weird just happend."; | |
echo ""; | |
echo "FIX: Switch theme folder to newest release,"; | |
echo " e.g., if you fixed a broken release manually."; | |
echo ""; | |
# tool selection | |
select dusc in "DEPLOY" "UNDO" "FIX" "exit"; do | |
case $dusc in | |
DEPLOY ) while true; do | |
read -p "DEPLOY: Do you really want to deploy a new release? (y/n) " yn | |
case $yn in | |
[Yy]* ) # prepare new release | |
echo "### preparing new release ###"; | |
if [ -d $RELEASE_DIR ] && [ -d $THEME_DIR ]; then | |
cd $THEME_DIR; | |
git fetch; | |
[[ "$(git rev-parse $BRANCH)" == "$(git rev-parse origin/$BRANCH)" ]] && echo "!!! repository already up to date -> aborting !!!" && exit 1; | |
cp -a $THEME_DIR $RELEASE_DIR/$RELEASE; | |
else | |
echo "### no existing theme found -> starting git clone ###" | |
mkdir -p $RELEASE_DIR/$RELEASE; | |
cd $RELEASE_DIR; | |
git clone -b $BRANCH $REPO $RELEASE || { echo "!!! git error -> aborting !!!"; exit 1; }; | |
echo "### installing npm ###"; | |
cd $RELEASE; | |
npm install; | |
echo "### installing bower ###"; | |
mkdir bower_components; | |
bower install; | |
fi | |
# update repo & run gulp | |
echo "### get remote updates ###"; | |
cd $RELEASE_DIR/$RELEASE; | |
git pull || { echo "!!! git error -> aborting !!!"; exit 1; }; | |
echo "### start gulp task ###"; | |
gulp --production || { echo "!!! gulp error -> aborting !!!"; exit 1; }; | |
# since error-handling in gulp is quite shitty, we better double-check manually | |
while true; do | |
read -p "Did the gulp task finish without errors? (y/n) " yn | |
case $yn in | |
[Yy]* ) break;; | |
[Nn]* ) echo "### oh no, how sad -> aborting ###"; exit 1;; | |
* ) echo "Please answer y(es) or n(o).";; | |
esac | |
done; | |
# update theme-dir | |
# reload php7.0-fpm – because: why not? | |
echo "### update theme folder & reload php-fpm ###"; | |
[ -d $THEME_DIR ] && mv $THEME_DIR $RELEASE_DIR/"$RELEASE"_b; | |
mv $RELEASE_DIR/$RELEASE $THEME_DIR; | |
sudo service php7.0-fpm reload; | |
# garbage collection: if more than 4 releases are present, delete the oldest | |
while [ $(ls $RELEASE_DIR | wc -l) -gt 4 ]; do | |
ls $RELEASE_DIR | head -1 | (echo -n $RELEASE_DIR/ && cat) | xargs rm -rf; | |
done; | |
# finish | |
echo " _______ __ .__ __. "; | |
echo " | ____|| | | \ | | "; | |
echo " ______ | |__ | | | \| | ______ "; | |
echo "|______| | __| | | | . | |______| "; | |
echo " | | | | | |\ | "; | |
echo " |__| |__| |__| \__| "; | |
echo " "; | |
exit 0;; | |
[Nn]* ) echo "Aborting – goodbye, $LOGNAME!"; | |
exit 1;; | |
* ) echo "Please answer y(es) or n(o).";; | |
esac | |
done;; | |
UNDO ) while true; do | |
read -p "UNDO: Do you really want to revert the last release? (y/n) " yn | |
case $yn in | |
[Yy]* ) if [ -d "$RELEASE_DIR/$(ls $RELEASE_DIR | tail -1)" ]; then | |
[ -d $THEME_DIR ] && mv $THEME_DIR $RELEASE_DIR/"$RELEASE"_b; | |
mv $RELEASE_DIR/$(ls $RELEASE_DIR | tail -2 | head -1) $THEME_DIR; | |
sudo service php7.0-fpm reload; | |
echo "Done – goodbye, $LOGNAME!"; | |
exit 0; | |
else | |
echo "ERROR: Could not find matching release -> aborting"; | |
exit 1; | |
fi;; | |
[Nn]* ) echo "Aborting – goodbye, $LOGNAME!"; | |
exit 1;; | |
* ) echo "Please answer y(es) or n(o).";; | |
esac | |
done;; | |
FIX ) while true; do | |
read -p "FIX: Do you really want to switch to the newest release? (y/n) " yn | |
case $yn in | |
[Yy]* ) if ! [[ "$(ls theme-releases | tail -1)" = *"_b" ]]; then | |
[ -d $THEME_DIR ] && mv $THEME_DIR $RELEASE_DIR/"$RELEASE"_b; | |
mv $RELEASE_DIR/$(ls $RELEASE_DIR | tail -2 | head -1) $THEME_DIR; | |
sudo service php7.0-fpm reload; | |
echo "Done – goodbye, $LOGNAME!"; | |
exit 0; | |
else | |
echo "ERROR: Newest release already live -> aborting"; | |
exit 1; | |
fi;; | |
[Nn]* ) echo "Aborting – goodbye, $LOGNAME!"; | |
exit 1;; | |
* ) echo "Please answer y(es) or n(o).";; | |
esac | |
done;; | |
exit ) echo "Aborting – goodbye, $LOGNAME!"; | |
exit 0;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment