azure-devopsnugetpipelinepublishartifact

Check/Fail Build pipeline if build pipeline artifact has already been published to artifact feed with same version number


I want to ensure that there is a warning or a failure in the build pipeline if the developer forgot to increment the version number in the packages they are developing.

Currently we have NuGet artifacts that are deployed with a version number in the build artifacts. I want to be able to check these against what has been deployed in the Artifact feed.

Build Pipeline artifacts example

Artifact Feed Example

Specifically I want to ensure that there is not FINAL release of a package (marked with the view "Release" in the artifact feed) that is deployed in the artifact feed if we are generating PRE RELEASES for it in the build pipeline. I want to ensure that any new developments have an incremented version number.

I attempted to use a task such as ArtifactDeploymentDetectorTask in conjunction with a powershell task but I was unsuccessful in getting that functioning.

Are there any tasks that would be well suited for this problem? Could this all be done using a powershell script?


Solution

  • Could this all be done using a powershell script?

    Yes. You can use Rest API: Artifact Details - Get Packages to list all version of the package.

    Then you can get the version of the nupkg file.

    Finally, you can loop all versions of the package and compare them with the current version of nupkg file. And then you can use logging command to set the error message of the pipleine.

    Here is the PowerShell example:

    $token = "PAT"
    
    $packagename= "pacakgename"
    
    $GetPackageversion="https://feeds.dev.azure.com/{Org}/{Project}/_apis/packaging/Feeds/{FeedId}/packages?packageNameQuery=$($packagename)&includeAllVersions=true&api-version=7.1-preview.1"
    
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    $response = Invoke-RestMethod -Uri $GetPackageversion -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json 
    
    
    $file=Get-Item "path\xxx.xxx.nupkg"
    
    
    
    $name= ($file.name) -split('.nupkg')
    
    $version = $name -split('pacakgename.')
    
    $count = 0
    foreach($versionobj in $response.value.versions)
    {
        $Artifactsversion = $versionobj.version
        echo $Artifactsversion
        if($Artifactsversion -eq $version)
        {
           $count = $count + 1
        }
    
    
    }
    
    echo $count
    
    if($count -eq 0)
    {
      echo "There is no same version"
    
    }
    
    else
    {
      echo "##vso[task.logissue type=error]The version is existing in the feed."
    }