在 Azure 管道中使用来自 PowerShell 的 Nuget 包提要

Using Nuget package feed from PowerShell in Azure pipeline

我正在尝试编写一个管道 PowerShell 任务来检查 csproj 文件中的包版本号是否已递增 - 以便在构建新发布包时推送步骤不会因为重复而失败。 (我们在 NuGetCommand@2 command: push 任务上使用 allowPackageConflicts: false。)

问题是让 Find-Package 使用我的 Azure DevOps NuGet 提要,所有包都转到那里。

- 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

任务运行时输出为:

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\_tempc6a3fb-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

但是为什么是'not valid'?它是有效的——它在 VisualStudio 中工作正常。

我尝试过的事情:

我可以重现你的问题:

我是如何解决这个问题的:

$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

请参考这些官方文档:

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

如果您有更多疑虑,请告诉我。