Skip to content

Instantly share code, notes, and snippets.

@julienma
Created July 7, 2018 12:53
Show Gist options
  • Save julienma/de6fbb8607f4d06c13478a48eb9d5279 to your computer and use it in GitHub Desktop.
Save julienma/de6fbb8607f4d06c13478a48eb9d5279 to your computer and use it in GitHub Desktop.
Script to update Wordpress FR and deploy via git push (like Heroku/Dokku)
#!/bin/bash
#
# Don't forget to `chmod +x update-wordpress.sh`
#
# Updates WordPress to a new version.
#
# The MIT License (MIT)
# Copyright (c) 2016 Sander Venema <sander@sandervenema.ch>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Should we be verbose?
VERBOSE=false
# controls whether git will autocommit the changes
GIT_AUTOCOMMIT=true
# controls whether git will automatically push the changes to upstream
GIT_PUSH=true
# controls whether git will automatically push the changes to Dokku to deploy
GIT_PUSH_DOKKU=true
# name of the git remote for Dokku
GIT_DOKKU_REMOTE=dokku
# Some ANSI colours for giving colours to certain messages.
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
NC="$(tput sgr0)"
# Get latest Wordpress version from its GitHub tag
VERSION="$(curl -s https://github.com/WordPress/WordPress/tags | awk '/tag-name/{print $3;exit}' FS='[<>]')"
echo -e "==> Latest WordPress version: $VERSION"
# Set paths
CACHE_DIR=$HOME/.update_wordpress_cache
WORDPRESS_URL="https://fr.wordpress.org/wordpress-$VERSION-fr_FR.tar.gz"
WORDPRESS_DIR=$CACHE_DIR/wordpress
# Check if cache directory exists, and create it if necessary
if [[ ! -d $CACHE_DIR ]]; then
echo -e "==> ${YELLOW}Cache directory $CACHE_DIR does not exist, creating...${NC}"
mkdir $CACHE_DIR
fi
# This should be run in a WordPress directory
if [[ ! -d wp-admin && ! -d wp-includes && ! -d wp-content ]]; then
echo -e "==> ${RED}Error: Not in a WordPress directory!${NC}"
echo -e "==> ${RED}cd to a WordPress directory to update and try again.${NC}"
exit
fi
echo "==> Downloading the latest WordPress..."
if $VERBOSE; then
wget -O $CACHE_DIR/latest.tar.gz $WORDPRESS_URL
else
wget --quiet -O $CACHE_DIR/latest.tar.gz $WORDPRESS_URL
fi
if [[ -d $CACHE_DIR/wordpress ]]; then
echo "==> Removing the old WordPress from the cache..."
rm -rf $CACHE_DIR/wordpress
fi
echo "==> Unpacking WordPress tarball..."
if $VERBOSE; then
tar xfvz $CACHE_DIR/latest.tar.gz -C $CACHE_DIR
else
tar xfz $CACHE_DIR/latest.tar.gz -C $CACHE_DIR
fi
GIT=$(which git)
# Are we in a git repo?
if [[ -d .git ]]; then
GIT_REPO=true
else
GIT_REPO=false
fi
# Ready to party!
echo "==> Updating WordPress..."
echo "==> Removing wp-includes/ and wp-admin/..."
if [[ -e $GIT && $GIT_REPO == true ]]; then
$GIT rm -r wp-includes/
$GIT rm -r wp-admin/
rm -rf wp-admin/
else
rm -rf wp-includes/
rm -rf wp-admin/
fi
echo "==> Copying new files..."
cp -r $WORDPRESS_DIR/wp-{includes,admin} .
cp $WORDPRESS_DIR/wp-content/*.* wp-content/
cp $WORDPRESS_DIR/*.* .
if [[ -e $GIT && $GIT_REPO == true ]]; then
echo "==> Add new files to git repo..."
$GIT add .
if [[ $GIT_AUTOCOMMIT == true ]]; then
echo "==> Commit files to git repo..."
$GIT commit -a -m "Upgrade to WP $VERSION FR."
fi
if [[ $GIT_PUSH_DOKKU == true && $GIT_AUTOCOMMIT == true ]]; then
if git config remote.$GIT_DOKKU_REMOTE.url > /dev/null; then
echo "==> Deploying changes to Dokku..."
$GIT push $GIT_DOKKU_REMOTE
fi
fi
if [[ $GIT_PUSH == true && $GIT_AUTOCOMMIT == true ]]; then
echo "==> Push changes to remote git repo..."
$GIT push
fi
fi
echo -e "==> ${GREEN}Done! WordPress was successfully updated.${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment