Created
April 2, 2015 16:06
-
-
Save remy/bcab0b4d74cd49d37700 to your computer and use it in GitHub Desktop.
JS Bin's deploy script, run using `deploy jsbin/jsbin` (though the argument should probably just be hard coded since jsbin appears in the source)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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