DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Release Automatically With PowerShell, Zipped Artifacts, and Chocolatey

Release Automatically With PowerShell, Zipped Artifacts, and Chocolatey

How to use Chocolatey, a package manager for Windows based on NuGet, and PowerShell to make continuous deployment easy.

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
Jun. 27, 16 · Tutorial
Like (1)
Save
Tweet
Share
3.53K Views

Join the DZone community and get the full member experience.

Join For Free

in previous posts i've described how to create a simple powershell script that is capable of installing software, starting from a zipped file that contains the “release” of a software (produced by a build), and some installation parameters. once you have this scenario up and running, releasing your software automatically is quite simple.

once you have automated the installation with a powershell script, plus an archive file with everything needed to install the software, you are only a step away from continuous deployment.

a possible release vector is chocolatey , a package manager for windows based on nuget. i’m not a chocolatey expert, but at the very basic level, to create a chocolatey package you should only need a bunch of files that contain artifacts to be installed and a powershell script to install the software.

the very first step is creating a .nuspec file that will define what will be contained in the chocolatey package. here is some example content of release.nuspec file:

<package>
  <metadata>
  …
  </metadata>

   <files>
        <file src=”tools\setup\chocolateyinstall.ps1″ target=”tools” />
        <file src=”tools\setup\configurationmanagersetup.ps1″ target=”tools” />
        <file src=”tools\setup\jarvisutils.psm1″ target=”tools” />
        <file src=”release\jarvis.configurationservice.host.zip” target=”artifacts” />
  </files>

</package>

the real interesting part of this file is the <files> section, where i simply list all the files i want to be included in the package. configurationmanagersetup.ps1 and jarvisutils.psm1 are part of the original installation script i wrote, and jarvis.configurationservice.host.zip is the name of the artifacts generated by the packrelease.ps1 script.

to support chocolatey you only need to create another script, called chocolateyinstall.ps1 that will be called by chocolatey to install the software. this script is really simple, it needs to parse chocolatey input parameters and invoke the original installation script to install the software.

write-output "installing jarvis.configurationmanager service. script running from $psscriptroot"

$packageparameters = $env:chocolateypackageparameters
write-output "passed packageparameters: $packageparameters"

$arguments = @{}

# script used to parse parameters is taken from https://github.com/chocolatey/chocolatey/wiki/how-to-parse-packageparameters-argument

# now we can use the $env:chocolateypackageparameters inside the chocolatey package
$packageparameters = $env:chocolateypackageparameters

# default the values
$installationroot = "c:\jarvis\setup\configurationmanager\configurationmanagerhost"

# now parse the packageparameters using good old regular expression
if ($packageparameters) {
    $match_pattern = "\/(?([a-za-z]+)):(?([`"'])?([a-za-z0-9- _\\:\.]+)([`"'])?)|\/(?([a-za-z]+))"
    $option_name = 'option'
    $value_name = 'value'

    write-output "parameters found, parsing with regex";
    if ($packageparameters -match $match_pattern )
    {
       $results = $packageparameters | select-string $match_pattern -allmatches
       $results.matches | % {

            $arguments.add(
                $_.groups[$option_name].value.trim(),
                $_.groups[$value_name].value.trim())
       }
    }
    else
    {
        throw "package parameters were found but were invalid (regex failure)"
    }

    if ($arguments.containskey("installationroot")) {

        $installationroot = $arguments["installationroot"]
        write-output "installationroot argument found: $installationroot"
    }
} else 
{
    write-output "no package parameters passed in"
}

write-output "installing configurationmanager in folder $installationroot"

$artifactfile = "$psscriptroot\..\artifacts\jarvis.configurationservice.host.zip"

if(!(test-path -path $artifactfile ))
{
     throw "unable to find package file $artifactfile"
}

write-output "installing from artifacts: $artifactfile"

if(!(test-path -path "$psscriptroot\configurationmanagersetup.ps1" ))
{
     throw "unable to find package file $psscriptroot\configurationmanagersetup.ps1"
}


if(-not(get-module -name jarvisutils)) 
{
    import-module -name "$psscriptroot\jarvisutils"
}

&amp; $psscriptroot\configurationmanagersetup.ps1 -deployfilename $artifactfile -installationroot $installationroot

this script does almost nothing, it delegates everything to the configurationmanagersetup.ps1 file. once the .nuspec file and this script are writenn, you can use nuget.exe to manually generate a package and verify installing in a target system.

when everything is ok and you are able to create and install a chocolatey package locally, you can schedule chocolatey package creation with a tfs build. the advantage is automatic publishing and semver version management done with gitversion.exe.

image

figure 1: task to generate chocolatey package.

to generate a chocolatey package you can use the standard nuget packager task, because chocolatey uses the very same technology as nuget. as you can see from figure 1 , the task is really simple and needs very few parameters to work. if you like more detail i’ve blogged in the past about using this task to publish nuget packages: publish a nuget package to nuget/myget with vso build .

this technique is perfectly valid for vsts or tfs 2015 on-premises. once the build is finished, and the package is published, you should check on nuget or myget if the package is ok.

image

figure 2: package listing on myget, i can verify all published versions

now you can install with this command:

choco install jarvis.configurationservice.host
 -source 'https://www.myget.org/f/jarvis/api/v2'
-packageparameters "/installationroot:c:\program files\proximo\configurationmanager" 
   -force 


all parameters needed by the installation scripts should be specified with the –packageparameters command line options. it is the duty of chocolateyinstall.ps1 to parse this string and pass all parameters to the original installation script.

if you want to install a prerelease version (the ones with a suffix) you need to specify the exact version and use the –pre parameter.

a working example of scripts and nuspec file can be found in configurationmanager project .

Chocolatey PowerShell Artifact (UML) Archive file Release (agency)

Published at DZone with permission of Ricci Gian Maria. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Securing VMs, Hosts, Kubernetes, and Cloud Services
  • Why You Should Automate Code Reviews
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • 3 Ways That You Can Operate Record Beyond DTO [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: