Psake, pronounced sah-ki, is an automation tool that allows anyone to create software build scripts in PowerShell. DevOps is a hot topic these days. As such, many traditional operations folks find themselves thrust into many traditional developer-focused roles like deploying custom software through a build process. This has led ops guys and gals to adapt their existing knowledge to software development concepts.

Since just about every ops person has some PowerShell command-line knowledge, wouldn't it be nice to use that skill and apply it to the software build process? Yep, and the answer is psake.

Traditionally, creating software builds might have required knowing how to parse XML or write MSBuild scripts. Now, a PowerShell module can abstract all of that away and break it down into a number of easy-to-understand tasks inside a PowerShell script. This way, anyone already knowing how to write scripts in PowerShell can now write advanced build automation scripts as well.

To get started with psake, first download and install it. Since psake is just a PowerShell module and is in the PowerShell Gallery, this process is easy with Find-Module.

Find-Module psake | Install-Module

If you're just fiddling around with psake, you can do this on your local workstation. Otherwise, you'll probably install it on a build server. After installation, you can kick the tires a bit by checking out the examples inside the module directory. I'm executing the default.ps1 build script below.

Invoke-psake 'C:\Program Files\WindowsPowerShell\Modules\psake\4.6.0\examples\default.ps1'
Psake default example

Psake default example

You can see that psake executed a PowerShell script, which contained three tasks called Clean, Compile, and Test. Tasks are at the heart of psake. Psake allows you to define these tasks and any dependencies required among them to execute PowerShell code within each task.

Looking at the default.ps1 build script, you can see each of the defined tasks. Notice above that the tasks executed in the order Clean --> Compile --> Test. However, in the actual script they are set from top to bottom as Test --> Compile --> Clean. Psake can automatically figure out task dependencies by using the -depends parameter on the task. This prevents having to reorder tasks by hand inside of the script itself.

$properties {
    $testMessage = 'Executed Test!'
    $compileMessage = 'Executed Compile!'
    $cleanMessage = 'Executed Clean!'
}
task default -depends Test
task Test -depends Compile, Clean { 
    $testMessage
}
task Compile -depends Clean { 
    $compileMessage
}
task Clean { 
    $cleanMessage
}
task ? -Description "Helper to display task info" {
    Write-Documentation
}

The depends parameter is just one of many useful parameters you can pass to each task. Also useful are a couple of the validation parameters called precondition and requiredvariable. The precondition parameter allows you to define code that will return either $true or $false. If it returns $false, the task will not run at all.

Here's an example:

task default -depends Test
task Test -precondition { $false }
{
    'Running the Test task here'
}
task Compile -depends Test 
{ 
    'Running the Compile task here'
}

You can see I have a precondition set on the Test task. For demonstration purposes, it's always going to fail since I'm explicitly defining a Boolean $false value. Here is the result:

Precondition example

Precondition example

Another way to validate a state before each task runs is with the requiredVariable parameter. This allows the user to ensure a particular variable is available before continuing. Perhaps I have a build that depends on a specific file system location to place files. I store this location in a variable called $buildLocation. It's critical that this exists before running the Compile task. I can check for this like so:

task default -depends Test
task Test
{
    'Running the Test task here'
}
task Compile -depends Test -requiredVariable buildLocation 
{ 
  'Running the Compile task here'
}

To get a complete list of all parameters possible for each task, psake has built-in help. This makes it easy to scour through all of the options available.

Get-Help task

In addition, Get-Help provides help content for all of the other functions inside the psake module. Since psake is just a PowerShell module with functions inside, it can use all of the functionality and discoverability that makes PowerShell so easy to use.

Psake has many other functions that I encourage you to look over as well. Get a list of all available functions with Get-Command -Module psake.

Subscribe to 4sysops newsletter!

You'll see that at its heart, psake is just a task management engine made for software builds. Although it appears to be a simple product, the more you use it, the more you'll find that the shortcuts it provides will save you and your team lots of time in crafting your own XML MSBuild or rake scripts.

avataravatar
0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account