How to automatically update NuGet packages to latest available version
Asked Answered
S

3

52

I have two repositories, and I need compiled libraries from one repository in the other. I don't want to manually check repo1 for updated libraries, and copy/commit to repo2, because that is stupid. I've got repo1 building NuGet packages on each build of the necessary libraries, and publishing them to an internal NuGet server. Projects in repo2 can then reference these NuGet packages, and everything is (almost) working.

The one last hurdle to this is automatically updating the NuGet packages in repo2's projects. Since I don't know when the libraries in repo1 will get updated (and I shouldn't have to), I would like some sort of build event on the projects in repo2 that will automatically update the NuGet packages. I currently just have a pre-build event doing it, but since packages.config files contain the version number of the installed package, I keep getting modified files in repo2 (the packages.config files get updated).

So my question is: what's a good way to automatically upgrade NuGet packages without mucking up my repo2 VCS? ScottGu says Here (in comments) that it's possible to hook package upgrades up to CI builds, but he doesn't specify how and my current solution is messy. Is there a built in way that I'm missing? Or any better work-arounds?

Senhorita answered 20/3, 2012 at 21:3 Comment(6)
Be careful. The libraries might change and break your software. blog.heroku.com/archives/2011/6/28/…Rootless
That's what our automated test suite is for :)Senhorita
@Senhorita did you ever get a solution in place? I'm facing the exact same issue.Heraclitus
Nope. We ended up skipping NuGet all together and doing it manually. However we're up to 4 different dependencies now, so we're going to set up all the NuGet stuff again and just manually build packages when things change.Senhorita
So what if... in one of the build steps there is a script to parse the version from a package feed (like this) and then modify your corresponding package version in package.config?Stav
learn.microsoft.com/en-us/nuget/reference/cli-reference/…Adnah
P
3

You could probably leverage the NuGet Package Restore feature (a bit of info here : http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages)

At project build, it calls "nuget.exe -install" to reinstall the packages from packages.config. I haven't tried it but you could add a Update command to the nuget.targets file in the same way. (You'd have to call both nuget.exe update and the existing nuget.exe install).

Padget answered 21/3, 2012 at 23:3 Comment(1)
I looked into that and that only restored packages to the solution level package folder (not to the actual project). I want to not check files into the project either, and neither nuget.exe update nor nuget.ext install get all files back into the project. Basically what I am looking for is this featureSenhorita
C
0

This explains how to do it via MSBuild

http://netitude.bc3tech.net/2014/11/28/auto-update-your-nuget-packages-at-build-time/

<Target Name="UpdatePackages" DependsOnTargets="CheckPrerequisites">
   <Exec Command="$(UpdateCommand)"
      Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

   <Exec Command="$(UpdateCommand)"
      LogStandardErrorAsError="true"
      Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>


<!-- Commands -->
<UpdateCommand>$(NuGetCommand) update "$(PackagesConfig)" -source "$(PackageSources)" -id AutoUpdater $(NonInteractiveSwitch)</UpdateCommand>
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>

<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>

<!-- We need to ensure packages are restored prior to assembly resolve -->
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
    RestorePackages;
    UpdatePackages;
    $(BuildDependsOn);
</BuildDependsOn>
Canso answered 8/12, 2017 at 22:33 Comment(0)
A
-1

You can modify your .cspoj file to execute a "BeforeBuild" target like this :

<Target Name="BeforeBuild">
  <Exec Command="&quot;$(SolutionDir).nuget\NuGet&quot; update &quot;$(ProjectDir)packages.config&quot; -Id your.package.id" />
</Target>

Note that : u'll need to have the "Nuget.exe" in ur solution directory

Abstention answered 14/4, 2015 at 6:46 Comment(1)
It’s not as simple as putting it in the pre-build steps. In fact that’s actually too late: #15027756Rapt

© 2022 - 2024 — McMap. All rights reserved.