My own Chocolatey package for dessert

Coffman
4 min readAug 20, 2018

--

Chocolatey.org

One of the very cool features of Chocolatey is the ability to create your own software packages (nuget) as well as host your own feeds.

Chocolatey.org has great information on how to create packages.

I wanted to create software packages for internal software. The process was relatively simple. The challenge is in the many options available to create your own packages.

One of the many processes at my company is the nightly build of our software. We have various products which are built each and every night. It was this nightly build that I wanted to use for packages. It was a challenging process but one that I was able to get working.

Here are my steps, in case anyone else has issues.

Step #1 — Create the nuspec File

First and foremost, the package needs a nuspec file. The nuspec file is the key to the package. Open PowerShell as Admin and run the following command to create a nuspec file.

choco new PackageName

The packagename.nuspec is an XML file with specific information used by Chocolatey to create the Nuget package. Below is a sample nuspec file. As I was using nightly builds, I needed to make sure the version was updated each and every night.

<package xmlns=”http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>IDforYourPackage</id>
<version>9.8.7.6</version>
<title>My Software</title>
<authors>My Company</authors>
<projectUrl>https://www.mywebsite.com</projectUrl>
<! — <tags>zetta</tags> →
<summary>My Software, ‘nuff said.</summary>
<description>This is the latest and greatest version of my software. Use at your own risk, as things might not work as expected!</description>
</metadata>
<files>
<! — this section controls what actually gets packaged into the Chocolatey package →
<file src=”tools\**” target=”tools” />
<! — Building from Linux? You may need this instead: <file src=”tools/**” target=”tools” /> →
</files>
</package>

Luckily, I didn’t have to reinvent the wheel. I was able to utilize the knowledge of others in my quest to programmatically update the nuspec file.

Step #2 — Special Consideration for Install

One thing to keep in mind when creating Chocolatey packages is the installation. Are there any specific steps needing to be performed to upgrade? What about the installation itself? Or perhaps uninstall?

In my case, I needed to ensure my application was closed prior to upgrade. So I had to create a before chocolateybeforemodify.ps1 that contained the following line of code.

Get-Process | Where-Object { $_.name eq ‘MyUniqueSoftware*’ } | Stop-Process

And let’s not forget about the actual install file for Chocolatey. In my case, chocolateyinstall.ps1 contained the following lines.

= ‘mynightlybuild’
= ‘exe’
mynightlybuild.exe = C:\mysource\forhosting\feeds\mynightlybuild.exe
= ‘/SILENT’
= @(0)

Install-ChocolateyInstallPackage “packageName” “” “” “” -validExitCodes $validExitCodes -checksum “VERYLARGEVALUE” -checksumType “sha256”

Step #3 — Create NuGet Package

After all that, the next step was to actually create the NuGet package. The secret command for Chocolatey to create the package: choco pack. Again, no reason to reinvest the wheel, there is plenty of information to help to create the build script. Here is a basic script:

cd “$PSScriptRoot”
$toolsDir = “$(Split-Path -parent $MyInvocation.MyCommand.Definition)”
$fileLocation = Join-Path $toolsDir ‘mynightlybuild.exe’
$file = “mynightlybuild.exe”
$sha = get-filehash -path .\tools\$file -Algorithm SHA256 | select -ExpandProperty “Hash”

# Fill into Choco Install Template
$filetext = @”
$packageName = ‘mynightlybuild’
$fileType = ‘exe’
$file = $fileLocation
$silentArgs = ‘/SILENT’
$validExitCodes = @(0)

Install-ChocolateyInstallPackage “packageName” “$fileType” “$silentArgs” “$url” -validExitCodes `$validExitCodes -checksum “$sha” -checksumType “sha256”
“@

# Write it to disk
out-file -filepath .\tools\chocolateyinstall.ps1 -inputobject $filetext

# Delete any existing NuGet Packages
del *.nupkg
#
# Create .nupkg from .nuspec
choco pack

# Forced install out of current folder
choco install “mynightlybuild” — install-arguments “YouWouldLikeToKnow” -fdv -s “.\”

After some trial and error, I was able create a mynightlybuild.nupkg. I had no intention of publishing the package to the Chocolatey community. No, no, no, this was my software for internal purposes. I used my own source for installation ( — is actually double hyphens).

Step #4 — Test of NuGet Package

choco install mynightlybuild -source=”\\UNCPath\MySource\Chocolatey\ — install-arguments=”/PASSWORD=InstallerPW” -y

choco upgrade mynightlybuild -source=”\\UNCPath\MySource\Chocolatey\ — install-arguments=”/PASSWORD=InstallerPW” -y

What is the “-y” on the end? Excellent question. If “-y” is passed, it tells Chocolatey that confirmation has been given for all prompts. So it is essentially passing a YES.

Wrap Up

Isn’t this a manual process? It was in the beginning. Eventually, I created PowerShell scripts to perform all the necessary steps. Chocolatey is definitely a very cool and very powerful tool in any techie’s arsenal.

--

--

Coffman

I am a father and geek who loves his job and technology. What more can I say?