Im setting up a Azure devops build pipeline for a .NET core 2.2 web app that includes Angular and one of the steps it runs is dotnet publish. However, the end result is not what i was expecting compared to when running a publish directly from VS 2017.
As a way to run custom npm commands to target specific environments. So in my csproj file I have this
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<!-- Use conditional builds based on build target setting eg. debug, dev, prod etc -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build " Condition=" '$(Configuration)' == 'Debug' " />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build --prod false --configuration=dev" Condition=" '$(Configuration)' == 'Test' " />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build --prod true --configuration=prod" Condition=" '$(Configuration)' == 'Release' " />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr --configuration=prod" Condition=" '$(BuildServerSideRenderer)' == 'true' And '$(Configuration)' == 'Release' " />
However, the devops build was not running the correct command. After looking at the build log, it was simply running ng build, not including the extra flags to target a specific config.
So then to confirm this, I ran at a command line
dotnet publish -c Test
, and sure enough, the output indicated it ran ng build, without seemingly using what was in the csproj file.
How then can I get my npm command to take the configuration values like those in the csproj file but when dotnet publish runs?
Instead of trying to get parameters working through MSBuild, I'd recommend moving your npm commands into package.json like this.
"scripts": {
"buildTest": "npm run build --prod false --configuration=dev",
"buildProd": "npm run build --prod true --configuration=prod"
}
And then use csproj to just run npm run buildTest
and npm run buildProd
and so on.