Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Git branches directly into a live server directory so files are seen live

How can I set up remote directories in Git where I can locally push a stage branch to the remote and see the live changes on a staging server, like stage.example.com?

The notion I have (part of why I am moving away from SVN) is that I can maintain (locally) 3 different "main" branches as follows:

  • master - used for local development, working dir
  • stage - should be in-sync with staging server directory (remote)
  • live - this should be the publicly accessible website (remote)

The idea I have (and what others claim is possible) is that I can maintain these remote "sites" from my local computer without having to constantly log into my remote server shell and run svn update (in my current svn workflow I need to do this all the time…) or of course in my Git workflow run git pull on the remote.

How can I setup the remote directories so that I can locally push my stage branch to the staging remote server and see the changes on (for example) stage.example.com right away?

Then once the stage is all okay and tested I would just locally be able to push to the live remote to make these changes that I have tested on the stage to the live website.

Can this even be done or am I getting crazy ideas here that are simply not meant to be done with Git?

In case this is of importance here are a few stats about my local and remote servers:

remote server:      Dreamhost (shared account)
remote GIT version: 1.7.1.1
remote GIT client:  shell

local computer:     Mac Pro (Snow Leopard 10.6.6)
local GIT version:  1.7.2.3
local GIT client:   Tower.app // git-tower.com

Also, so far I have unsuccessfully tried the following workflow:

  1. Create a --bare Git repo on the remote (so I can access it from everywhere)
  2. Clone this remote repo to a local directory and the use Git Tower app to manage it
  3. Work locally in master (HEAD)
  4. scp -r copy the --bare git repo from the remote server into my remote live domain stage.example.com
  5. Add remote to local working copy and then try to push to origin/stage

Clearly this doesn't work but I don't know why or how to do it any better.

Coming from a SVN background I'm new to Git but have watched plenty of tutorials (Peepcode & ThinkVitamin) but still cannot figure out how to set this up.

like image 866
Jannis Avatar asked Apr 02 '11 00:04

Jannis


1 Answers

The one notion to realize with a DVCS ("Distributed" VCS, like Git or Mercurial) is that it adds the notion of publication (push/pull) to the notion of branching.
A CVCS ("Centralized" VCS, like SVN) has only branching (and one central repo to push to on a server).

In your case, staging or live are publication steps, i.e. different Git repo ready to receive the modifications you want to see in staging or in live environment.

That would mean:

  • 2 branches to keep track of what belong to staging ("staging" branch) or live ("live" branch)
  • 1 remote bare repo (in order to be able to push to it, pushing either the staging or the live branch
  • 1 post-update hook for the bare repo in order to checkout and update a working tree (representing your actual "staging" or "live" files)
  • 1 local repo where you add the bare repo as remote, and where you can push to staging or to live.
    You can also clone the bare repo to any other local computer you need to work on.

The difference between a post-receive and a post-update hook is that the post-update one is executed once for every branch modified:
See the "Git hook to update various web folders based on branch pushed to remote server" SO question.

On the initial push, do a "git push --all origin" and all branches will be created on the remote bare repo.

The idea is no pulling should be involved on the server side: Only a git --work-tree=/path/to/your/live/files/ checkout live or git --work-tree=/path/to/your/staging/files/ checkout staging, depending on the parameters of the post-update hook: you only checkout the files of the bare repo into these 'folders' on the server.

If you do a ruby script for your hook, make sure to:

  • use the right shebang: #!/usr/bin/env ruby,
  • surround your git command with backtick should be enough: `git ...`, like in this script,
  • use ENV['HOME'] to specify the homedir of the current user within said script, if you want commands like `cd ~/stagedomain.com` or `--work-tree=~/stagedomain.com` to work (with `~` being set to the right path),
  • if you chose to git pull, unset GIT_DIR on the same line than the other commands like in your other question: `cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage`.
like image 98
VonC Avatar answered Oct 14 '22 08:10

VonC