Using OneGet with Artifactory

Artifactory, OneGet, NuGet, Chocolatey, and Powershell

Setup and Installations

Recently there has been an enthusiastic buzz around OneGet, Microsoft’s new download manager for Powershell. With OneGet, Windows now has a first-class deployment manager quite similar to what the *inx folks had for years with the Apt-Get download manager.

OneGet is a command line tools which runs inside a Powershell command box (currently only available for Win 8.1 and 2012 Server R2). OneGet uses the NuGet packaging format and currently retrieves packages stored in the Chocolatey gallery (other remote sources and formats will be added in the future). There are already quite a few very good OneGet blogs already written. A quick search for “OneGet Powershell” will find these.

The purpose of this post is to describe how to use Artifactory to serve as a proxy between OneGet and Chocolatey galleries. Why would we need a proxy? Imagine the internet connection is not available. Imagine not having control over which versions and packages are actually downloaded. Imagine hosting the locally built packages for developers to share with other developers. Imagine having just one source for all binaries, even if they are not NuGet packages.

First step, we need is the downloading and setting up of Artifactory for NuGet packages. This has been described already in the link above. After Artifactory has been download and setup, create the following customizations. First, create a remote repo “chocolatey-galley” that resolves to https://chocolatey.org from the internet.  Second, create a virtual repo named “chocolatey” that resolves to the remote repo “chocolatey-gallery”.  This allows you to search the local virtual repo “chocolatey” for any item that is stored in “chocolatey.org”. Any item downloaded from chocolatey.org will be cached in the local “chocolatey-gallery” and be available for the next download request without going to the internet.

Second step, execute the setup of Powershell 5.0 as described in this recent post. Open an administrator command window and start Powershell.

Third step, need to setup and use of OneGet as described in this recent post. Follow the instructions to setup OneGet.

Using OneGet

Now that OneGet is setup, use OneGet to install a NuGet package on this machine. From the Powershell command line issue the following OneGet command:

Find-Package -Name NuGet.vs | Install-Package –Verbosity

After installing NuGet, list the “sources” available for NuGet packages using this command.

Get-PackageSource

Just one Chocolatey (NuGet) source is now available, with the name “NuGet”. We want to add our two new Chocolatey (NuGet) sources found inside of our Artifactory server. The first, will give access to artifacts built locally and stored as NuGet packages (CI Builds) while the second, will give access to items that can be found at Chocolatey.org’s website.

Let’s use the following OneGet commands:

addpackagesource -Provider Chocolatey -Name JFrog –Location  https://tfs:8081/artifactory/api/nuget/nuget-staging-local

addpackagesource -Provider Chocolatey -Name localchoco –Location  https:// tfs:8081/artifactory/api/nuget/chocolatey-gallery

After setting up Artifactory assure that Artifactory is being used to proxy the Chocolately gallery. We can do this by removing the original “NuGet” gallery source using the OneGet command below:

removepackagesource –Name NuGet

This will remove the “NuGet” source for Chocolatey (NuGet) packages. Now running the get-packagesource command will list only two sources, “jfrog” and “localchoco”, in the Names header. Both are located in the Artifactory server. The “localchocopackagesource will proxyhttps://Chocolatey.org and the “jfrogpackagesource will proxy the local release folder in Artifactory https://tfs:8081/artifactory/api/nuget/nuget-release-local.

Get-PackageSource

The OneGet command “find-package” will list only the first 20 packages found in the repositories. Note that the Source header column is listing both “jfrog” and “localchoco” as sources. The Jfrog source will contain packages built and stored locally, while the localchoco source will list packages found in the chocolatey.org on-line repository.

Find-Package

To download a package from the chocolatey.org, use the “install-package packagename” command. This will pull and install the package and any dependencies to the local machine. Since we are using Artifactory as a proxy cache, Artifactory will cache and store the files. Below is an example of an install for the ack package with a dependency on the StrawberryPerl package.

Install-Package ack

As the ack and StrawberryPerl packages are installed locally, these packages are cached inside of the chocolatey-gallery in Artifactory. The next time any developer needs to install the “ack” package, downloads will occur from Artifactory instead of the chocolatey.org gallery.

Now we can use all the Powershell OneGet commands to manage our NuGet packages from Chocolatey:

Get-PackageSource Get-Packages Find-Package Add-PackageSource Remove-PackageSource Install-Package Uninstall-Package

For example:

Find-packages

Notice the two different sources. Artifactory will list only the first 20 packages found.

Find-packages sqlserver

We can also access the same files with the NuGet commands.

NuGet list –s https://tfs:8081/artifactory/api/nuget/chocolatey

NuGet list sqlserver –s https://tfs:8081/artifactory/api/nuget/chocolatey

Chocolatey Installations

Once a package is installed using OneGet, it behaves like a regular Chocolatey installation. The Nuget packages are installed to the $Chocolateylib directory. The packaged is cached under the $AppDataLocalNuGetCache. If you do not have connection to the internet, previously installed packages can be retrieved from either of these locations.

The Chocolatey libs folder is the list of installed packages for each developer’s machine. This can be very important in helping to track down dependency differences on multiple machines or build boxes.

Chocolatey (NuGet) package binaries may or may not be stored inside the NuGet packages. These file can be stored on a public facing local resources or a private repository such as BinTray. The binaries are accessed using a Powershell script that is stored inside the Chocolatey (NuGet) package.

NuGet Package Information in Artifactory

In addition to the benefits of having local control for NuGet packages, Artifactory provides valuable information about each package stored, including license information, owner information, author information, version, and dates for each package.

Standard properties are stored as metadata within Artifactory. Additionally, any custom properties can be easily created and stored.

For every artifact stored in Artifactory a sha1 and md5 will be calculated. These are unique for every artifact and can be used in searches.

An Artifactory repository for your NuGet packages will help reduce network traffic, give reliable access to Chocolatey.org, provides approved versions of packages and gives security and access control to your binary dependencies.

Local proprietary NuGet packages and other non-NuGet package formats may also be stored on the same Artifactory server.