Skip to content

Instantly share code, notes, and snippets.

@AvnerCohen
Forked from remy/deploy.sh
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AvnerCohen/000a78e39120c41eccd6 to your computer and use it in GitHub Desktop.
Save AvnerCohen/000a78e39120c41eccd6 to your computer and use it in GitHub Desktop.
#!/bin/bash
readonly ARGS="$@"
readonly DEPLOY_USER="www-data"
clone_repo_at_tag() {
local repo="$1"
local tag="$2"
local project_name="$3"
local repo_dirname="$project_name-$tag"
echo ">>> cloning $tag..."
git clone --depth 1 -b $tag https://github.com/$repo.git $repo_dirname &> /dev/null
if [ $? -ne 0 ]
then
echo "*** clone failed"
exit 1
fi
build $repo_dirname
}
diff_deps() {
echo ">>> diffing dependencies"
local repo_dirname="$1"
diff -B <(cat jsbin/package.json | json dependencies) <(cat $repo_dirname/package.json | json dependencies)
if [ $? -ne 0 ]
then
echo "*** package diff failed"
exit 1
fi
}
build() {
local repo_dirname="$1"
local d=$(diff_deps $repo_dirname)
cd $repo_dirname
echo ">>> copying node modules from v$(json -f ../jsbin/package.json version)..."
cp -R ../jsbin/node_modules .
if [ $? -ne 0 ]
then
echo "*** copy previous version of modules failed"
exit 1
fi
if [ -z "$d" ]; then
echo ">>> no update required"
else
echo ">>> updating npm modules"
npm install &> /dev/null
if [ $? -ne 0 ]
then
echo "*** npm install failed"
exit 1
fi
fi
echo ">>> running build..."
grunt build &> /dev/null
if [ $? -ne 0 ]
then
echo "*** grunt build failed"
exit 1
fi
}
symlink_repo_to_name() {
local repodir="$1"
local project_name="$2"
echo ">>> symlinking $repodir"
ln -nfs /WWW/$repodir /WWW/$project_name
}
get_latest_tag() {
local repo="$1"
curl -s https://api.github.com/repos/$repo/tags | json 0.name
}
restart_jsbin() {
echo ">>> restarting..."
sudo restart jsbin
}
update_repo_and_symlinks() {
local cwd=$(pwd)
local args="$@"
cd /WWW/
local current_version=$(json -f jsbin/package.json version)
# read the arg first, or get it from the environment value
local repo=${1:-$REPO}
local tag=$(get_latest_tag $repo)
local project_name=$(echo $repo | awk -F'/' '{print $2}')
clone_repo_at_tag $repo $tag $project_name
cd $cwd
symlink_repo_to_name "$project_name-$tag" $project_name
}
run_as_deploy_user() {
echo "*** launching deploy as $DEPLOY_USER"
sudo -Hu "$DEPLOY_USER" -- sh -c "$0 $ARGS"
if [ $? -ne 0 ]
then
echo "*** deploy failed"
exit 1
fi
}
main() {
if [ "$USER" != $DEPLOY_USER ]
then
run_as_deploy_user
restart_jsbin
echo ">>> deploy complete"
exit 0
else
update_repo_and_symlinks $ARGS
exit 0
fi
}
main $ARGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment