This first post is about preparing our project for Octopus deploy. This example is based upon a standard Asp.Net MVC application.
Preparing involves some knowledge of Teamcity and Octopus deploy. Because deployments in Octopus are done using NuGet, we need the possibility to create a NuGet package from our website.
First we need some deeper knowledge of MsBuild and targets. Open the Developer Command prompt and navigate to your project directory. This is the directory where you can find the csproj file(when using c#). Developer Command prompt can be found under the Visual Studio tools.
Opening the command prompt we can use the raw power of MsBuild, actually we are doing exactly the same thing as Visual studio without the GUI.
First of all, let’s create a build:
This will just build the project, nothing special. Now let’s clean the project:
msbuild TeamcityOctopus.Web.csproj /target:clean
Cleaning removes all assemblies from the bin directory and the temp directories in the obj folder. Remember, every project must survive a clean build! Ever! If you need a reference to a library, create a ‘libs’ directory and refer to that location. Don’t paste the dll in the bin directly, never! Cleaning and Building should produce a new working application.
You can add a configuration to the command:
msbuild TeamcityOctopus.Web.csproj /target:build /property:Configuration=Release
This will build the project using the Release configuration, defined in the project itself.
Ofcourse, it is possible to combine targets:
msbuild TeamcityOctopus.Web.csproj /target:clean;build /property:Configuration=Release
These ‘clean’ and ‘build’ targets are ‘internal’ msbuild targets. Besides using these predefined targets, it is also possible to create your own target. And that’s is precisely what I am looking for.
Using tools like Teamcity and Octopus deploy forces you to think ahead. Running an application locally in not enough. You must lean to think like TeamCity. It will take some time but it’s a great adventure 😉
The first step in creating a package is offering tools for TeamCity to build the project and copy the produced output to a given directory. Unfortunately MsBuild doesn’t offer this out of the box, so we have to create our own target:
<Target Name="PublishToFolder" DependsOnTargets="PipelinePreDeployCopyAllFilesToOneFolder"> <Error Condition="'$(PublishDestination)'==''" Text="Please select a publishing destination folder" /> <MakeDir Condition="!Exists($(PublishDestination))" Directories="$(PublishDestination)" /> <ItemGroup> <PublishFiles Include="$(_PackageTempDir)\**\*.*" /> </ItemGroup> <Copy SourceFiles="@(PublishFiles)" DestinationFiles="@(PublishFiles->'$(PublishDestination)\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="True" /> </Target>
This target requires a parameter called ‘PublishDestination’ so we place this new parameter in the project file:
When running a build running this target, MsBuild copies the created files in the temp directory(obj) to the given PublishDestination. You can accomplish this also using publishprofiles but using Teamcity we need a seperate, clean directory which contains a nice clean Release build of our project.
We can test this target by using this in our MsBuild command:
msbuild TeamcityOctopus.Web.csproj /target:build,PublishToFolder /property:Configuration=Release
(note the PublishToFolder target!)