How to automatically update NuGet packages to latest available version

Continuous IntegrationNugetNuget Package

Continuous Integration Problem Overview


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?

Continuous Integration Solutions


Solution 1 - Continuous Integration

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).

Solution 2 - Continuous Integration

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>

Solution 3 - Continuous Integration

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthemilkyninjaView Question on Stackoverflow
Solution 1 - Continuous IntegrationAlexandre DionView Answer on Stackoverflow
Solution 2 - Continuous IntegrationdynamiclynkView Answer on Stackoverflow
Solution 3 - Continuous IntegrationShafaet KarimView Answer on Stackoverflow