Skip to content

Instantly share code, notes, and snippets.

@janosgyerik
Last active November 2, 2016 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save janosgyerik/5725039 to your computer and use it in GitHub Desktop.
Save janosgyerik/5725039 to your computer and use it in GitHub Desktop.
A post-receive hook that runs a script if changes were pushed to a specific branch. (Hook scripts are in the hooks/ directory of a Git repository, and must be executable.)
#!/bin/sh
# 1. Create symlinks to the real upgrade scripts for each branch, for example:
# ./upgrade-beta.sh # will only be called when "beta" branch is updated
# ./upgrade-prod.sh # will only be called when "prod" branch is updated
#
# 2. Put these symlinks in the repository root, NOT in the hooks/ directory
# the post-receive hook receives parameters on stdin
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
upgrade_sh=./upgrade-$branch.sh
if test -e $upgrade_sh; then
upgrade_sh=$(readlink $upgrade_sh || echo $upgrade_sh)
echo calling upgrade script: $upgrade_sh
$upgrade_sh
else
echo NOT calling non-existent upgrade script: $upgrade_sh
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment