Skip to content

Instantly share code, notes, and snippets.

@lemiorhan
Last active February 8, 2023 10:06
Show Gist options
  • Save lemiorhan/8912188 to your computer and use it in GitHub Desktop.
Save lemiorhan/8912188 to your computer and use it in GitHub Desktop.
Post-receive hook to deploy the code being pushed to production branch to a specific folder
#!/bin/bash
target_branch="production"
working_tree="PATH_TO_DEPLOY"
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ -n "$branch" ] && [ "$target_branch" == "$branch" ]; then
GIT_WORK_TREE=$working_tree git checkout $target_branch -f
NOW=$(date +"%Y%m%d-%H%M")
git tag release_$NOW $target_branch
echo " /==============================="
echo " | DEPLOYMENT COMPLETED"
echo " | Target branch: $target_branch"
echo " | Target folder: $working_tree"
echo " | Tag name : release_$NOW"
echo " \=============================="
fi
done
@silveur
Copy link

silveur commented Jul 4, 2016

This is great, thanks

@ryanhs
Copy link

ryanhs commented Feb 1, 2017

cool!

@grimb0t
Copy link

grimb0t commented Feb 13, 2018

A very useful bit of code and easy to follow, thanks.

It might be worth mentioning in your instructions that if you need to roll back to a previous commit you'll need to do something like git push <remote> +<commit>:<target_branch>

@Zielak
Copy link

Zielak commented Oct 3, 2018

Lovely, I event added second if condition to push my development branch to different directory 💃

@joao-pedro-alves
Copy link

Unfortunatelly it doesn't work on Bitbucket =/

@reignwestry
Copy link

I love the idea of this post-receive sample versus the standard push to master or main branch... Thanks I am trying it now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment