Update NuGet packages in multiple repositories

On my current project we are struggling of updating multiple repositories after we are pushing a new version of our custom NuGet packages. A solution could be to use floating versions but I wanted more control over the versions, because there could be cases where I need different versions on each solutions. Therefore I went in the direction to update the versions manually on a developer machine. This way, pull requests needs to be opened for each package updates, but this is great because I want all developers aware of the changes.

The scripts below are made to automate this workflow. The following actions should run in order:

  1. .\create-branches.ps1 -repos .\project-1,.\project-2 -branch feature/nuget-updates – this script checks out the master branch and creating and pushing a new branch with the given name
  2. .\upgrade-packages.ps1 -repos .\project-1,.\project-2 – to run this snippet, first you need install dotnet-outdated-tool. This one jumps into each given repository directory and updating all package which starts with my.custom.packages. After the update, it also runs a build to able detect build errors early.
  3. .\push-branches.ps1 -repos .\project-1,.\project-2 -commit "Update packages" – the last one, it commits and pushes all changes in all given repositories.

Automation of creating nuget package updates in multiple repos

Use the commands in the following order

  1. create-branches.ps1
  2. upgrade-packages.ps1
  3. push-branches.ps1
##
## Be careful, because it's doing a git clean and git reset for all given repos!
## How to use: .\create-branches.ps1 -repos .\project-1,.\project-2 -branch feature/nuget-updates
##
param ([string[]] $repos, [string] $branch)
foreach ($repo in $repos) {
Write-Host "[Create-Branches] Creating branch for" $repo -ForegroundColor "Green"
Set-Location -Path $repo
git checkout master
git clean -fd
git reset --hard
git pull
git checkout -B $branch
git push -u origin $branch
cd..
}
##
## Prerequisite: dotnet tool install --global dotnet-outdated-tool
## How to use: .\upgrade-packages.ps1 -repos .\project-1,.\project-2
##
param ([string[]] $repos)
foreach ($repo in $repos) {
Write-Host "[Upgrade-Packages] Start to upgrade packages for" $repo -ForegroundColor "Green"
Set-Location -Path $repo
Write-Host "[Upgrade-Packages] Upgrade packages for" $repo -ForegroundColor "Green"
dotnet outdated --include my.custom.packages -u
Write-Host "[Upgrade-Packages] Build solution for" $repo -ForegroundColor "Green"
dotnet build
cd..
}
##
## Be careful, because it's commiting and pushing to all given repos!
## How to use: .\push-branches.ps1 -repos .\project-1,.\project-2 -commit "Update packages"
##
param ([string[]] $repos, [string] $commit)
foreach ($repo in $repos) {
Write-Host "[Push-Branches] Commit changes branch for" $repo -ForegroundColor "Green"
Set-Location -Path $repo
git add .
git commit -m $commit
git push
cd..
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s