Skip to content

Instantly share code, notes, and snippets.

@zouppen
Last active January 20, 2017 20:24
Show Gist options
  • Save zouppen/c67bcaff1db6c85a8786873d3fde10df to your computer and use it in GitHub Desktop.
Save zouppen/c67bcaff1db6c85a8786873d3fde10df to your computer and use it in GitHub Desktop.
Git autodeployment script
command="~/git_deploy_hook",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty DEPLOYMENT_KEY_HERE
#!/bin/sh -eu
#
# Idea taken from http://stackoverflow.com/a/38994123/514723
# Fetching new commits
cd /PATH/TO/REPO
git fetch
# Save the local changes, keep a reference to them
stashed_commit=`git stash create`
# Reset working tree to the sandbox branch on server
git reset --hard origin/sandbox
# If there were local changes, then restore them
if test -n "$stashed_commit"; then
if ! git stash apply "$stashed_commit"; then
# Autodeployment failed. Reverting.
git reset --hard "$stashed_commit"
git reset HEAD^
echo 'Autodeployment FAILED! Local changes on sandbox server? You need to resolve manually.'
exit 1
fi
fi
# Add here some other tasks to do after deployment, such as compiling locales etc.
echo Successfully deployed to https://sandbox.example.com
#!/bin/sh -eu
#
# Deploy the stuff into sandbox if the branch in question is "sandbox"
# Modeled after http://stackoverflow.com/a/13057643/514723
for refname in "$@"; do
branch=`git rev-parse --symbolic --abbrev-ref "$refname"`
if test sandbox = "$branch"; then
ssh -T YOUR_SANDBOX_SERVER
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment