Skip to content

Instantly share code, notes, and snippets.

@zackjp
Last active November 19, 2019 02:00
Show Gist options
  • Save zackjp/8816795 to your computer and use it in GitHub Desktop.
Save zackjp/8816795 to your computer and use it in GitHub Desktop.
Simple Git deploy script for PHP apps

Simple Git deploy script for PHP apps

Usage

SSH into your server then copy deploy.sh, deploy.json, and deploy_post_hook.sh.

git clone https://gist.github.com/8816795.git

Then set your configs in json and run:

./deploy.sh deploy.json

How it works

Here is what deploy.sh does:

  1. check for current deploy number on server (based on dir names)
  2. create new deploy dir for current version + 1
  3. copy master branch from GitHub via svn into new deploy dir (will prompt for credentials)
  4. call your post deploy hook, which can link public_html -> new deploy public dir
  5. ?
  6. profit
{
"repo":"https://github.com/username/com.your.app.git",
"deploy_dir_prefix":"main_v",
"deploy_post_hook":"deploy_post_hook.sh"
}
#!/bin/bash
set -e
echo "*** Starting deploy..."
CWD=$(pwd)
CONFIG_FILE=$1
if [ -z $CONFIG_FILE ]
then
echo "*** Config file not found"
exit 1
fi
echo "*** Reading config file"
CONFIG=$(cat ${CONFIG_FILE})
echo $CONFIG
if [ -z "${CONFIG}" ]
then
echo "*** Deploy config was empty"
echo "*** Aborting deploy..."
exit 1
fi
echo "*** Defining cfg function"
function cfg () {
local config=$1
local key=$2
local regex="\"${key}\":\"([^\"]*)\""
[[ ${config} =~ $regex ]]
echo ${BASH_REMATCH[1]}
}
echo "*** Defining current_version function"
function current_version () {
local dir_version_regex="$1([0-9]+)"
local version=0
for f in */
do
[[ $f =~ $dir_version_regex ]]
if [[ ${BASH_REMATCH[1]} ]] && (( ${BASH_REMATCH[1]} > $version ))
then
version=${BASH_REMATCH[1]}
fi
done
echo $version
}
echo "*** Loading configs"
repo=$(cfg "${CONFIG}" "repo")
deploy_dir_prefix=$(cfg "${CONFIG}" "deploy_dir_prefix")
deploy_post_hook=$(cfg "${CONFIG}" "deploy_post_hook")
echo "repo = ${repo}"
echo "deploy dir prefix = ${deploy_dir_prefix}"
echo "post hook = ${deploy_post_hook}"
old_version=$(current_version $deploy_dir_prefix)
let new_version=$old_version+1
target_deploy_dir="${CWD}/${deploy_dir_prefix}${new_version}"
echo "*** Upgrading version from ${old_version} => ${new_version}"
echo "*** Deploying to ${target_deploy_dir}"
svn export "${repo}/trunk" "${target_deploy_dir}"
if [ ! -d $target_deploy_dir ]
then
echo "*** Target deploy dir not found (${target_deploy_dir})"
echo "*** Aborting deploy..."
exit 1
fi
if [ -x $deploy_post_hook ]
then
echo "*** Executing deploy post-hook (${deploy_post_hook})"
sh $deploy_post_hook $target_deploy_dir
else
echo "*** Deploy post-hook (${deploy_post_hook}) does not have exec permission"
fi
echo "*** Deploy post-hook finished"
echo "*** Completed deploy"
#!/bin/bash
CWD=$(pwd)
DEPLOY_DIR=$1
APP_PUBLIC_DIR="${DEPLOY_DIR}/src/public"
# Example of copying/linking any necessary production files
# cp -r /path/to/copied/file "${DEPLOY_DIR}/src/public/"
# ln -s /path/to/shared/data/across/deploys/ "${DEPLOY_DIR}/src/private/shared/data"
echo "*** Creating symlink: public_html -> ${APP_PUBLIC_DIR}"
rm public_html
ln -s ${APP_PUBLIC_DIR} public_html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment