Using Nuget package feed from PowerShell in Azure pipeline

Viewed 220

I'm trying to write a pipeline PowerShell task that checks that package version numbers in the csproj files have been incremented -- so that the push step doesn't fail because of duplicates when building new release packages. (We use allowPackageConflicts: false on the NuGetCommand@2 command: push task.)

The problem is to get Find-Package to use my Azure DevOps NuGet feed that all the packages go to.

- task: PowerShell@2
  displayName: 'Check package version numbers'
  inputs:
    targetType: 'inline'
    script: |
      [bool] $bad = $false;

      Get-PackageSource
      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
      Register-PackageSource -Name TheAzureDevOpsFeed -Trusted -Force -ProviderName NuGet -Location "https://pkgs.dev.azure.com/Organisation/_packaging/TheAzureDevOpsFeed/nuget/v3/index.json"
      Get-PackageSource
    
      foreach( $csproj in Get-ChildItem **\*.csproj ) {
          $m = Select-String -Path $csproj.FullName -Pattern '<VersionPrefix>([\d\.]+)</VersionPrefix>'
          $pkgName = [System.IO.Path]::GetFileNameWithoutExtension($csproj.FullName)
          echo $pkgName
          if( $m.Matches.Count -eq 1 )
          {
              $csprojVersion = $m.Matches[0].Groups[1].Value
              
              $pkg = $null
              try {
                  $pkg = Find-Package $pkgName -Source TheAzureDevOpsFeed -ErrorAction Stop
              }
              catch {
                  $pkg = $null
              }

              if( $pkg -eq $null )
              {
                  echo "  No existing package."
                  continue
              }
              $current = $pkg.Versions[0].OriginalVersion
    
              if( [System.Version]"$csprojVersion" -le [System.Version]"$current" )
              {
                  echo "  REJECTED: current version is $current but csproj has version $csprojVersion."
                  $bad = $true
              }
              else {
                echo "  $current -> $csprojVersion"
              }
          }
          else {
              echo "  skipped"
          }
      }
    
      if( $bad ) {
        exit 1
      }
      exit 0

When the task runs the output is:

Name                             ProviderName     IsTrusted  Location                                                  
----                             ------------     ---------  --------                                                  
nuget.org                        NuGet            False      https://api.nuget.org/v3/index.json                       
PSGallery                        PowerShellGet    False      https://www.powershellgallery.com/api/v2                  
Register-PackageSource : Source Location 
'https://pkgs.dev.azure.com/Organisation/_packaging/TheAzureDevOpsFeed/nuget/v3/index.json' is not valid.
At D:\a\_temp\62c6a3fb-93a6-4d4a-8cf5-ce5dc7d9dcbf.ps1:7 char:1
+ Register-PackageSource -Name TheAzureDevOpsFeed -Trusted -Force -Provi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (TheAzureDevOpsFeed:String) [Register-PackageSource], Exception
    + FullyQualifiedErrorId : SourceLocationNotValid,Microsoft.PowerShell.PackageManagement.Cmdlets.RegisterPackageSou 
   rce

But why is it 'not valid'? It is valid -- it works fine in VisualStudio.

Things I've tried:

  • single quotes, double quotes, no quotes,
  • [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12,
  • -Location https://pkgs.dev.azure.com/.../v2
1 Answers

I can reproduce your issue:

enter image description here

How I solve this:

$patToken = "<Your PAT here>" | ConvertTo-SecureString -AsPlainText -Force

$credsAzureDevopsServices = New-Object System.Management.Automation.PSCredential("<Your email address that related to PAT here>", $patToken)

Register-PackageSource -Name "xxx" -Location "https://pkgs.dev.azure.com/<organizaton name>/_packaging/<feed name>/nuget/v2" -ProviderName NuGet -Credential $credsAzureDevopsServices

Please refer to these official documents:

https://docs.microsoft.com/en-us/powershell/module/packagemanagement/register-packagesource?view=powershell-7.2

https://docs.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops#connecting-to-the-feed-as-a-powershell-repo

enter image description here

Let me know if you have more concerns.

Related