11

I am using ASP.NET Core MVC 6 using Visual Studio 2015. In my gulpfile.js script I want to know if the hosting environment is Development, Staging or Production so that I can add or remove source maps (.map files) and do other things. Is this possible?

UPDATE

Relevant issue on GitHub.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

2 Answers2

6

You can use the ASPNETCORE_ENVIRONMENT (Was formerly ASPNET_ENV in RC1) environment variable to get the environment. This can be done in your gulpfile using process.env.ASPNETCORE_ENVIRONMENT.

If the environment variable does not exist, you can fallback to reading the launchSettings.json file which Visual Studio uses to start your application. If that also does not exist, then fallback to using the Development environment.

I wrote the following JavaScript object to make dealing with the environment in gulpfile.js easier. You can find the full gulpfile.js source code here.

// Read the launchSettings.json file into the launch variable.
var launch = require('./Properties/launchSettings.json');

// Holds information about the hosting environment.
var environment = {
    // The names of the different environments.
    development: "Development",
    staging: "Staging",
    production: "Production",
    // Gets the current hosting environment the application is running under.
    current: function () { 
        return process.env.ASPNETCORE_ENVIRONMENT ||
            (launch && launch.profiles['IIS Express'].environmentVariables.ASPNETCORE_ENVIRONMENT) ||
            this.development;
    },
    // Are we running under the development environment.
    isDevelopment: function () { return this.current() === this.development; },
    // Are we running under the staging environment.
    isStaging: function () { return this.current() === this.staging; },
    // Are we running under the production environment.
    isProduction: function () { return this.current() === this.production; }
};

See this answer for how to set the environment variable.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • This is null in RC1 and thus doesn't work and still doesn't give us our current configuration. – James Hancock Dec 17 '15 at 15:17
  • You need to actually set the ASPNET_ENV variable in your Windows/Linux/Mac OS. See this: https://stackoverflow.com/questions/32301840/how-to-set-asp-net-5-environment-variables-on-production-environment/32326666#32326666 – Muhammad Rehan Saeed Dec 17 '15 at 15:21
  • Note also that this environment variable name is changing in RC2. – Muhammad Rehan Saeed Dec 17 '15 at 15:24
  • Note that your answer requires me to set the environment variable before deploy, but because the environment variables in VS.net are set once for the project and you can't set it per publish target or build configuration, you can't possibly do what's needed in the question which is a major problem because you can't deploy angular with the right path to your API as it currently stands in an automated way rewritting (using gulp.replace or anything else) the api Uri. – James Hancock Jan 11 '16 at 16:17
  • Firstly, if you don't set the environment variable, it falls back to 'Development'. Secondly, you seem to be talking about setting the environment variable in the project settings, this setting is just for debugging, in fact you should be setting them like so: https://stackoverflow.com/questions/32301840/how-to-set-asp-net-5-environment-variables-on-production-environment/32326666#32326666. – Muhammad Rehan Saeed Jan 12 '16 at 08:22
  • You're missing the point: I need to, at publish, know the publishing target or the build configuration so that I can process my javascript files. THUS: Either I need to be able to get the publish target in gulp or the build config, or get a environment variable that is dependant upon the build configuration. – James Hancock Jan 12 '16 at 18:38
  • And we're talking about gulp here. We're not talking about executing code on a server. Thus it will always be in VS.net or on your build server that it's executing and thus you need to know the build configuration that is executing gulp. – James Hancock Jan 12 '16 at 18:39
  • 1
    Where is this `process` variable coming from... I don't see it defined in my `gulpfile.js` – Serj Sagan Mar 24 '16 at 20:18
1

You would need to set the NODE_ENV environment variable in each environment and then in your gulpfile, read it in using process.env.NODE_ENV.

Have a look at https://stackoverflow.com/a/16979503/672859 for additional details.

Community
  • 1
  • 1
Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68
  • So it looks like you must set a node environment and ASP.NET 5 environment to get this working. Is it possible to set the node environment based on the ASP.NET 5 hosting environment? – Muhammad Rehan Saeed Jul 05 '15 at 11:35
  • 1
    I don't know about Visual Studio since I've never used it, but it depends how you run your gulp tasks. Does VS have some kind of embedded node environment where your gulp tasks run? If so, I'd look in the preferences. If your gulp tasks are merely run from the command line, you'd have to set your OS level environment variables. – Patrick Grimard Jul 05 '15 at 12:30
  • The issue is that I don't want environment or at least I want to be able to set environment variables per build configuration (DEBUG, RELEASE etc.) so that when I build for our demo environment, I know it's going to the demo environment and the API's url is set accordingly in our Angular app. Right now I have no way of deploying to a specific environment and before deploy rewritting configuration that is readable in javascript in a web browser. I have to use mvc for no reason other than to contextually set it from web.config or otherwise. – James Hancock Jan 11 '16 at 16:15