1

Is it possible (and if so, is it good practice) for a remote Git repository to deploy to multiple web servers (e.g., a staging server and production server) depending upon branch name? For example, if developer A pushes a commit to remote branch STAGING, the commit is deployed to the corresponding staging web server, whereas commits (or merges) to branch MASTER are deployed to the production server.

Does this illustrate a good/reliable workflow? If not, what is a better approach?

Git workflow example

reformed
  • 4,505
  • 11
  • 62
  • 88

1 Answers1

1

Yes, I have seen that done before (like in my previous answer from 2012)

To get the branch name, use:

branch=$(git rev-parse --symbolic --abbrev-ref $refname)

Then you can push to the right server

  if [ "master" == "$branch" ]; then
    git push production
    echo 'Changes pushed live.'
  fi

  if [ "staging" == "$branch" ]; then
    git push staging
    echo 'Changes pushed to staging.'
  fi

That assume two remotes each one referencing a remote bare repo.
The destination bare repo would have a post-receive hook with:

git --work-tree=/path/under/root/dir/site/ checkout -f $branch
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250