Integration of Cake Build Script With TeamCity - CodeProject
Integration of Cake Build Script With TeamCity - CodeProject
Introduction
Cake is a great tool for organizing a delivery pipeline for your application. I like it because it lets me to write the pipeline using C#,
the language I know well. The great property of Cake, PSake and other similar frameworks is that they allow as to use the same
building script on a local development machine and on CI servers. Here I'll explain how to integrate Cake with TeamCity.
Requirements
I'll assume you have initial knowledge of Cake and TeamCity. Otherwise, you can start with reading:
For Cake:
Website: https://www.cakebuild.net
Pluralsight course: https://www.pluralsight.com/courses/cake-applications-deploying-building
For TeamCity:
Logging
Cake pipeline usually consists of several tasks. It would be good to have a separate section for each such task in the TeamCity
build log. I want to have a collapsible section for each Cake task in the log:
TaskSetup(setupContext =>
{
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteStartBuildBlock(setupContext.Task.Name);
https://www.codeproject.com/Tips/1271037/Integration-of-Cake-build-script-with-TeamCity?display=Print 1/6
10/12/2018 Integration of Cake build script with TeamCity - CodeProject
}
});
TaskTeardown(teardownContext =>
{
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteEndBuildBlock(teardownContext.Task.Name);
}
});
Here TeamCity.IsRunningOnTeamCity property is used to execute the code only if it runs on TeamCity.
Now we have collapsable blocks in the build log. But still, we can improve it a little bit more.
Usually, tasks of Cake tend to have short names like Build, Test, Clean. In this case, it is easier to run them from the
command line. But in the build log, I'd prefer to have more expanded descriptions of Cake tasks. And it is possible to provide such
descriptions. To set description of a task, use Description method:
Task("Clean")
.Description("Create and clean folders with results")
.Does(() => { ... });
TaskSetup(setupContext =>
{
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteStartBuildBlock(setupContext.Task.Description ?? setupContext.Task.Name);
}
});
TaskTeardown(teardownContext =>
{
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteEndProgress(teardownContext.Task.Description ?? teardownContext.Task.Name);
}
});
Progress indication
If running a Cake script takes a lot of time, it would be great to see, which task is executing now.
TaskSetup(setupContext =>
{
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteStartBuildBlock(setupContext.Task.Description ?? setupContext.Task.Name);
TeamCity.WriteStartProgress(setupContext.Task.Description ?? setupContext.Task.Name);
}
});
https://www.codeproject.com/Tips/1271037/Integration-of-Cake-build-script-with-TeamCity?display=Print 2/6
10/12/2018 Integration of Cake build script with TeamCity - CodeProject
TaskTeardown(teardownContext =>
{
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteEndProgress(teardownContext.Task.Description ?? teardownContext.Task.Name);
TeamCity.WriteEndBuildBlock(teardownContext.Task.Description ??
teardownContext.Task.Name);
}
});
Tests results
If you run some tests in your Cake task, it would be great to show the results of their execution in TeamCity.
It can be done using TeamCity.ImportData method. This method accepts two parameters: string description of data type
and path to a file with data. For example, if MSTest is used for tests, here is how you can execute tests and inform TeamCity about
their results:
Task("Run-Tests")
.Description("Run tests")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.Does(() => {
var testDllsPattern = string.Format("./**/bin/{0}/*.*Tests.dll", configuration);
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.ImportData("mstest", testResultsFile);
}
});
TeamCity supports several types of tests. Instead of mstest you can use nunit, vstest and several more.
https://www.codeproject.com/Tips/1271037/Integration-of-Cake-build-script-with-TeamCity?display=Print 3/6
10/12/2018 Integration of Cake build script with TeamCity - CodeProject
Now TeamCity supports integration with DotCover tool. Let me show how to use DotCover in your Cake script. First of all, DotCover
must be installed by adding:
#tool "nuget:?package=JetBrains.dotCover.CommandLineTools"
Task("Analyse-Test-Coverage")
.Description("Analyse code coverage by tests")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.Does(() => {
var coverageResultFile = System.IO.Path.Combine(temporaryFolder, "coverageResult.dcvr");
DotCoverCover(tool => {
tool.MSTest(testDlls, new MSTestSettings() {
ResultsFile = testResultsFile
});
},
new FilePath(coverageResultFile),
new DotCoverCoverSettings()
.WithFilter("+:Application")
.WithFilter("-:Application.*Tests")
);
if(TeamCity.IsRunningOnTeamCity)
{
TeamCity.ImportData("mstest", testResultsFile);
TeamCity.ImportDotCoverCoverage(coverageResultFile);
}
});
As you can see, during this task tests were also run. So we can inform TeamCity both about test results and coverage analysis
results. Method TeamCity.ImportDotCoverCoverage does the last thing.
Publishing artifacts
https://www.codeproject.com/Tips/1271037/Integration-of-Cake-build-script-with-TeamCity?display=Print 4/6
10/12/2018 Integration of Cake build script with TeamCity - CodeProject
TeamCity allows you to publish some artifacts that will be available for each build. A good candidate for such artifacts is a NuGet
package created during the build process:
In order to do it, place all your artifacts in a folder. Then you can publish this folder using TeamCity.PublishArtifacts:
Task("Publish-Artifacts-On-TeamCity")
.Description("Publish artifacts on TeamCity")
.IsDependentOn("Create-NuGet-Package")
.WithCriteria(TeamCity.IsRunningOnTeamCity)
.Does(() => {
TeamCity.PublishArtifacts(artifactsFolder);
});
Conclusion
I hope these short code snippets will save you some time and effort if you want to run your Cake script on TeamCity. The full
version of the Cake script and application you can find at GitHub. Good luck!
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
https://www.codeproject.com/Tips/1271037/Integration-of-Cake-build-script-with-TeamCity?display=Print 5/6
10/12/2018 Integration of Cake build script with TeamCity - CodeProject
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2018 by Ivan Yakimov
Web03 | 2.8.181207.3 | Last Updated 10 Dec 2018 Everything else Copyright © CodeProject, 1999-2018
https://www.codeproject.com/Tips/1271037/Integration-of-Cake-build-script-with-TeamCity?display=Print 6/6