Created
June 23, 2013 19:02
-
-
Save sergeylukin/5846124 to your computer and use it in GitHub Desktop.
Run commands remotely through `git push` - example with 1 level deployment
This file contains hidden or 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
#! /usr/bin/env sh | |
# This script accepts 1 mandatory argument: | |
# COMMAND | |
# | |
# 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 | |
if [ ! $command ] | |
then | |
echo " ___ _ " | |
echo " / _ \ ___ _ __ ___| |" | |
echo " | | | |/ _ \| '_ \/ __| |" | |
echo " | |_| | (_) | |_) \__ \_|" | |
echo " \___/ \___/| .__/|___(_)" | |
echo " |_| " | |
echo "--------------------------------------------" | |
echo "--------------------------------------------" | |
echo "--------------------------------------------" | |
echo " Wrong arguments passed to branch2command.sh" | |
echo "--------------------------------------------" | |
echo " USAGE: branch2command.sh <command>" | |
echo "--------------------------------------------" | |
echo "--------------------------------------------" | |
echo "--------------------------------------------" | |
exit 1 | |
fi | |
############################### | |
#### Match command | |
############################### | |
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 | |
;; | |
esac | |
###### | |
# 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 |
This file contains hidden or 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
#! /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 | |
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 | |
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