Home > Blockchain >  How to deploy/publish an ASP.NET Core React JS website with a particular .env file?
How to deploy/publish an ASP.NET Core React JS website with a particular .env file?

Time:12-05

I have a simple ASP.NET Core React JS web app. I've been publishing it directly to Azure just fine.

Recently I introduced a 2nd environment (prod vs dev).

How can I publish it to Prod using .env, and publish to Dev using .env.dev ?

Notes:

  • I build using VS Enterprise.
  • I deploy using VS Enterprise (right-click -> Publish).
  • I know I can update the 'scripts' section in packages.json. But I dont believe this script(s) are called when I do a Publish from VS IDE.

Perhaps there is a way to specify the script??

Ex: build:dev would build using .env.development, and build:prod would build using .env

Thanks

CodePudding user response:

You can use the following steps:

Create two environment files: .env for production and .env.dev for development.

In your package.json file, update the scripts section to include two new commands: build:dev and build:prod. These commands will be used to build your app using the appropriate environment file.

Here is an example of what your scripts section might look like:

"scripts": {
  "build:dev": "env-cmd -f .env.dev react-scripts build",
  "build:prod": "env-cmd -f .env react-scripts build",
  "start:dev": "env-cmd -f .env.dev react-scripts start",
  "start:prod": "env-cmd -f .env react-scripts start",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},

In your .csproj file, update the TargetFramework element to include the Publish target:

<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>

In your project, create a new file named publish.prod.cmd that contains the following commands:

npm run build:prod
dotnet publish

This file will be used to build your app using the production environment file and publish it to Azure.

In your project, create a new file named publish.dev.cmd that contains the following commands:

npm run build:dev
dotnet publish

This file will be used to build your app using the development environment file and publish it to Azure.

In Visual Studio, open the Publish window for your project. In the Build section, select the Custom option and enter the path to the appropriate publish script file (publish.prod.cmd for production and publish.dev.cmd for development).

Select the appropriate publish profile and click the Publish button to publish your app to Azure. The app will be built using the environment file specified in the selected publish script, and the resulting build will be published to Azure.

  • Related