NuGet package restore in builds and Visual Studio - azure-devops

We have a number of VS2013 solutions, with the "Enable NuGet package restore" option selected. Remembering back, selecting this option creates a ".nuget" solution folder containing a number of files (NuGet.config, NuGet.exe, NuGet.targets). As a final step, I would edit the NuGet.targets file to add a package source for our in-house repo.
I've just upgraded our old TFS2012 server to Azure DevOps 2019 and am currently creating new build pipelines to replace our old XAML-based build definitions. I've added a "NuGet restore" task, but I'm not sure if this is necessary given that our solutions have the "package restore" option enabled? If I remove this task, will the packages be restored by the "Visual Studio build" task instead?
I never really liked having all that package restore stuff in the solution. If I was to remove it, I guess the "NuGet restore" build task would be needed after all? But without the package restore option, how would a developer restore the packages on their PC (in VS2019)?

The best practice is to make two tasks in the build pipeline - restore and build.
The option "Enable NuGet package restore" does not exist anymore from VS2015, so how the developer restore the packages? just right-click on the solution and "Restore NuGet Packages:
In fact, VS should do it automatically when you build, if not check this setting (under Tools - Options):
You need to add your private NuGet feed to the NuGet sources (Tools - Options):

Related

Azure DevOps keep NLog configuration on Release, when Nuget Packaging updates

I am trying to keep my NLog configurations, when I do a release from Azure Devops. The Nuget Package portion overwrites my NLog configurations and sets it back to default. I would like to know how to preserve my configurations when I release my build, but keep the Nuget Packages update to date for the other packages.
You need to remove the NLog.Config package for that.
It's documented here: https://www.nuget.org/packages/NLog.Config
Note: Unfortunately this package won't work well when using
Advised to:
download manually: https://raw.githubusercontent.com/NLog/NLog/dev/src/NuGet/NLog.Config/content/NLog.config
set "Copy To Output Directory" to "Copy if newer"

Nuget packages included in artifacts automatically

We have a TeamCity build server and for a time were had the internal Nuget server enabled. We'd generate Nuget packages, which it'd automatically publish to the nuget feed.
We've since moved to another Nuget server and disabled the TC internal Nuget server; additionally we started building Nuget packages using OctoPack in some configurations which never built them before. The problem is that these Nuget packages seem to automatically be added to the build configuration's Artifacts. This is a waste, since the same packages are being published to the external (to TeamCity) Nuget server.
Is there a setting which automatically will find and include Nuget packages in TeamCity's artifacts, or is it just doing this on its own? I cant even explicitly exclude the packages in the configuration's artifacts setting, they get included regardless.
TeamCity is version 2018.1.2.
New packages in built-in TeamCity NuGet feeds could be added via the followings ways.
So to avoid publishing produced nuget packages as a build artifacts inspect:
Artifact paths settings in your build configuration
NuGet Pack build steps settings: uncheck "Publish created packages to build artifacts" option
NuGet Publish build steps settings: don't specify TeamCity built-in NuGet feed URLs in "Package source" field.

TeamCity can't find Nuget

I have a build set up on Team City but can't seem to get NuGet to restore the packages. My build configuration has two steps: Restore Nuget, then Build the solution. I have set up NuGet on TeamCity, so when I create a NuGet build step, I can choose a NuGet version from the drop down:
But when I build, it cant find NuGet:
Failed to find NuGet executable NuGet.CommandLine.3.3.0.nupkg. Check the version is listed in NuGet server settings tab.
Why cant it find NuGet?
UPDATE

Automatic Nuget package restore in Visual Studio Online fails

I've verified that it works locally by deleting all of the packages from the packages folder, opening up visual studio and then building my project - all of the packages are downloaded first and then it builds.
I do not have a .nuget folder and in the visual studio options I've allowed Nuget to download missing packages.
When building using TFS I get a ton of errors that I do not get when building locally. I believe this to be related to
Thoughts?
I had the same problem when building a new project with Visual Studio Online and found this solution :
In VS enable Nuget Package Restore (rt click on solution in solution explorer)
Remove solution /Packages folder from both local workspace and Visual Studio Online Version control. (Thanks Robert)
Confirm build works locally
Check-in changes: 3 Nuget files & modified .sln and .csproj
trigger build
NuGet should restore the local packages when you build and will also restore after VSO build. Version control will not have a packages folder, but the required packages end up on the release server. I trigger a release manager with each build and deploy with DSC.
NuGet documentation ( 1 & 2 )doesn't explain how that works and I've had to 'toggle' this on & off once to get builds to work after. I've emailed support#nuget.org to ask for more detail and clearer documentation.
You should not have to checkin packages to source control. But you'll need to help MSBuild restoring that.
Check this link: http://docs.nuget.org/docs/reference/package-restore-with-team-build
The trick is the MSBuild .proj file you have to create and reference there your solutions.
Nuget 2.7 is required

NuGet package restore *without* changing every project in the solution to import the NuGet.targets MsBuild task

NuGet Package Restore seems to be the correct way to combine NuGet with source-control (in my case TFS), for example in this answer and in the first comment to this closed question. NuGet Package Restore allows a solution fetched from source-control and built on another dev' machine to automatically fetch the required NuGet packages.
To add NuGet Package Restore to a solution you right click on the solution and then select Enable NuGet Package Restore. That "added a solution folder named .nuget containing NuGet.exe and a NuGet.targets MsBuild file. It also changed every project in the solution to import the NuGet.targets MsBuild task." (Quote from http://docs.nuget.org/docs/Workflows/Using-NuGet-without-committing-packages). But some of the projects in the solution I am working on are utility projects, shared between different solutions and between different developers and they do not require references handled by NuGet.
How do I enable NuGet Package Restore but preclude certain projects in the solution from the NuGet build task set-up?
At the moment, NuGet Package Restore has some limitations and can't be restricted to specific projects.
There's an existing NuGet work item that would make this possible :
#1812 - Enable Package Restore - Selective Projects
Please comment/vote on it to bump the priority since it's currently backlogged.
Note: at the surface, .csproj files appears to have a property to support turning off NuGet Package Restore, but due to another issue, it keeps turning back on : NuGet command line forces package restore
You can run a script from the PreBuild step of the first project to be built that walks through all packages.config files and downloads their contents. You need to call
.nuget/nuget.exe install "....\packages.config" -o "packages"
from your $(SolutionDir) for every packages.config in your solution.
This will actually work better than the standard solution, since it'll also download solution-level packages, that are not installed into a project.
On my buildserver I use this (shell) snippet:
find -iname packages.config -print0 | xargs -0 -ti mono --runtime=v4.0.30319 .nuget/nuget.exe install {} -o "packages"