2
votes

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?

1
Question: any reason you cannot explicitly use npm tasks and keep these out of csproj altogether? I'd highly recommend that to get full control over the commands. If you really have to do it through csproj, I'd create different commands in package.json and call them "buildTest", "buildProd", etc. And just keep the Command part of the csproj to "npm run buildTest", "npm run buildProd". That way, you don't have to worry about the build system dropping the arguments. - Hiral Desai
yes, i was just considering that. im trying that now....and it works! awesome! move your reply to be the answer, thanks - bitshift
Awesome, added the recommendation as answer now. - Hiral Desai

1 Answers

2
votes

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.