C
C#2y ago
no >> body

✅ Iteration through a list in csproj

I have a csproj file with a next section
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish" Condition="'$(BuildingDocker)' == ''">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn install" />

<!-- Iterate over each environment in the Environments variable and run yarn run build:<ENVIRONMENT> -->
<ItemGroup>
<Environment Include="$(Environments)" />
</ItemGroup>
<Message Importance="high" Text="Started building %(Environment.Identity) versions of the spa ..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:%(Environment.Identity)" />

<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)%(Environment.Identity)\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish" Condition="'$(BuildingDocker)' == ''">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn install" />

<!-- Iterate over each environment in the Environments variable and run yarn run build:<ENVIRONMENT> -->
<ItemGroup>
<Environment Include="$(Environments)" />
</ItemGroup>
<Message Importance="high" Text="Started building %(Environment.Identity) versions of the spa ..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:%(Environment.Identity)" />

<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)%(Environment.Identity)\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
This is how I pass environments dotnet publish --configuration Release -p:Environments=sandbox;dev;qa;staging It works correctly only for the first value from environments. On the second iteration it shows me an error
dev: The term 'dev' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
qa: The term 'qa' is not recognized as a name of a cmdlet, function, script file, or executable program.
.....
dev: The term 'dev' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
qa: The term 'qa' is not recognized as a name of a cmdlet, function, script file, or executable program.
.....
I'm wondering how to make it work for the rest of the environments? My package.json file contains all env-specific commands
12 Replies
no >> body
no >> bodyOP2y ago
Based on the error message, I can assume that it tries to run yarn run build:sandbox as a first command. Everything goes as expected with this command. All further commands are: dev qa staging (without yarn run build:) And they are causing these errors. Any idea why this happens?
no >> body
no >> bodyOP2y ago
The whole log is here https://pastebin.com/Tzqv8jK2
Pastebin
error message - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Anton
Anton2y ago
if you can help it, move your build tasks away from msbuild Nuke is probably your best option
no >> body
no >> bodyOP2y ago
It would be overkill for this project and task. I can do something like this
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish" Condition="'$(BuildingDocker)' == ''">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn install" />
<Message Importance="high" Text="Started building version of the spa ..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:sandbox" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:dev" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:qa" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:staging" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:production" />

<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)sandbox\**" />
<DistFiles Include="$(SpaRoot)dev\**" />
<DistFiles Include="$(SpaRoot)qa\**" />
<DistFiles Include="$(SpaRoot)staging\**" />
<DistFiles Include="$(SpaRoot)production\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish" Condition="'$(BuildingDocker)' == ''">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn install" />
<Message Importance="high" Text="Started building version of the spa ..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:sandbox" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:dev" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:qa" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:staging" />
<Exec WorkingDirectory="$(SpaRoot)" Command="yarn run build:production" />

<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)sandbox\**" />
<DistFiles Include="$(SpaRoot)dev\**" />
<DistFiles Include="$(SpaRoot)qa\**" />
<DistFiles Include="$(SpaRoot)staging\**" />
<DistFiles Include="$(SpaRoot)production\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
But I want to keep environments configurable I'm using a powershell
dotnet publish --configuration Release "-p:Environments=sandbox;dev;qa;staging"
MSBuild version 17.4.0+18d5aef85 for .NET
MSBUILD : error MSB1006: Property is not valid.
Switch: dev
dotnet publish --configuration Release "-p:Environments=sandbox;dev;qa;staging"
MSBuild version 17.4.0+18d5aef85 for .NET
MSBUILD : error MSB1006: Property is not valid.
Switch: dev
no >> body
no >> bodyOP2y ago
Holy cow
no >> body
no >> bodyOP2y ago
It's alive Thank you @Retax, now it works
Anton
Anton2y ago
Once you have any logic in the build, I'd say Nuke is worth it you've got quite a bunch already, it seems trying to do things like this in MSBuild is just a tremendous waste of time, it doesn't report errors well and it's impossible to debug
no >> body
no >> bodyOP2y ago
I believe every word you say, but this task should have been done yesterday, so there is no time for that, unfortunately. Oh shit, here we go again... Now I'm trying to run this script in my Azure DevOps pipeline and I'm getting the same error.
- task: PowerShell@2
displayName: Restore and publish project
inputs:
targetType: inline
workingDirectory: $(startProjectPath)
script: |
dotnet restore
dotnet publish -c $(releaseBuildConfiguration) '-p:Environments="sandbox;dev;qa;staging;production"' -o $(Build.ArtifactStagingDirectory)/$(startProjectArtifactName)
pwsh: true
- task: PowerShell@2
displayName: Restore and publish project
inputs:
targetType: inline
workingDirectory: $(startProjectPath)
script: |
dotnet restore
dotnet publish -c $(releaseBuildConfiguration) '-p:Environments="sandbox;dev;qa;staging;production"' -o $(Build.ArtifactStagingDirectory)/$(startProjectArtifactName)
pwsh: true
It looks like the problem is still the same.
Starting: Restore and publish project
==============================================================================
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.212.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/a4a8592b-19f4-4825-ab8a-1b54a9aecc72.ps1'
Determining projects to restore...
Restored /home/vsts/work/1/s/src/Company.Start/Company.Start.csproj (in 7.05 sec).
Microsoft (R) Build Engine version 16.7.3+2f374e28e for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1006: Property is not valid.
Switch: dev

