Reusable sources for build, test, release automation of PowerShell modules, written in DotNet/C# or PowerShell.
PsBuildTasks provides reusable sources for build, test, release automation of PowerShell modules, written in DotNet/C# or PowerShell.
This includes:
For details, visit the GitHub Pages.
The PsBuildTasks PowerShell module helps install and manage PsBuildTasks.
Install it with Install-Module -Name PsBuildTasks from PsGallery.
Choose a task template based on your project’s language and supported platforms:
See the InvokeBuild task guide for setup details. The module’s container tasks can then update the build, workflow, and other PsBuildTasks files.
Go to the project root directory and execute the following script. Make sure to save your existing code in git, since files may be overwritten.
Import-Module -Name PsBuildTasks
Install-PsBuildTask -Path . -Task PowerShell-Matrix
$ModuleName = ( Get-Location | Get-Item ).Name
Invoke-Build -File .\tasks\PsBuild.Tasks.ps1 -Task UpdatePsBuildTasks
Set-Content -Path .build.ps1 -Value @"
[CmdletBinding()]
param (
[Parameter()]
[int] $BuildNumber
)
`$ModuleName = '$ModuleName'
. `$PSScriptRoot/tasks/Build.Tasks.ps1
. `$PSScriptRoot/tasks/PsBuild.Tasks.ps1
task InstallModuleDependencies -Jobs {}
task InstallBuildDependencies -Jobs InstallModuleDependencies, {
Install-Module platyPs
}
task InstallTestDependencies -Jobs InstallModuleDependencies, {}
task InstallReleaseDependencies -Jobs InstallModuleDependencies, {}
"@
In case you start a module from scratch, create a module structure like this.
New-Item src -ItemType Directory
New-ModuleManifest -Path src/$ModuleName.psd1 -RootModule "$ModuleName.psm1"
$local:manifest = Get-Content -Path src/$ModuleName.psd1 | ForEach-Object {
$_ -Replace 'FunctionsToExport', '# FunctionsToExport' -Replace 'CmdletsToExport', '# CmdletsToExport' -Replace 'VariablesToExport', '# VariablesToExport' -Replace 'AliasesToExport', '# AliasesToExport'
}
$local:manifest | Set-Content -Path src/$ModuleName.psd1
New-Item src/Internal -ItemType Directory
New-Item src/Public -ItemType Directory
New-Item test -ItemType Directory
For a Windows-only module, install the Windows container task and use it to update the project files:
Import-Module -Name PsBuildTasks
Install-PsBuildTask -Path . -Task PowerShell-Windows
$ModuleName = ( Get-Location | Get-Item ).Name
Invoke-Build -File .\tasks\PsBuild.Tasks.ps1 -Task UpdatePsBuildTasks
Create .build.ps1 with the same PowerShell configuration shown above: set $ModuleName, dot-source tasks/Build.Tasks.ps1 and tasks/PsBuild.Tasks.ps1, and define the dependency tasks.
The .NET build tasks expect the project under src/<ModuleName> and the published output to contain <ModuleName>.psd1. Place the manifest beside the project file and configure the project to copy it to publish output. For example, add this to <ModuleName>.csproj:
<ItemGroup>
<None Update="MyModuleName.psd1">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>
Replace MyModuleName with the module name. Install the .NET matrix task and update the project task files:
Import-Module -Name PsBuildTasks
Install-PsBuildTask -Path . -Task Dotnet-Matrix
$ModuleName = ( Get-Location | Get-Item ).Name
Invoke-Build -File .\tasks\PsBuild.Tasks.ps1 -Task UpdatePsBuildTasks
Create .build.ps1 with the required module name and build configuration, and define the dependency tasks:
[CmdletBinding()]
param (
[int] $BuildNumber,
[string] $Configuration = 'Release'
)
$ModuleName = 'MyModuleName' # Replace with the module name and src subdirectory.
. $PSScriptRoot/tasks/Build.Tasks.ps1
. $PSScriptRoot/tasks/PsBuild.Tasks.ps1
task InstallBuildDependencies {
Install-Module platyPs -Scope CurrentUser
}
task InstallTestDependencies {}
task InstallReleaseDependencies {}
These commands update the PsBuildTasks build, workflow, and supporting template files in your project; they do not update the installed PsBuildTasks PowerShell module. The update tasks use the main branch by default. To update your project files from main, run:
Invoke-Build -File .\tasks\PsBuild.Tasks.ps1 -Task UpdatePsBuildTasksTasks; Invoke-Build -File .\tasks\PsBuild.Tasks.ps1 -Task UpdatePsBuildTasks
To use a different branch, set $PsBuildTaskBranch before running the update tasks:
$PsBuildTaskBranch = 'develop'; Invoke-Build -File .\tasks\PsBuild.Tasks.ps1 -Task UpdatePsBuildTasksTasks; Invoke-Build -File .\tasks\PsBuild.Tasks.ps1 -Task UpdatePsBuildTasks
See the changelog file.