Skip to content

Instantly share code, notes, and snippets.

@necolas
Last active January 1, 2016 21:09
Show Gist options
  • Select an option

  • Save necolas/8201662 to your computer and use it in GitHub Desktop.

Select an option

Save necolas/8201662 to your computer and use it in GitHub Desktop.
Ghetto git deployment

Simple git deployment strategy

I wanted something simple for staging and deploying my static site.

  1. Generate build locally
  2. Push the build to a bare git repo on your server
  3. A post-receive hook checks out the files in the public directory

Remote server config

Repeat for staging server, if you want.

# ssh into your server
$ ssh you@example.org

# create a directory for your site
$ mkdir ~/example.com

# create a directory for git
$ mkdir ~/example.git 

# create a bare git repo
$ cd example.git
$ git init --bare

# setup a post-receive hook
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/path/to/example.com git checkout -f
$ chmod +x hooks/post-receive

Local process

I'm using Make:

make prod
make staging
# Local project makefile
BUILD_DIR := ./build
DEPLOY_DIR := ./deploy
STAGING_REPO = ssh://you@example.org/~/staging.example.git
PROD_REPO = ssh://you@example.org/~/example.git
build:
# whatever your build step is
clean-deploy:
@ rm -rf $(DEPLOY_DIR)
clone-prod: clean-deploy
git clone $(PROD_REPO) $(DEPLOY_DIR)
clone-staging: clean-deploy
git clone $(STAGING_REPO) $(DEPLOY_DIR)
# force update the remote repo with whatever is in the build directory
deploy:
cp -R $(BUILD_DIR)/ $(DEPLOY_DIR) && \
cd $(DEPLOY_DIR) && \
git checkout --orphan new_master && \
git add -A && \
git commit -m "Release" && \
git branch -M master && \
git push -f origin +master:refs/heads/master
prod: build clone-prod deploy
staging: build clone-staging deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment