Skip to content

Instantly share code, notes, and snippets.

@garrydzeng
Last active June 22, 2016 07:47
Show Gist options
  • Save garrydzeng/1c45d1ef32ff4228664f3814704f6a2d to your computer and use it in GitHub Desktop.
Save garrydzeng/1c45d1ef32ff4228664f3814704f6a2d to your computer and use it in GitHub Desktop.
checkout source code and publish to remote server via rsync
#!/bin/env bash
# deploy project script.
# make sure /usr/local/etc/deployment.key permissions is 600.
# this is required to open it.
set -e
build=
revision=HEAD
options=
help=false
dist=
while getopts "r::l::e::b::v::hc" option; do
case $option in
r) dist=$OPTARG ;;
c) options="$options --compress" ;;
l) options="$options --exclude-from=$OPTARG" ;;
e) options="$options --exclude=$OPTARG" ;;
v) revision=$OPTARG ;;
b) build=$OPTARG ;;
h) help=true ;;
esac
done
shift $(($OPTIND - 1))
# show manual
# if repository or remote pathname doesn't present,
# or help option found.
if [[ $# < 2 || $help == true ]]; then
echo "Usage: deploy [OPTIONS] REPOSITORY DISTRIBUTION"
echo
echo "Available option:"
echo " -r DIR Synchronization root."
echo " -c Compress file data during the transfer."
echo " -v REVISION Subversion revision."
echo " -l FILE Read list of source-file names from FILE."
echo " -e PATTERN Exclude files matching PATTERN."
echo " -b NAME Specified build script NAME."
echo " -h Show this help."
echo
exit 0
fi
repo=$1
cache=(/tmp/$(echo $repo | md5sum)) # checkout
path=$2
# add protocol
# it make people uses local repository
# without file://
if [[ ! $repo =~ ^[^:]+:// ]]; then
repo=file://$repo
fi
mkdir -p $cache
shift
shift
# checkout,
# if build script is available,
# run it first.
cd $cache
svn export --force --revision $revision $repo . > /dev/null
if [[ -f $build ]]; then
source $build
fi
# into dist
if [[ -n $dist && -d "$cache/$dist" ]]; then
cd "$cache/$dist"
fi
rsync --quiet --recursive --delete --force --rsh="ssh -i /usr/local/etc/deployment.key" $options . $path
cd $cache
rm -rf $(ls | grep --invert-match ^node_modules)
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment