Just another programming blog

Azure, HowTo, Tips&Tricks

How to easily migrate old packages from self-hosted NuGet server to Azure Artifacts

Introduction

In the project I am currently working in we have decided to move from our self-hosted (in Azure App Service) NuGet server to use Azure DevOps Artifacts, as we have been moving more and more towards utilizing its capabilities. It should reduce our costs and add some features we have been missing like a reasonable UI to manage the packages. In this short article I will share with you how we have managed to migrate all our old packages to Azure Artifacts to make the transition easier.

// Note: this article assumes you are using Windows environment.

How?

Download nuget.exe tool

In order to migrate the packages you need a CLI tool named nuget.exe. You can download latest recommended version from here.

Directories structure

I have organized my directories like below:

  • Packages is a directory containing *.nupkg files. I have downloaded them all from my server via KUDU (literally just grabbed it as a ZIP file with all files from the directory).
  • migrate.ps1 is powershell script with the content presented later in the article.
  • nuget.exe is the CLI tool you probably have already downloaded by this point.

Scripts

NuGet source

Firstly, we need to specify our Azure Artifacts NuGet feed. To do that navigate to the root directory of your project (in my case it's: C:\Projects\NugetMigration) and run the following command:

.\nuget.exe sources add -Name "MyFeedName" -Source "MyFeedUrl"

where:

  • MyFeedName should be the name of the NuGet source to be used in the script. I have named used the same name as the name of my Azure Artifacts feed name (something like in example: AzureArtifactsNuGet) to be clear where do I push the package to (taken from 1 in the picture below).
  • MyFeedUrl should be the URL of the index.json from Azure Artifacts (taken from 2 in the picture below).

// Note: to get to the page displayed in the picture go to your Azure Artifacts and hit Connect to feed button.

Packages migration script

The content of the script named migrate.ps1 should be as follows:

param ($SourceName)

$packages = Get-ChildItem -Path .\Packages -Recurse -ErrorAction SilentlyContinue -Force `
            | %{$_.FullName} `
            | where {$_ -like "*.nupkg" -and $_ -notlike "*symbols*"}

Foreach ($package in $packages) {
    .\nuget.exe push -Source $SourceName -SkipDuplicate -ApiKey az $package
}

After saving the content, run the script with the following parameter:

.\migrate.ps1 "MyFeedName"

For the first push, you'll be promped to log in to your Azure Active Directory account associated with Azure DevOps. After that, you should then see the script iterating through the all packages and pushing them one by one to your newly created Azure Artifacts feed.

Summary

Microsoft is providing us with its own migration tool, but it is supporting only MyGet hosting. In order to achieve the same with self-hosted NuGet server, the solution presented in this article should be suitable for you. Actually it should work for every NuGet migration, no matter where from, as long as you have your *.nupkg files exported into Packages directory. I hope it will be useful! 馃檪

7 Comments

  1. Paul

    Hello,
    Firstly, apologies for posting in this article- I was not sure how to contact otherwise.
    I believe you programmed Cheese Terminator Reloaded back in 2015-2016. I thought you might like to see it mentioned in this recent Cheese Terminator video:
    https://www.youtube.com/watch?v=KUOKIpKcbG8
    If you look in the comments, you can see we were trying to play the Cheese Terminator Reloaded app but unfortunately can’t get it to run so far. Maybe one day we’ll get the cheese…
    Best Regards

    • Comment by post author

      Dawid Chr贸艣cielski

      Hey!

      First of all, great job finding me as I indeed am the developer behind this game (created it in 2015 for Microsoft back when I was still a student).

      What’s even better – I was able to find the codebase, ran it on my machine and reached out to my friend from Microsoft if I could get a permission to republish/redistribute it somehow – waiting for the response from Microsoft.

      Thanks for reaching out and I’ll keep you updated 馃檪

  2. Paul

    That’s very cool to hear!
    If possible that would be really awesome. 馃榾
    I’ll stay tuned to any news…

  3. Paul

    Hello,
    We are still trying to play the game but still no luck 馃檨
    Do you have any news about it or it is cancelled?

  4. RB

    Works, thank you Dawid! Ran this after adding the source feed to suppress prompts for credentials

    .\nuget.exe sources update -Name “MyFeedName” -username -password

Leave a Reply