Miguel Moura
Miguel Moura

Reputation: 39484

Pass variable when publishing Asp.Net Core application

I have the following on an ASP.NET Core 2.2 project file:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build --prod" />
</Target>

When publishing the application, using dotnet publish, instead of using:

npm run build --prod

I need to be able to specify if I should run:

npm run build --prod.machine-1

or

npm run build --prod.machine-2

How can I do this?

Upvotes: 2

Views: 2043

Answers (1)

Gustavo Hidalgo
Gustavo Hidalgo

Reputation: 94

Instead of calling dotnet publish, you will need to call dotnet msbuild /t:publish. These two are essentially the same but the second one gives you access to all of MSBuild's command line arguments.

You can then set arbitrary properties in the call to msbuild using the /property (or /p for short) argument. You can then change your target to look like this:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  <PropertyGroup>
    <NPMMachine Condition=" '$(NPMMachine)' != '' ">--prod</NPMMachine>
  <PropertyGroup>
  <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build $(NPMMachine)" />
</Target>

This gives the target a default NPMMachine of --prod but allows you to override NPMMachine if necessary.

If you call dotnet msbuild /t:publish then the target runs with the --prod argument. If you want to override the npm argument, you can call msbuild with dotnet msbuild /t:publish /p:NPMMachine=--prod.machine-1.

Upvotes: 3

Related Questions