dotnet / sdk-container-builds

Libraries and build tooling to create container images from .NET projects using MSBuild
https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
MIT License
179 stars 34 forks source link

Caching of dependencies in container builds #504

Closed amoszheng closed 1 year ago

amoszheng commented 1 year ago

We were looking for ways to adopt docker containers for our .NET Core web apps and discovered this native container publish feature which we found really awesome because that way we don't have to worry about our project references or maintaining Dockerfiles. However we have a question concerning caching of project dependencies in GitHub workflows to optimize build speed.

Before containerizing we would cache the ~/.nuget/packages directory (using the actions/cache action) so the dotnet build process can leverage packages downloaded from previous builds. Also when using a Dockerfile we can write it so that the dependencies layer is cached properly with the docker/build-push-action action. How can we accomplish the same thing with the built-in container publish functionality? Does caching the ~/.nuget/packages directory in the runner still work in this case or should we look for some other ways to do that?

Any help will be highly appreciated.

baronfel commented 1 year ago

Yes! The publish command to create the container would reuse whatever NuGet packages cache that you've set up - this is one of the big benefits of making container generation more 'native' to the tools. You also get to seamlessly reuse your NuGet authentication, which has historically been difficult for Dockerfile-based builds.