Project Scripts

The scripts every project has, calling other programs from PowerShell, and keeping secrets out of scripts.

Updated

In a Project

  1. The Scripts Every Project Has (Journeyman): A build.ps1 gathers a project's chores in one script, each task a function that -Task picks through a ValidateSet parameter and a switch; Get-Command checks that the tools the project needs are installed, and $ErrorActionPreference = 'Stop' with throw stops the script at the first problem.
  2. Calling Other Programs (Journeyman): Programs such as git, dotnet and npm run from PowerShell by name, hand back their output as lines of text, take arguments from variables and arrays, and run by a path with & in front; a failing program doesn't stop a script by itself, so check $LASTEXITCODE after it, or set $PSNativeCommandUseErrorActionPreference to $true.
  3. Secrets Out of Scripts (Journeyman): A password or token written into a script travels wherever the script goes, so a script names the secret it needs and reads it from an environment variable, asks for it with Get-Credential, or fetches it from a SecretManagement vault, and stops with a clear message when it isn't there.

Exercise: A Project's Build Script

  1. Exercise: A Project's Build Script (Journeyman): Finish the Maple Street Bikes website's build.ps1 with check, test, package and publish tasks that find their tools with Get-Command, stop when a program's exit code says it failed, and read the deploy token from the environment, and let a Pester check run each task the way a build server would.