Search code examples
artifactorychocolateychoco

How to create local cache for choco package manager


I am building a windows docker image and use a package manager called choco for automating package installation. I would like to avoid downloading packages from the internet every time I modify and rebuild the dockerfile. Locally we run an Artifactory server that purpose But the package I am installing has hardcoded url value which is pointing to microsoft.com

In the package installation script here https://community.chocolatey.org/packages/visualcpp-build-tools#files

$packageName = 'visualcpp-build-tools'
$installerType = 'EXE'
$url = 'https://download.microsoft.com/download/5/A/8/5A8B8314-CA70-4225-9AF0-9E957C9771F7/vs_BuildTools.exe'
$checksum = 'E77D433C44F3D0CBF7A3EFA497101DE93918C492C2EBCAEC79A1FAF593D419BC'

How can I overcome this hardcoded Url value and force the install script to download the package from my local artifactory server?

Before discovering the hardcoded Url value inside the package installation script, I tried using the --source option with choco install as described in the guide here https://jfrog.com/blog/artifactory-as-a-caching-mechanism-for-package-managers/ but I noticed that the package is still downloaded from the internet.


Solution

  • Packages on the Chocolatey Community Repository often can't include the original binaries due to licenses and distribution rights - leading to each user downloading the file from the source URL.

    However, you can manually re-create the package with the downloaded file inside - to do so, you should:

    • Extract the relevant content of the nupkg file (e.g. the tools directory and nuspec file)
    • Download the file specified in URL (which is the file being downloaded repeatedly during the package installation) to the tools directory (or somewhere else within the package)
    • Replace the URL in URL with the relative path to the downloaded file
    • Run choco pack $PathToNuspecFile (in the root of the extracted files)
    • Upload the resultant nupkg file to your Artifactory instance

    Alternatively, you could host the binary file on your Artifactory server and update URL to point to that. This would have the potential benefit of not downloading the full size if you had logic in the package that would short-circuit the installation before the actual install step, e.g. if you had gigantic MSU file you only wanted to download if you needed to apply it.

    Finally, one of the parts of a licensed copy of Chocolatey for Business is the Package Internalizer.
    This automates the process described above, allowing you to simply call choco download visualcpp-build-tools --internalize.