Skip to content

Instantly share code, notes, and snippets.

@alexlafroscia
Forked from atomkirk/publish_using_git.md
Last active January 3, 2016 02:09
Show Gist options
  • Save alexlafroscia/8393580 to your computer and use it in GitHub Desktop.
Save alexlafroscia/8393580 to your computer and use it in GitHub Desktop.

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
    
  4. Make the owner of all files in the repo to the git user.

     $ sudo chown -R git .
    
  5. 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
    
  6. 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
    
  7. 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
    
  8. Move your publish ssh key to the server:

     $ scp ~/Desktop/id_rsa.pub [email protected]:/Users/git
    
  9. 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
    
  10. Create a post-receive hook to checkout the repo to your web folder:

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

    $ git add remote production [email protected]:/mt/git/getfirehose.git
    
  12. Push to your repo

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

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

    $ vi /usr/local/etc/nginx/nginx.conf
    
  15. 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;
          }
      }
    }
    
  16. Make sure your git user owns everything in the site folder

    $ sudo chown -R git /mt/sites/getfirehose
    
  17. 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