Search code examples
next.jsvercel

ZEIT NOW variable secrets


I'm looking for a way to refer to a different set of secrets(env variables) depending on the value of another env variable.

i.e.
if (process.env.STAGE === "test") return config = process.env.TEST_CONFIG

I know that with automated github deployments there are included env variables

But how do I reference them correctly? For example:

const config = process.env.NOW_GITHUB_COMMIT_REF === 'master' ? prod : dev;

Doesn't work for me.

(NOW_GITHUB_COMMIT_REF: The branch that the app was deployed using.)


Solution

  • I just duplicate my answer here from Spectrum.

    Folder structure:

    config/
        dev.json
        test.json
        prod.json
        index.js
    api/
        search.js
    

    config/index.js

    const devConfig = require('../config/dev');
    const testConfig = require('../config/test');
    const prodConfig = require('../config/prod');
    
    const { NOW_GITHUB_COMMIT_REF } = process.env;
    if (NOW_GITHUB_COMMIT_REF === 'test') 
      return testConfig;
    else if (NOW_GITHUB_COMMIT_REF === 'master') 
      return prodConfig;
    else
      return devConfig;
    

    api/search.js

    const config = require('../config');
    

    In that case, master branch gets prod config, test branch - test config, all other branches - dev config.