Skip to content

Instantly share code, notes, and snippets.

@ang3lkar
Last active December 4, 2020 16:04
Show Gist options
  • Save ang3lkar/350101ac0b48a70871cd to your computer and use it in GitHub Desktop.
Save ang3lkar/350101ac0b48a70871cd to your computer and use it in GitHub Desktop.
Git hook scripts
#!/bin/bash
remote="$1"
url="$2"
[[ $url =~ git[@|\.]heroku.com[:|\/](.+)\.git ]]
app=${BASH_REMATCH[1]}
z40=0000000000000000000000000000000000000000
IFS=' '
while read local_ref local_sha remote_ref remote_sha
do
# Example stdin values:
# local_ref refs/heads/hotfix/lazy_registration_fixes
# local_sha fe0831011d03737a5a280ee4e7b012eff4e1c474
# remote_ref refs/heads/master
# remote_sha 5e61d3437c774f82ce8ceb1039db608d246d6726
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
# If the remote is a heroku staging environment
if [[ $url =~ git[@|\.]heroku.com && $app != 'workablehr' ]]
then
echo -e "Deploying to $app, running pre-push hook\n"
# get branch that is being deployed
branch=${local_ref:11}
# get last commit short hash
sha=$(git rev-parse --short $branch)
# get last commit message
message=$(git show -s --format=%B $sha | tr ',' ' ' | tr ' ' '-' | tr '(' '-' | tr ')' '-')
# get last commit author, if it's a full name, camelcase it
author=$(git show -s --format=%an $sha | tr ' ' '-')
# get number of commits
count=$(git rev-list master..$branch --count)
# get time of deploy
deployment_timestamp=$(date +%s)
echo -e "Ah, deploying on staging, let me set the DEPLOYMENT_INFO on ${app}\n"
echo -e "Branch: \t${branch}"
echo -e "Author of last commit: \t${author}"
echo -e "SHA of last commit: \t${sha}"
echo -e "Last commit message: \t${message}"
echo -e "Commits since master: \t${count}"
echo -e "Deployment timestamp: \t${deployment_timestamp}"
echo -e "\n"
# <branch>,<commit>,<author>,<count>
# eg. feature/new_branch,Angelos-Karagkiozidis,hgfd98,Update-readme.MD,34,1446550673
value=$(echo "${branch},${author},${sha},${message},${count},${deployment_timestamp}" | tr -d '\n')
heroku config:set DEPLOYMENT_INFO=${value} --app ${app}
else
echo 'Skipping pre-push hook'
fi
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment