Skip to content

Instantly share code, notes, and snippets.

@sergeylukin
Created June 23, 2013 19:08
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 sergeylukin/5846152 to your computer and use it in GitHub Desktop.
Save sergeylukin/5846152 to your computer and use it in GitHub Desktop.
Run commands remotely through `git push` - example with 2 levels deployment
#! /usr/bin/env sh
# This script accepts 2 mandatory arguments:
# COMMAND and ENVIRONMENT
#
# It's run by git repo hook and current
# workign directory is the bare repo path
#
# Global variables accessible:
# $WORKTREE (points to the working tree)
#
command=$1
environment=$2
if [ ! $command ] || [ ! $environment ]
then
echo " ___ _ "
echo " / _ \ ___ _ __ ___| |"
echo " | | | |/ _ \| '_ \/ __| |"
echo " | |_| | (_) | |_) \__ \_|"
echo " \___/ \___/| .__/|___(_)"
echo " |_| "
echo "--------------------------------------------"
echo "--------------------------------------------"
echo "--------------------------------------------"
echo " Wrong arguments passed to branch2command.sh"
echo "--------------------------------------------"
echo " USAGE: branch2command.sh <command> <environment>"
echo "--------------------------------------------"
echo "--------------------------------------------"
echo "--------------------------------------------"
exit 1
fi
if [ $environment == "hub" ]
then
###############################
#### Match command for HUB
###############################
case $command in
# Deploy master branch to nodes
deploy)
script="$WORKTREE/deploy.sh"
if [ -e $script ]
then
. $script master
matched=1
else
echo "script could not found"
exit 1
fi
;;
rollback)
git --work-tree=$WORKTREE reset --hard HEAD~
matched=1
;;
# for any NODE-* commands - just redirect them to nodes..
node-*)
# Run this command on remote nodes
. $WORKTREE/deploy.sh $command
;;
esac
elif [ $environment == "node" ]
then
###############################
#### Match command for NODE
###############################
case $command in
node-build)
# Build script should be executed in the root
# of working tree
cd $WORKTREE
script="build.sh"
if [ -e $script ]
then
. $script
matched=1
else
echo "script could not found"
exit 1
fi
;;
esac
fi
######
# END
######
if [ $matched ]
then
echo " _ .-') _ .-') _ ('-. "
echo "( ( OO) ) ( OO ) )_( OO) "
echo " \ .'_ .-'),-----. ,--./ ,--,'(,------. "
echo " ,\`'--..._)( OO' .-. '| \ | |\ | .---' "
echo " | | \ '/ | | | || \| | )| | "
echo " | | ' |\_) | |\| || . |/(| '--. "
echo " | | / : \ | | | || |\ | | .--' "
echo " | '--' / \`' '-' '| | \ | | \`---. "
echo " \`-------' \`-----' \`--' \`--' \`------' "
echo "--------------------------------------------"
echo "--------------------------------------------"
echo "--------------------------------------------"
echo " Command was executed. Ignore errors below"
echo "--------------------------------------------"
echo "---------------------------------------^----"
echo "---------------------------------------|----"
echo "-But verify there are no errors above--|----"
echo "--------------------------------------------"
exit 1
fi
#! /usr/bin/env sh
#
# This hook is placed in a Bare repository.
#
# It executes a script inside working tree (so it's part of repo)
# that contains some logic on "what to do" if such "branch name"
# was pushed. If it finds it, it does an action and exits
# So, we actually are using git to run commands remotely from withing
# repo hook which gives our script direct access to the repo for free
#
# Assuming following file structure:
# .
# |-- myproject
# |-- myproject.git
# set WORKTREE=../myproject
#
# Where myproject.git - current bare repo and myproject - working directory
# To enable this hook, rename this file to "pre-receive" and make sure it is executable
#
WORKTREE=../myproject
# environment is used in some scripts
# for instance, script "branch2command.sh" takes
# different actions depending on environment
# it runs in
#
# Possible values are: "hub", "node"
# Where "hub" is the bare repository to which
# developers push
# and "node" is for nodes that receive pushes from
# hub and that run actual apps
#
# for example, running "build" command is more reasonable
# on "node" so it will run only there
# while, running "rollback" sounds more reasonable for "hub",
# also "deploy" is good for "hub" as it is it's primary task
#
# hopefully you get the point
#
environment="hub"
while read oldrev newrev ref
do
branch=$(echo $ref | awk -F'/' '{print $3}')
# Convert branch name into a command
branch2command_script="$WORKTREE/branch2command.sh"
if [ -e "$branch2command_script" ]
then
. $branch2command_script $branch $environment
fi
if [ "$branch" != "master" ] && [ "$branch" != "docs" ]
then
echo Unrecognized branch name
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment