Saturday, August 1, 2015

Setup your Chocolatey repository in Azure - Part 2

In the first part of this series we've seen how we can have our chocolatey repository hosted on Azure, we can start pushing and using some packages from the server.

To create and publish packages you’ll need nuget executable on your workstation. You can download the nuget.exe from the link https://www.nuget.org/nuget.exe.

The next step is to create a nuspec file with the details of the package you want to create. In this sample I’ll create a nuspec file for creating a package for the Foxe XML editor and push that to the chocolatey repository.

Create a folder foxe-package in your workstation and add the nuspec file with the contents as given below.

xml version="1.0" encoding="utf-8"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
        <id>Foxe</id>
        <version>1.2.0</version>
        <title>Firstobject's free XML editor for Windows</title>
        <authors>Firstobject</authors>
        <owners>Firstobject</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Free XML editor for Windows</description>
        <summary>
          It loads big files (multi-megabyte) fast and lets you format XML and edit HTML and any loosely formed XML or other markup.
          The tree view is editable and customizable for useful navigation.
        </summary>
        <language>en-US</language>
        <tags>XML, Windows, Free, Tools, Editor</tags>       
    </metadata>
</package>

Now create a Tools folder under the foxe-package folder and add a chocolateyinstall.ps1 file with the contents

Install-ChocolateyPackage 'Foxe' 'exe' '/quiet' 'http://www.firstobject.com/foxesetup242.exe'

This will download the installer from the url mentioned in the script and install it on the workstation. We've use the Install-ChocolateyPackage command to run the installer downloaded from the FirstObject website. You can also make use of other commands available to further customize your installation process. For a full list of commands refer the wiki here https://github.com/chocolatey/chocolatey/wiki/HelpersReference 

After creating the nuspec file, we’ll use the pack command of the nuget executable to create a nupkg file that will be pushed to the repository.


PS C:\Packages\foxe-package> .\nuget.exe pack .\Foxe.nuspec
Attempting to build package from 'Foxe.nuspec'.
Successfully created package 'C:\Packages\foxe-package\Foxe.1.2.0.nupkg'.

WARNING: 1 issue(s) found with package 'Foxe'.

Issue: Unrecognized PowerShell file.
Description: The script file 'tools\chocolateyinstall.ps1' is not recognized by NuGet and hence will not be executed during installation of this package
.
Solution: Rename it to install.ps1, uninstall.ps1 or init.ps1 and place it directly under 'tools'.


Now we have the package ready, lets push this to our chocolatey repository


PS C:\Scripts\Chocolatey> .\nuget.exe push .\Foxe.1.2.0.nupkg "YOUR_API_KEY" -s http://prajeeshchoco.azurewebsites.net/
Pushing Foxe 1.2.0 to 'http://prajeeshchoco.azurewebsites.net/'...
Your package was pushed.


You can list the packages available on the server by using the list command, to verify the packages available.


PS C:\Scripts\Chocolatey> .\nuget.exe list -s http://prajeeshchoco.azurewebsites.net/nuget -allversions
Foxe 1.0.0
Foxe 1.1.0
Foxe 1.2.0



Next we can try to install this package using chocolatey on the workstation. For installation, open PowerShell as Administrator and execute the command

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
and press enter. This will install Chocolatey on the machine.



Now you can use the choco command to install the package from the server as given below.


Next we’ll use PowerShell PackageManagement which is a unified interface to package management systems to make Software Discovery, Installation and Inventory (SDII) work via a common set of cmdlets. We’ll add this repository to the OneGet sources and install packages from here. That's in the next post.

No comments: