Skip to content

How Build Works

Roman Kuzmin edited this page Nov 24, 2017 · 21 revisions

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:

  1. 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.

  2. 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 and If is defined as a script block which gets true that time.

  3. 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 using Before and After. Before jobs are inserted before the first script job. After jobs are added to the end.

  4. Before the first script block is invoked, the inputs and outputs from the parameters Inputs and Outputs 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 (job Task -Safe) by the calling task and all other tasks having a chance to be invoked in this build.

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