Search code examples
node.jsherokucodeship

How to set some flags on my node build


When I build, test and deploy my node application from Codeship to Heroku I want to be able to set a release flag to true using a command line during the build. And in my code I want to do something like this....

if(config.release) load(liveConnection);
else load(debugConnection);

How can I achieve this? Is there some sort of package I install to run a build script which will transform my config file?


Solution

  • Instead of using a config file, you should use environment variables. For example:

    heroku config:set NODE_ENV=production
    

    Then, in node:

    if (process.env.NODE_ENV === 'production') load(etc);
    

    An even better way is to just provide connection information uniformly, through config files, like this:

    heroku config:set CONNECTION_STRING=foo
    

    Then in node:

    load(process.env.CONNECTION_STRING);
    

    That way, the environment is providing the config. Locally, you can start the app with a development string like CONNECTION_STRING=some_debug_string node server.js, or you can use a .env file to provide a whole set of them. More info here:

    http://12factor.net/config