Skip to content

Instantly share code, notes, and snippets.

@anthumchris
Last active March 29, 2017 18:25
Show Gist options
  • Save anthumchris/0ddb56ea5ff78634338ac8379794a5dd to your computer and use it in GitHub Desktop.
Save anthumchris/0ddb56ea5ff78634338ac8379794a5dd to your computer and use it in GitHub Desktop.
Git Push to Deploy — Instantly deploy your local Git repository to your remote web server
#/bin/bash
# This script will push your local repository's latest commit to a remote repository on your server.
# It's useful for quickly pushing your local changes to deployment servers (Dev, Stage, Prod/Live, etc.)
#
# This file should be placed in your remote server's project's git hooks folder and named .git/hooks/post-receive.
# Security reminder: Your web server should not allow access to the /.git folder URL
#
# Ensure that this script is executable:
# $ chmod +x .git/hooks/post-receive
#
# Configure your repository to update its current branch after we push to it:
# $ git config receive.denyCurrentBranch ignore
#
# From your local repository, add the Git remote where this file resides (i.e. your Stage server)
# $ git remote add stage username@myserver.com:/var/www/vhost/myapp-stage
#
# From any branch in your local repository, push the current commit to Stage's master branch. (Use --force flag when you require)
# $ git push stage HEAD:master
#
# Exit on Errors
set -e
# Script excutes from .git/ folder, so back up one to get deploy path
DEPLOY_PATH=$(cd ..; pwd)
# Specify the branch we're deploying from the push. You can also use "deploy" if you prefer to keep master unaffected
DEPLOY_BRANCH=master
echo
echo "Deploying branch '$DEPLOY_BRANCH' to $DEPLOY_PATH..."
git --work-tree=$DEPLOY_PATH --git-dir=$DEPLOY_PATH/.git checkout -f $DEPLOY_BRANCH
echo "Successfully deployed to $DEPLOY_PATH"
echo
# You can add your project's build, cache-clearing, or other scripts here. For example:
# gulp -cwd $DEPLOY_PATH build
# clear-varnish-cache.sh
# send-slack-notification.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment