-
-
Notifications
You must be signed in to change notification settings - Fork 59
How Build Works
Invoke-Build invokes a build script, the default or specified by the parameter
File
, with specified tasks and script parameters. $ErrorActionPreference
is
set to Stop
, the current location is set to the build script directory. The
script declares its parameters, sets variables, and defines at least one task
.
Then Invoke-Build invokes tasks as follows:
-
Tasks specified by the parameter
Task
are invoked. If the parameter is not specified or it is null, empty, or equal to "." then the "." task is invoked if it is defined, otherwise the first added task is invoked. -
The task parameter
If
is checked. If it is present and evaluates to false then the task is skipped. The task still may be invoked later if it is called again andIf
is defined as a script block which gets true that time. -
The task jobs are invoked, that is referenced tasks and own scripts in the specified order. The jobs are the original jobs defined by the parameter
Jobs
and task references added by other tasks usingBefore
andAfter
.Before
jobs are inserted before the first script job.After
jobs are added to the end. -
Before the first script block is invoked, the inputs and outputs from the parameters
Inputs
andOutputs
are evaluated and compared. If they are not specified or outputs are out-of-date then all script blocks are invoked. Otherwise script blocks are skipped, only referenced tasks are invoked.
The $ErrorActionPreference
for each build script is set to Stop, otherwise
it is too easy to miss errors that should normally stop the build. Scripts may
change it at the script level once for all their tasks. But it is safer to keep
it and specify relaxed error actions for some commands.
Before any script code is invoked, i.e. a script itself, task jobs, conditions,
inputs, outputs, and blocks, the current location is set to $BuildRoot
which
is by default the build script directory. This is very useful for accessing
other files by relative paths, including invocation of external scripts.
As soon as a task gets invoked and succeeds or fails, its contribution to the current build is over, the task itself and its parts are never invoked again. Code that may have to be invoked more than once should be defined as a function, not a task. This function is called by tasks when needed.
If a task throws an exception or writes a terminating error then the whole
build fails unless the task is referenced as safe ?TaskName
by the
calling task and other tasks having a chance to be invoked.
The same convention works for task names in command lines.
In addition to tasks, build scripts may define special script blocks which are invoked as described below:
-
Enter-Build {}
- before all tasks -
Exit-Build {}
- after all tasks -
Enter-BuildTask {}
- before each task -
Exit-BuildTask {}
- after each task -
Enter-BuildJob {}
- before each task action -
Exit-BuildJob {}
- after each task action -
Set-BuildHeader {param($path)}
- on writing task headers
- Concepts
- Script Tutorial
- Incremental Tasks
- Partial Incremental Tasks
- How Build Works
- Special Variables
- Build Failures
- Build Analysis
- Parallel Builds
- Persistent Builds
- Portable Build Scripts
- Using for Test Automation
- Debugging Tips
- VSCode Tips
Helpers
- Invoke Task from VSCode
- Generate VSCode Tasks
- Invoke Task from ISE
- Resolve MSBuild
- Show Build Trees
- Show Build Graph
- Argument Completers
- Invoke-Build.template
Appendix