PowerShell Pipeline Patterns
A pipeline is a script that runs build, test, and package steps in order. Each step is a function, and the script calls them one after another.
One step is one function
A step function prints what it finished. This block defines and calls a build step.
function Invoke-BuildStep { "Build done" }
Invoke-BuildStepSteps run in order
The script calls build, then test, then package. The output lines follow the same order.
function Invoke-BuildStep { "Build done" }
function Invoke-TestStep { "Test done" }
function Invoke-PackageStep { "Package done" }
Invoke-BuildStep
Invoke-TestStep
Invoke-PackageStepA failed test stops the package step
The script packages only when the test passes. Here the test fails, so the package step is skipped.
function Invoke-BuildStep { "Build done" }
function Invoke-TestStep { $false }
Invoke-BuildStep
$passed = Invoke-TestStep
if ($passed) { "Package done" } else { "Package skipped" }Example
function Invoke-BuildStep { "Build done" }
function Invoke-TestStep { $true }
function Invoke-PackageStep { "Package done" }
Invoke-BuildStep
$passed = Invoke-TestStep
if ($passed) { "Test passed"; Invoke-PackageStep } else { "Package skipped" }
"Pipeline done"Key points
- Pipeline: a script that runs several steps in a fixed order.
- Step: one stage of the pipeline, such as building or testing.
- Build step: the step that prepares the product.
- Test step: the step that checks the product works.
- Package step: the step that bundles the product for delivery.
- Gate: a later step runs only when the earlier steps pass.
In this example, the script defines the three step functions, runs the build step, checks the test result, runs the package step because the test passed, and then reports that the pipeline is done.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A test step fails. What should the pipeline do with the package step?