Bash Script to auto-update WordPress Plugin and Theme GIT Reporitory

Recently eForm has seen a lot of changes. I use GIT and Gitlab to manage the private repository and pull from the gitlab to my hosting server. In the hosting server, I have three websites running eForm:

  • eform.live – The primary presentation website.
  • clients.ipanelthemes.com – For admin demo.
  • sandbox.ipanelthemes.com – A sandbox for my personal testing.

Everytime I would tag a release, I have to ssh into my server, change user account and do a git pull for all three of those websites. That is a lot of time consuming work. So I was looking for a way to easily manage the deployment.

WPPusher looked like a handy solution, but even so, I would need to login into all three websites and do a deployment by git pull. So, what I though would be best is to come up with a bash script which would:

  1. Loop through my specified server user accounts.
  2. Log into those user accounts.
  3. Do a git pull in the directory I have specified.

Sounds nice, but unfortunately I had no idea how to. So I resorted to my good friend Prasanth (who is a Sys Admin working at Accenture (and also getting married!!)) and after numerous phone calls and discussion, we came up with a nifty script.

The bash script

 

#!/bin/bash
#
# Performs automatic updates of git repositories
# Inside /home/${user}/public_html/wp-content directory
# Checks for the path specified inside
# If it is there, then performs a `git pull origin`
# Shows output on screen
#
# @author Swashata <swashata@iptms.co>
# @contributor Prasanth Gopinath ( Sys Admin: Accenture )
# @link https://www.intechgrity.com
#
# Call this script like ./update-repo.sh "plugins/my-plugin"
# To perform `git pull origin`
# inside `/home/${user}/public_html/wp-content/plugins/my-plugin/`
#
# Installation:
#
# 1. Copy this script in your directory ~/update-git/update-repo.sh
# 2. Change the `serverUsers` array into actual usernames for your server
# 3. CD to the directory
#    $ cd ~/update-git
# 4. CHMOD to executable
#    $ chmod +x update-repo.sh
# 5. Execute as root
#    $ sudo ./update-repo.sh
#

# User Directory array
declare -a serverUsers=("user1" "user2" "www" "www-data") ## Change this

# Search path
SEARCH_PATH_SPECIFIED=$1

## now loop through the above array
for i in "${serverUsers[@]}"; do
  ## Check the directory
  if [[ ${SEARCH_PATH_SPECIFIED} != "" ]]; then
    ## Calculate user directory
    SEARCH_PATH_USER="/home/${i}/public_html/wp-content/${SEARCH_PATH_SPECIFIED}/"

    ## Check if directory actually exists
    if [[ -d "$SEARCH_PATH_USER" ]]; then
      ## Admin has specified directory, so check there only
      echo "Checking Specified directory ${SEARCH_PATH_USER}"
      su - $i <<EOF
      cd ${SEARCH_PATH_USER}
      echo "Current Directory Structure"
      ls -al|tail
      echo "Doing Git Pull"
      git pull origin
      echo "Operation Done"
EOF
    else
      echo "Sorry the directory ${SEARCH_PATH_USER} for User ${i} does not exist. Skipping!"
    fi
  else
    echo "Error! Please specify directory"
  fi
  # or do whatever with individual element of the array
done

Installation of the auto-update script

Installation is pretty easy.

  • Modify line 30 to have your actual users.
  • Create a file somewhere in your server.
    nano update-repo.sh
  • Paste the script in that file.
  • Make it executable.
    chmod +x update-repo.sh
  • Run with sudo.
    sudo ./update-repo.sh "plugins/my-plugin"

    where plugins/my-plugin is the directory where you would like to perform git pull.

Quite easy now isn’t it? If you have any trouble, feel free to comment.

Leave a Reply