Search code examples
.netnugetazure-pipelinescsproj

Azure pipeline NuGet restore fails to set MsBuild properties in csproj files


I am trying to set up a pipeline for a C# project which uses the build configuration to decide which package to install. I can build just fine in Visual Studio, but using NuGetCommand@2 on Azure Pipelines fails to restore packages. It looks like when using that command, the property I created in the csproj file doesn't get set, so NuGet tries to find an invalid version of the package. Is there any way around this?

csproj:

...
  <PropertyGroup Condition="$(Configuration.Contains('2020'))">
    <RevitVersion>2020</RevitVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Autodesk.Revit.SDK" Version="$(RevitVersion).*" IncludeAssets="build; compile" />
  </ItemGroup>

pipeline:

...
variables:
  solution: '**/*.sln'
  projects: '**/*.csproj'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release 2020'

steps:
  - task: NuGetToolInstaller@1
    displayName: 'Install NuGet'
    inputs:
        versionSpec: 
  - task: NuGetCommand@2
    displayName: 'Restore packages'
    inputs:
        command: 'restore'
        restoreSolution: '$(solution)'
        configuration: $(buildConfiguration)
        feedsToUse: 'select'
        vstsFeed: '<private feed id>'

error:

error MSB4018: System.ArgumentException: '.*' is not a valid version string.


Solution

  • It looks like when using that command, the property I created in the csproj file doesn't get set, so NuGet tries to find an invalid version of the package. Is there any way around this?

    Local Nuget Restore command doesn't support Configuration switch.

    Online Nuget task supports Configuration switch, however only the nuget pack command supports this switch. Online Nuget task has four commands: Restore,Pack,Push,Custom, the Configuration input is only valid for nuget Pack. That's why nuget pack ignores configuration: $(buildConfiguration) even if you doesn't receive warning/error when you edit the yaml pipeline.

    The Configuration input of that task is only for nuget pack!

    enter image description here

    Similar discussions please check link1, link2, link3.

    Here's suggestion from Github/Nuget. You can try using Nuget.config file and PAT to provide auth for msbuild /t:restore.