tags:

views:

61

answers:

3

I am working on a PHP website where I have just added a switch for what environment it is running in - development for when it is running on my local site, and production when it is running live on the web host:

<?php
define('ENV','development');
//or
define('ENV','production');

I have the site under VC with Mercurial, and usually simply deploy my site with hg push (the server runs hg too), however, with the addition of this switch, the "production" site will always differ from the "development" site in that the version deployed live will always be set to production instead of development.

This means my deployment process goes from

  1. Develop
  2. Test
  3. hg commit -m "Made changes"
  4. hg push
  5. ssh host hg update
  6. Go to 1.

to

  1. Develop
  2. Test
  3. hg commit -m "Made changes"
  4. Change development to production
  5. `hg commit -m "dev -> prod"
  6. hg push
  7. ssh host hg update
  8. (later:) Change production -> development
  9. hg commit -m "prod -> dev"
  10. Go to 1.

Which is obviously not great.

Is there some way to keep one insulated from the other, so that the live site will always be set to production and my local copy set to development?

+2  A: 

Instead of having a switch, have a development branch and a production branch.

hg up prodbranch 
hg merge -r devbranch
hg push
ssh yourserver hg update prodbranch
Paul Nathan
So in my production branch, I put code specific to it being live (like database configurations), and on my development branch, I keep code specific to it being local? And how does the merge across branches work? If I change code that affects both branches, will the merge catch that?
Austin Hyde
Yes it will, which also poses a high risk of merging the wrong thing. You should have a better strategy for keeping your production/development settings seperate than abusing your SCM.
Johannes Rudolph
@Austin: Um, I'd keep the configurations outside of the code repo. I'd have 1 configuration repo for production, and a 100% different configuration epo for development.
Paul Nathan
Why exactly is having unversioned code better than branches? I'm not trying to argue, just understand. I'm rather new to version control. Also, having unversioned files means I'd have to manually upload them to the host, correct? (no easy way to send them along with the `push`?)
Austin Hyde
@Austin: I didn't say that I'd have unversioned code. Branches are fine. What you are proposing is not an intended use of SCM. I'd keep the configuration files *in a separate repository* from the regular source.
Paul Nathan
@Paul: Ok, I guess that makes more sense.
Austin Hyde
Another question: If I'm keeping the configuration files in a separate repository, won't I be facing the same problem, just on a smaller scale - having different code under different environments in the same repository?
Austin Hyde
@Austin: No. I would have *3* repositories. One for production-only configuration, one for development only configuration, one for the actual code.Your configuration isn't *code*, is it?
Paul Nathan
@Paul: Well... most of it isn't. The configuration I'm worried about in this question is only a very small part of the site, only for a page or two, otherwise it would be pure configuration.
Austin Hyde
+3  A: 

If you have an Apache webserver, you could just set this in server/vhost/per-directory/.htaccess config:

SetEnv Deployment development

Or in production

SetEnv Deployment production

And in your script use:

define('ENV',$_ENV['Deployment'])

I assume (as it usually is) that the actual webserver / virtualhost config is outside the normal code.

http://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv

Wrikken
A: 

Have you thought about just not putting your conifguration file under version control?

middus
Well, the configured part of the site really only occupies a very small part, and is worked into the code, not in its own file. I thought about refactoring it out, but even if I did, that's not the point of the question (that's the specific example, and the main reason for asking, but not the underlying problem - having the same source altered just so slightly, yet with the same semantics)
Austin Hyde