Quickly Migrate NuGet Packages to a New Feed
Summary
This is a very simple bash script that can assist you in migrating NuGet packages to a different Artifact feed. It’s written with Azure DevOps in mind as a target, but there’s no reason why you couldn’t use any other artifact feed as a destination.
I used the script after I ran a NuGet restore locally of a Visual Studio solution, found my .NuGet
folder with all of the cached packages, placed my script in that folder, and ran it. This saved me a lot of time, migrating 102 packages while I grabbed a cup of coffee ☕️!
Note that this won’t work for migrating NuGet packages to GitHub Packages since the
<repository url="..." />
element in the.nuspec
file in the.nupkg
needs to be updated. See my other posts:
The Script
Copy the script below and save it as nuget-pusher.sh
in the folder where your .nupkg
files are located. Don’t forget to chmod +x nuget-pusher.sh
to make it executable!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
# Usage: ./nuget-pusher-script.sh <nuget-feed-name> <nuget-feed-source> <PAT>
if [ -z "$3" ]; then
echo "Usage: $0 <nuget-feed-name> <nuget-feed-source> <PAT>"
exit 1
fi
echo "..."
NUGET_FEED_NAME=$1
NUGET_FEED_SOURCE=$2
PAT=$3
# adding to ~/.config/NuGet/NuGet.config
dotnet nuget add source \
$NUGET_FEED_SOURCE \
--name $NUGET_FEED_NAME \
--username "az" \
--password $PAT \
--store-password-in-clear-text
results=$(find . -name "*.nupkg")
resultsArray=($results)
for pkg in "${resultsArray[@]}"
do
echo $pkg
dotnet nuget push --source \
$NUGET_FEED_NAME \
--api-key az \
$pkg
done
# clean up
dotnet nuget remove source $NUGET_FEED_NAME
echo "..."
According to the docs, any string will work for the
--api-key
parameter.
Running the Script
The script below can be called via:
1
./nuget-pusher-script.sh <nuget-feed-name> <nuget-feed-source> <PAT>
An example:
1
2
3
4
./nuget-pusher-script.sh \
azure-devops \
https://pkgs.dev.azure.com/jjohanning0798/_packaging/my-nuget-feed/nuget/v3/index.json \
xyz_my_pat
Bonus: Locating .nupkg Packages
How to find the location of .nupkg
files:
1
find / -name "*.nupkg" 2> /dev/null
How to find the location of .nupkg
files and copy them all to a directory:
1
find / -name "*.nupkg" -exec cp "{}" ./my-directory \; 2> /dev/null
The
2> /dev/null
is to suppress permission errors.
Improvement Ideas
- The
username
doesn’t matter in this script since when using an Azure DevOps PAT, username is irrelevant. If one was pushing to a NuGet feed that required username authentication, I would presume you would add that as an input. - One could also add a source folder as an input too instead of relying on current directory
- Also one could use a more elaborate input mechanism to the script…
- Use
dotnet nuget
instead ofnuget
command - Support other systems (such as
ubuntu
) with--store-password-in-clear-text
flag - Clean up temporary NuGet source when done