Skip to content

Parallel Builds

Roman Kuzmin edited this page Nov 27, 2011 · 9 revisions

The script Invoke-Builds.ps1 is used in order to invoke several builds at the same time. It should be located in the same directory as Invoke-Build.ps1. Invoked builds should be independent and should not share any resources simultaneously. Other restrictions are mentioned below.

Any number of builds is allowed, including 0 and 1, though in normal scenarios there are two or more builds. Maximum number of parallel builds is limited by the number of processors by default and can be changed by the parameter.

Every script is invoked in its own runspace, as if PowerShell is just started. That is why build scripts should configure environment for their tasks on their own. A calling script cannot do this. But it can prepare and pass some data in build scripts via parameters.

Builds are specified by hashtables where Task, File, and Parameters entries correspond to the Invoke-Build parameters with the same names. The extra entry Log tells to write build output to the specified file.

Example

Five parallel builds are invoked with various combinations of build parameters. Note that it is fine to invoke the same build script more than once if build flows specified by different tasks do not conflict:

Invoke-Builds @(
    @{File='Project1.build.ps1'}
    @{File='Project2.build.ps1'; Task='MakeHelp'}
    @{File='Project2.build.ps1'; Task='Build', 'Test'}
    @{File='Project3.build.ps1'; Log='C:\TEMP\Project3.log'}
    @{File='Project4.build.ps1'; Parameters=@{Configuration='Release'}}
)

Avoid Host Cmdlets And Members

Any user interaction, host cmdlets and UI members should be removed from scripts designed for parallel builds.

Alternatively, some scripts may be prepared to work in standard and parallel modes differently. The best way is to use a script parameter telling what the current mode is (say, the switch -Parallel).

In many cases this simple trick may be enough: check the host name, if it is Default Host then host cmdlets and members should not be used or should be redefined. Write-Host, for example, can be redefined for doing nothing:

# Define empty Write-Host for the default host
if ($Host.Name -eq "Default Host") {
    function Write-Host {}
}

Be aware that there are more things to avoid in parallel builds. Unfortunately it is not that easy to list them all. Just one example: Out-String does not work properly if strict mode is enabled with some hosts, see Connect.