For switch syntax, type "MSBuild -help"
##[error]PowerShell exited with code '1'.
Finishing: Restore and publish project
Starting: Restore and publish project
==============================================================================
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.212.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/a4a8592b-19f4-4825-ab8a-1b54a9aecc72.ps1'
Determining projects to restore...
Restored /home/vsts/work/1/s/src/Company.Start/Company.Start.csproj (in 7.05 sec).
Microsoft (R) Build Engine version 16.7.3+2f374e28e for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1006: Property is not valid.
Switch: dev

For switch syntax, type "MSBuild -help"
##[error]PowerShell exited with code '1'.
Finishing: Restore and publish project
Anton
Anton2y ago
maybe quote the whole command idk in the yaml file it might be getting unescaped after having been parsed
no >> body
no >> bodyOP2y ago
Do you mean something like this?
`'-p:Environments="sandbox;dev;qa;staging;production"'`
`'-p:Environments="sandbox;dev;qa;staging;production"'`
I'm thinking to try bash instead of PowerShell in the pipeline My bash task:
- task: Bash@3
displayName: Restore and publish project (bash)
inputs:
targetType: inline
script: |
dotnet restore
dotnet publish -c $(releaseBuildConfiguration) -p:Environments="sandbox;dev;qa;staging;production" -o $(Build.ArtifactStagingDirectory)/$(startProjectArtifactName)
- task: Bash@3
displayName: Restore and publish project (bash)
inputs:
targetType: inline
script: |
dotnet restore
dotnet publish -c $(releaseBuildConfiguration) -p:Environments="sandbox;dev;qa;staging;production" -o $(Build.ArtifactStagingDirectory)/$(startProjectArtifactName)
The output is the same.
Microsoft (R) Build Engine version 16.7.3+2f374e28e for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1006: Property is not valid.
Switch: dev

For switch syntax, type "MSBuild -help"
##[error]Bash exited with code '1'.
Finishing: Restore and publish project (bash)
Microsoft (R) Build Engine version 16.7.3+2f374e28e for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1006: Property is not valid.
Switch: dev

For switch syntax, type "MSBuild -help"
##[error]Bash exited with code '1'.
Finishing: Restore and publish project (bash)
So, I've played around with bash script and found that this version is working
- task: Bash@3
displayName: Restore and publish project (bash)
inputs:
targetType: inline
script: |
dotnet restore
dotnet publish -c $(releaseBuildConfiguration) -p:Environments=\"sandbox\;dev\;qa\;staging\" -o $(Build.ArtifactStagingDirectory)/$(startProjectArtifactName)
- task: Bash@3
displayName: Restore and publish project (bash)
inputs:
targetType: inline
script: |
dotnet restore
dotnet publish -c $(releaseBuildConfiguration) -p:Environments=\"sandbox\;dev\;qa\;staging\" -o $(Build.ArtifactStagingDirectory)/$(startProjectArtifactName)
The interesting thing is that in the end of the execution I'm getting an error.
Done in 34.66s.
Company.Start -> /home/vsts/work/1/a/start/
##[error]Bash exited with code '1'.
Finishing: Restore and publish project (bash)
Done in 34.66s.
Company.Start -> /home/vsts/work/1/a/start/
##[error]Bash exited with code '1'.
Finishing: Restore and publish project (bash)
Any idea why is this happening?
Anton
Anton2y ago
quirks you could've integrated Nuke 5 times over at this point lol
no >> body
no >> bodyOP2y ago
Sorry, but it's still overkill in my case. The problem was that I forgot about specifying the working directory.
Want results from more Discord servers?
Add your server