Skip to content

How Build Works

Roman Kuzmin edited this page Oct 12, 2011 · 21 revisions

Invoke-Build invokes the build script specified by the parameter File with Parameters and the current location set to the script directory. The script does whatever is coded there but the goal is to define parameters, set script variables, and add at least one build task (task). Note: if the script does anything else then it can check for the build variable $WhatIf and skip some jobs if its value is true.

Then Invoke-Build invokes the tasks and their jobs 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 dependent 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 tasks are inserted before the first script job. After tasks are inserted after the last script job. If a task has no own script jobs then extra tasks are added to the end.

  4. Before the first own script job is invoked, the inputs and outputs from the parameters Incremental or Partial are evaluated and compared. If they are not specified or outputs are out-of-date then all script jobs are invoked. Otherwise all scripts are skipped, only referenced tasks are invoked.

Before any script is invoked (job, condition, inputs, outputs), the current location is set to $BuildRoot which is the build script directory by default. Scripts and tasks may change the current location and they do not have to care of restoring it.

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.

If a task throws an exception or writes a terminating error then the whole build fails unless the task is referenced as protected (@{Task = 1}) by the calling task and all other tasks having a chance to be invoked in this build.

The $ErrorActionPreference for each build script is set to Stop, otherwise it is too easy to miss errors that should stop the build. Scripts can change it at the script level once for all their tasks.