Define Build Logic For Your App

When building your package, your project source (or pre-built binary) goes through a series of steps as part of the package compilation process. Depending on whether or not your Chef Habitat plan resides in the same repo as your project, these steps could include downloading your source, unpacking it, building it, and then installing it into the appropriate location within a Chef Habitat package.

The default behavior for these build steps are defined in build phase callbacks, which are functions called by Chef Habitat when building your package. When overriding any callbacks, you may use any of the plan variables, settings, or functions in the Plan syntax guide, except for the runtime configuration settings. Those can only be used in runtime hooks once a Chef Habitat service is running.

Overriding Build Phase Callbacks

You can override build phase callbacks in your plan. Default behavior may be changed and new functionality can be introduced to package your ASP.NET application appropriately.

At a minimum, you will want to override the Invoke-Build function to install dependent .Net assemblies and compile your application code. You will also need to override Invoke-Install to publish the artifacts of your build to the Chef Habitat build's package directory.

function Invoke-Build {
Copy-Item $PLAN_CONTEXT/../* $HAB_CACHE_SRC_PATH/$pkg_dirname -recurse -force
nuget restore "$HAB_CACHE_SRC_PATH/$pkg_dirname/C#/$pkg_name/packages.config" -PackagesDirectory "$HAB_CACHE_SRC_PATH/$pkg_dirname/C#/packages" -Source ""
nuget install MSBuild.Microsoft.VisualStudio.Web.targets -Version -OutputDirectory $HAB_CACHE_SRC_PATH/$pkg_dirname/
$env:TargetFrameworkRootPath="$(Get-HabPackagePath dotnet-45-dev-pack)\Program Files\Reference Assemblies\Microsoft\Framework"
$env:VSToolsPath = "$HAB_CACHE_SRC_PATH/$pkg_dirname/MSBuild.Microsoft.VisualStudio.Web.targets."
MSBuild "$HAB_CACHE_SRC_PATH/$pkg_dirname/C#/$pkg_name/${pkg_name}.csproj" /t:Build
if($LASTEXITCODE -ne 0) {
Write-Error "dotnet build failed!"
function Invoke-Install {
MSBuild "$HAB_CACHE_SRC_PATH/$pkg_dirname/C#/$pkg_name/${pkg_name}.csproj" /t:WebPublish /p:WebPublishMethod=FileSystem /p:publishUrl=$pkg_prefix/www

Here we use the nuget.exe utility to populate dependencies and install the MSBUILD target we need to build Visual Studio web projects. We use MSBUILD.exe, included in the Visual Studio Build Tools, to build our C# code and compile our .NET assembly. Next we use the WebPublish target from MSBUILD to copy all of our web application artifacts to the $pkg_prefix directory. This variable is defined by the Chef Habitat build system and expands to the absolute path of the Chef Habitat package staging area. All files located in that directory will find their way to the final Chef Habitat .hart file which is the Chef Habitat package artifact.

Next: Add Configuration