Continuous Deployment With Psake



Last updated: January 28th, 2024

What is Psake?

Psake is a PowerShell module for running dependent tasks. Each task can invoke console commands and .NET code. Psake helps with build automation by allowing tasks to be executed in a business process oriented fashion. For example, to deploy the code, first the code must be packaged, before that it needs to be tested, before that it needs to be built. There can be new types of tests and new types of deployments as well as validation all along the way. This makes it a great choice to move onto dedicated build server(s) with little setup or couping to a vendor.
Psake builds can enable continuous integration, continuous delivery (packaging) and also continuous deployment. Continuous deployment is possible through a build server utilizing Psake. Successful continuous deployment invokes post deployment checking and reporting, if it can be done in PowerShell or .NET, it can become a Psake task.


Basic Installation

To get Psake go to: psake on GitHub and read the readme.md.
To Use:
PS> Import-Module [PATH TO MODULE FOLDER]
To Run (executes: default in default.ps1):
PS> Invoke-Psake
To run a specific task from a specific file:
PS> Invoke-psake .\FILE_NAME.ps1 TASK_NAME


Module Installation

To install Psake as a module, get the path:
PS> $env:psmodulepath
After that paste the contents of the extracted zip into a folder name: psake. The module can then be imported as:
PS> Import-Module psake


Creating and Executing a Task

A Psake script needs a default task named: default that depends on one or more tasks and it cannot define an action block.
 task -name Build -description "This is documentation" -action { 
     Write-Host "This is a task" -ForegroundColor Black -BackgroundColor Green
 }; 

task -name default  -depends build 
To run the task in the default.ps1 file:
PS> Invoke-psake .\default.ps1
The task to run can be specified in the task parameter with properties:
PS> Invoke-psake .\default.ps1 Build  -properties @{'msbuildconfig'='debug'} 
The tasks are run in the order of the depends parameter. Each Psake task can be invoked only once during execution. The order of task execution is a comma separated list of tasks then the dependency tree of the tasks. The optional description parameter is useful when querying the documentation.


Task Execution

When executing console commands, always wrap them in:
 exec { command }
Unless there is good reason not to, such as checking exit codes, use exec. This will cause failure when the exit code is not 0. If this isn't done, there can be a false positive from Psake saying there was success when it was failure. No task to a console command should be used without wrapping it in an exec command.
Each task can have an -action where it executes code and a -depends which lists the task names that must be completed prior to running that task. Each task can only be executed one time in the entire call to Invoke-Psake. Psake can be invoked recursively if that's needed.

Writing Task Documentation

To view the documentation of tasks, which displays the -description parameter, the name and dependencies:
PS> Invoke-psake .\default.ps1 -docs

Properties

Properties are set in a psake script to use variables throughout the script that can be passed in as arguments.
properties {
 $msBuildConfig = 'debug'
}
Properties can be set in psake by passing the -properties parameter. These properties will overwrite the values defined in the properties declaration in the psake script.
-properties @{'msbuildconfig'='debug'} 



Psake Functions

Psake provides the ability to format the task name, and run script before and after the execution of each task.

Conclusion

Psake is a great way to do continous deployment because it allows the invocation of console applications with proper exit code handling. It's a great way to interact with MSBuild which can do almost everything related to building, packaging and deploying an ASP.NET application. This is the ideal way to have extensibility points in a build process because new tasks can easily be added with proper validation and dependencies. It's much easier to debug and maintain PowerShell scripts than it is XML files.
I just pushed all the code for this site to GitHub and added the build script, sans the config.ps1, to the repo: default.ps1. Psake was made by: James Kovacs.
For more deatils about Psake, checkout the Psake wiki page.

Comments

No Comments

Post Comment

Prove you are human 3 + 3 =



Join my email list!



ryan
About Me

With over 15 years in tech, I've excelled as a senior software engineer, specializing in ASP.NET, C#, SQL, Azure, and front-end technologies. I've led diverse projects across various sectors, from startups to global corporations, particularly during my decade in the San Francisco Bay Area.


Sign Up With SoftSys Hosting! (My host)