Skip to content

Instantly share code, notes, and snippets.

@atomkirk
Last active December 31, 2015 21:09
Show Gist options
  • Save atomkirk/8044479 to your computer and use it in GitHub Desktop.
Save atomkirk/8044479 to your computer and use it in GitHub Desktop.
Publishing Using Git

You've got files in a git repo and you want them on a static server. Here's how it's done using a Mac Mini:

  1. Set up your local repo

     $ mkdir marketing && cd marketing
     $ git init
     $ echo 'Hello, world!' > index.html
     $ git add .
     $ git commit -am "init"
    
  2. On the server, init a bare repo

     $ cd /mt/git
     $ mkdir getfirehose.git && cd getfirehose.git
     $ git init --bare --shared
    

3.Create a git user in System Preferences. If can't screenshare into your server for some reason, you can also do this:

    $ sudo dscl . -create /Users/git
    $ sudo dscl . -create /Users/git UserShell /usr/bin/git-shell
    $ sudo dscl . -create /Users/git RealName "Git server"
    $ sudo dscl . -create /Users/git UniqueID 504
    $ sudo dscl . -create /Users/git PrimaryGroupID 20
    $ sudo dscl . -create /Users/git NFSHomeDirectory /Users/git
    $ sudo dscl . -passwd /Users/git [the users password]
    $ sudo mkdir -p /Users/git
    $ sudo chown -R git:staff /Users/git
  1. Make the owner of all files in the repo to the git user.

     $ sudo chown -R git .
    
  2. Change git user's shell to use git-shell (this allows the git user to do only git things)

     $ chsh -s /usr/bin/git-shell
     $ sudo dscl . -create /Users/git UserShell /usr/bin/git-shell
    
  3. Print a greeting when someone tries to ssh in as the git user:

     $ mkdir $HOME/git-shell-commands
     $ cat >$HOME/git-shell-commands/no-interactive-login <<\EOF
     #!/bin/sh
     printf '%s\n' "Hi $USER! You've successfully authenticated, but I do not"
     printf '%s\n' "provide interactive shell access."
     exit 128
     EOF
     $ chmod +x $HOME/git-shell-commands/no-interactive-login
    
  4. Create SSH keys on your local machine (if you already have them, skip this step)

     $ mkdir .ssh
     $ chmod 700 .ssh
     $ cd .ssh
     $ ssh-keygen -t rsa
     $ cp id_rsa.pub ~/Desktop
    
  5. Move your publish ssh key to the server:

     $ scp ~/Desktop/id_rsa.pub git@git.some.domain:/Users/git
    
  6. Add your local machines public key to the git user's authorized_keys file.

     $ mkdir .ssh
     $ chmod 700 .ssh
     $ cat id_rsa.pub >> .ssh/authorized_keys
    
  7. Create a post-receive hook to checkout the repo to your web folder:

    $ mkdir /mt/sites/getfirehose
    $ cd /mt/git/getfirehose.git
    $ cat > hooks/post-receive
    #!/bin/sh
    GIT_WORK_TREE=/mt/sites/getfirehose git checkout -f
    $ chmod +x hooks/post-receive
    
  8. Add your repo as a remote on your local machine.

    $ git add remote production git@mtmini.com:/mt/git/getfirehose.git
    
  9. Push to your repo

    $ git push production master
    
  10. Install nginx (could be a lot more complex, but if you're starting off simple)

    $ brew install nginx
    
  11. Open the nginx config file:

    $ vi /usr/local/etc/nginx/nginx.conf
    
  12. Configure nginx to serve up your site:

    worker_processes  1; 
     
    events { 
      worker_connections  1024; 
    } 
     
    http { 
      include       mime.types; 
      default_type  application/octet-stream; 
    
      sendfile        on; 
      keepalive_timeout  65; 
      gzip  on; 
    
      server {
          listen       80;
          server_name  getfirehose.com www.getfirehose.com;
    
          location / {
              root   /mt/sites/getfirehose/;
              index  index.html index.htm;
          }
      }
    }
    
  13. Make sure your git user owns everything in the site folder

    $ sudo chown -R git /mt/sites/getfirehose
    
  14. Start or restart nginx:

    $ sudo kill -HUP `cat /usr/local/var/run/nginx.pid`
    

http://getfirehose.com

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