Search code examples
azure-pipelinesnuget

Azure Pipelines - How to get latest version of a published NuGet package from a feed


I have a pipeline that builds a package and publishes it to a private feed. I then want to extract the version of the package and use it in other steps in the pipeline.

Is there a way to get the version of the published package or maybe some way of getting the version by somehow inspecting the nupkg I just built?


Solution

  • How to get latest version of a published NuGet package from a feed

    Based on your description, you can use the Rest API: Artifact Details - Get Package Versions to get the latest version of the nuget package in the feed. Then we can use logging command to set variable to pass the version number to next tasks.

    Here is an example:

    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
    
          $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$(system.accesstoken)"))
          
          $url = 'https://feeds.dev.azure.com/{Organization}/{ProjectName}/_apis/packaging/Feeds/{feedname}/Packages/{PackageID}/versions?api-version=6.0-preview.1'
          
          $PackageInfo = (Invoke-RestMethod -Uri $url -Method Get  -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 
          
          $LatestVersion= $PackageInfo.value.version | Select-Object -first 1
          
          
          Write-Host "Latest package Version = $LatestVersion"
          echo "##vso[task.setvariable variable=LatestVersion;]$LatestVersion"
    

    We can get the PackageID with the Rest API: Artifact Details - Get Packages.

    GET https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/Feeds/{feedId}/packages?api-version=7.1-preview.1
    

    Result:

    enter image description here

    maybe some way of getting the version by somehow inspecting the nupkg I just built?

    Yes. This is also can be achieved.

    Here is an example:

    steps:
    - task: NuGetCommand@2
      displayName: 'NuGet restore'
    
    - task: NuGetCommand@2
      displayName: 'NuGet pack'
      inputs:
        command: pack
        packDestination: '$(Build.ArtifactStagingDirectory)'
    
    - task: NuGetCommand@2
      displayName: 'NuGet push'
      inputs:
        command: push
       
    
    
    - powershell: |
       $name = Get-ChildItem -Path "$(Build.ArtifactStagingDirectory)" -Filter *.nupkg -Recurse -ErrorAction SilentlyContinue -Force
       
       $nupkgname = $name.name
       
       echo $nupkgname
       
       if ($nupkgname -match '(?<=(?<id>.+))\.(?<version>((\d+\.\d+(\.\d+)?))(?<suffix>(-.*)?))\.nupkg')
       {
       
           $packageVersion = $Matches.version
           echo $packageVersion
           echo "##vso[task.setvariable variable=LatestVersion;]$packageVersion"
       }
      displayName: 'Get Latest nupkg version'
    

    In this case, the PowerShell script will search for the .nupkg file and get the version number based on the file name.

    Result:

    enter image description here

    Note: You need to make sure that the packDestination (nuget pack task) and Get-ChildItem command (PowerShell task) are using the same target folder path.