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:

  • – The primary presentation website.
  • – For admin demo.
  • – 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


# 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 <>
# @contributor Prasanth Gopinath ( Sys Admin: Accenture )
# @link
# Call this script like ./ "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/
# 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
# 5. Execute as root
#    $ sudo ./

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

# Search path

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

    ## 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"
      echo "Sorry the directory ${SEARCH_PATH_USER} for User ${i} does not exist. Skipping!"
    echo "Error! Please specify directory"
  # or do whatever with individual element of the array

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.
  • Paste the script in that file.
  • Make it executable.
    chmod +x
  • Run with sudo.
    sudo ./ "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.