Skip to content

Instantly share code, notes, and snippets.

@rmanalan
Created December 9, 2010 20:13
Show Gist options
  • Save rmanalan/735260 to your computer and use it in GitHub Desktop.
Save rmanalan/735260 to your computer and use it in GitHub Desktop.
My Git Deploy Workflow

My Git Deploy Workflow

I use this for static and simple Sinatra based sites -- great for prototyping simple apps. Credit goes to http://toroid.org/ams/git-website-howto for the original idea.

If you don't know what this is, here's an example of how I deploy my website/app to a server:

# create/update/delete files in my site
git add .
git commit -m "description of the changes I made"
git push
# rinse and repeat

A lot of web designers or front-end'ers like to work with FTP to deploy their static sites. This is how I do it.

Remote repo

Create a remote git repository:

mkdir myproject.git && cd myproject.git
git init --bare

Then we define (and enable) a post-receive hook that checks out the latest tree into a directory where you'll serve your app from. I like to just create a ./www directory from within the bare git repo for simplicity:

mkdir myproject.git/www
cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=./myproject.git/www git checkout -f
chmod +x hooks/post-receive

Local repo

If you don't have an existing git repo with your code in it, you can create one...

Create a repo

mkdir myproject && cd myproject
git init
echo 'Hola Mundo!' > index.html
git add index.html
git commit -m "The humble beginnings of my web site."

... or you can initialize a git repo in your existing code base:

Initialize a git repo inside an existing directory

git init .
git add .
git commit -m "initial commit"

Link your local repo with your remote repo

After you've set up your local repository, the next step is to link up your local repository to your remote repository:

git remote add origin me@gitserver.com:[path to]myproject.git
git push origin master

That last command git push origin master will "push" your local repo to the remote repo and also copy your code to myproject.git/www.

Serving up your site

Static site

I usually use this workflow when I'm prototyping or building out sites. I usually don't bother with apache or nginx since sometimes it's easier to just use python to serve up simple static sites:

python -m SimpleHttpServer &

This will launch a simple HTTP server on port 8000. The important thing is that you'll need to keep this running in the background somehow -- and it's not trivial to turn it into a daemon. My simple solution to problems like this is to run this inside a screen session

Sinatra

I use Sinatra for lots of my small projects and this workflow works well for most of them. The process for Sinatra is the same for static sites, but instead of using python to serve up the site, I like using shotgun. Shotgun is used primarily for development so it's not ideal for running production sites on it, but it works well for my purposes. So, to run your sinatra app, all you need to do is run shotgun inside a screen session:

shotgun -o hostname-to-listen-on [-p port]

The -o parameter is important to set since shotgun will only listen to 127.0.0.1 if you don't set it.

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