How to properly check NuGet package version for duplicate in build policy before push to private feed in Azure DevOps pull request

Viewed 755

I have a repo where is several libraries (csproj projects) which are published into a private NuGet feed hosted by Azure DevOps. They are consumed by other project in my company.

Current state

A push pipeline for each library which pushes NuGet into the private feed. These pipelines are triggered by

trigger:
  branches:
   include:
     - master
  paths:
      include:
      - MyCompany.Product.PackageName/*

During PR, another simple build pipeline that checks the build of the new package version. The PR can't be merged if this build pipeline fails. The new package version gets pushed into the feed when PR is merged into master.

The problem is that no version check in the build pipeline. During the PR, the developer doesn't have any information if the push pipeline would be successful or not.

I would like to do an automatic check if the package can be pushed into the private feed.

Desired state

When some developer opens a new PR, the build pipeline tells him/her that the package can't be pushed into the feed because (s)he forgot to update a package version.

My idea was create a project-specific build pipeline and employ PowerShell for some magic version check which fails if there is a duplicate in the feed.

What would be a proper strategy for this task? Does anybody applied some successful solution for this? Some pipeline examples would appreciated.

1 Answers

When you push a package version into the specified feed, if the version is duplicated with an existing version in the feed, normally the push will fail with an error for the duplication of the versions. Then you just need to changed the package version and try the push again.

If you want to check the package versions before the push, in the build pipeline you set to check the package versions, you can try like this:

  • Get the current version of the package you want to push. Generally you can read the version from the csproj file or the relate NuGet configuration files (such as .nuspec).

  • Use the 'nuget list' command to list all the existing versions of the specified package in the feed.

nuget list <PackageName> JSON -AllVersions -Source "https://pkgs.dev.azure.com/<Organization>/<Project>/_packaging/<FeedName>/nuget/v3/index.json"

If the Scope of the feed is Organization, omit '<Project>'.
To get the Source URL, open Azure Artifacts in your team project on DevOps, switch to the feed where the package is, click Connect to feed > NuGet.exe, then you can see the Source URL.
enter image description here

  • Check whether the package version you want to push is included in the listed package versions.
Related