Category Archives: CCNET

How to share CCNET label across multiple projects and get incremented for each success build

There is already one post below where I have implemented this but it also increment for failure build also-

How to share CCNET label across multiple projects and get incremented for each build

Now here’s how you can stop it to increment during failure builds-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Exortech.NetReflector;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Remote;
namespace ccnet.SharedLabeller.CruiseControl.plugin
{
    [ReflectorType(“sharedLabeller”)]
    public class SharedLabeller : ILabeller
    {
        ///
        /// The path where the file that holds the shared label should be located
        ///
        /// none
        [ReflectorProperty(“sharedLabelFilePath”, Required = true)]
        public string SharedLabelFilePath { get; set; }
        ///
        /// Any string to be put in front of all labels.
        ///
        [ReflectorProperty(“prefix”, Required = false)]
        public string Prefix { get; set; }
        ///
        /// If true, the label will be incremented even if the build fails. Otherwise it will only be incremented if the build succeeds.
        ///
        [ReflectorProperty(“incrementOnFailure”, Required = false)]
        public bool IncrementOnFailure { get; set; }
        ///
        /// Allows you to set the initial build number.
        /// This will only be used when on the first build of a project, meaning that when you change this value,
        /// you’ll have to stop the CCNet service and delete the state file.
        ///
        /// 0
        [ReflectorProperty(“initialBuildLabel”, Required = false)]
        public int InitialBuildLabel { get; set; }
        public SharedLabeller()
        {
            IncrementOnFailure = false;
            InitialBuildLabel = 0;
        }
        #region ILabeller Members
        public string Generate(IIntegrationResult integrationResult)
        {
            if (ShouldIncrementLabel(integrationResult.LastIntegration))
            {
                return Prefix + IncrementLabel();
            }
            else
            {
                return integrationResult.LastIntegration.Label;
            }
        }
        public void Run(IIntegrationResult integrationResult)
        {
            integrationResult.Label = Generate(integrationResult);
        }
        #endregion
        private string IncrementLabel()
        {
            ThoughtWorks.CruiseControl.Core.Util.Log.Debug(“About to read label file. Filename: {0}”, SharedLabelFilePath);
            using (FileStream fileStream = File.Open(this.SharedLabelFilePath,
                     FileMode.OpenOrCreate,
                     FileAccess.ReadWrite,
                     FileShare.None))
            {
                // read last build number from file
                var bytes = new byte[fileStream.Length];
                fileStream.Read(bytes, 0, bytes.Length);
                string rawBuildNumber = Encoding.UTF8.GetString(bytes);
                // parse last build number
                int previousBuildNumber;
                if (!int.TryParse(rawBuildNumber, out previousBuildNumber))
                {
                    previousBuildNumber = InitialBuildLabel – 1;
                }
                int newBuildNumber = previousBuildNumber + 1;
                // increment build number and write back to file
                bytes = Encoding.UTF8.GetBytes(newBuildNumber.ToString());
                fileStream.Seek(0, SeekOrigin.Begin);
                fileStream.Write(bytes, 0, bytes.Length);
                return newBuildNumber.ToString();
            }
        }
        private bool ShouldIncrementLabel(IntegrationSummary integrationSummary)
        {
            return integrationSummary == null || integrationSummary.Status == IntegrationStatus.Success || IncrementOnFailure;
        }
    }
}
This is how you can implement in ccnet.config under project-
         <sharedLabelFilePath>E:\Program Files\CruiseControl.NET\server\buildnumber.txt</sharedLabelFilePath>
Advertisements

Setting up Cruise Control . NET with TFS for ASP .NET Web Application

I have setup Cruise Control .NET from scratch very recently in integration with TFS 2010 for ASP .NET MVC3 application. I am writing down steps which I followed to set it up here –

1) Download Cruise Control .NET from this location and unzip it to use it.

2) Install .NET framework, IIS and NANT as well.

3) Add “WebDashboard” as an IIS application.

4) Update Dashboard.config file with admin password for CC dashboard.

<administrationPlugin password=”yourpassword” />

5) You can browse URL in IIS to check if dasboard is working fine. You can reach admin dashboard to install any required packages. For me loading http://localhost/ccnet/ViewFarmReport.aspx errored out for TCP connecton not made. It was happening because ccnet service was not running from “Server”. To make it run as background I used below VB script-

Set WshShell = CreateObject(“WScript.Shell”)WshShell.Run chr(34) & “C:\pathtoinstallfolder\Server\ccnet.exe” & Chr(34), 0Set WshShell = Nothing

(Run it with .vbs extension. Put above code and double click to execute it)

6) Create project in TFS 2010 and map it to local folder.

7) Edit “ccnet.config” to below in order to compile and publish website-

<cruisecontrol xmlns:cb=”urn:ccnet.config.builder”>

<project name=”Web Application” queue=”Q1″ queuePriority=”1″>

<sourcecontrol type=”vsts” autoGetSource=”true” applyLabel=”false”>

<server>TFS Server name till collection</server>

<project>Source Code path of TFS Project under collection</project>

<workingDirectory>Path to local working directory </workingDirectory>

<workspace>tfs server workspacename</workspace>

<cleanCopy>true</cleanCopy>

</sourcecontrol>

<tasks>

<msbuild>

<executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</executable>

<workingDirectory>Path to local working directory</workingDirectory>

<projectFile>MSBuild.xml</projectFile>

<buildArgs>/noconsolelogger /p:Configuration=Debug /v:diag</buildArgs>

<targets>BuildAndPublish</targets>

<timeout>150</timeout>

<logger>C:\CruiseControl.NET-1.6.7981.1\Server\ThoughtWorks.CruiseControl.MsBuild.dll</logger>

</msbuild>

</tasks>

<publishers>

<buildpublisher>

<sourceDir>Path to Site where we have published output with compilation</sourceDir>

<publishDir>Path to copy published site to IIS directory</publishDir>

<useLabelSubDirectory>false</useLabelSubDirectory>

<alwaysPublish>false</alwaysPublish>

</buildpublisher>

<xmllogger logDir=” path to logs” />

</publishers>

</project>

</cruisecontrol>

8) Create MSBuild.xml and put below code. I have customized this according to my web application under same TFS location which we are referring in same ccnet.config-

<Project DefaultTargets=”BuildAndPublish” xmlns=”http://schemas.microsoft.com/developer/msbuild/2003″&gt;

<PropertyGroup>

<SolutionPath>Path to source code or solution folder</SolutionPath>

<OutputPath>Path to output directory</OutputPath>

</PropertyGroup>

<ItemGroup><testwebapplicationBin Include=”$(SolutionPath)\testwebapplication\bin\**\*.*” /></ItemGroup>

<Target Name=”CleanTarget”>

<Message Text=”Removing all source files from $(OutputPath)” />

<RemoveDir Directories=”$(OutputPath)\_PublishedWebsites\” />

<RemoveDir Directories=”$(OutputPath)\” /></Target><Target Name=”BuildAndPublish”>

<MSBuild Projects=”$(SolutionPath)\testwebapplication.sln” Targets=”Clean;Build” />

<CallTarget Targets=”CleanTarget” />

<MSBuild Projects=”$(SolutionPath)\testwebapplication\testwebapplication.csproj” Targets=”_CopyWebApplication;_BuiltWebOutputGroupOutput” Properties=”OutDir=$(OutputPath)\” />

<Copy SourceFiles=”@(testwebapplicationBin)” DestinationFolder=”$(OutputPath)\_PublishedWebsites\testwebapplication\bin\%(RecursiveDir)” />

</Target>

</Project>

(Note: I have used testwebapplication everywhere because I created testwebapplication project in tfs 2010, change name according to your project)9) Go to Dashboard and Click Force Build to compile and deploy code in IIS.10) You might also need to install AJAX 1.0 or ASP .NET MVC 3.0 to make your website work in IIS. Also, you need to give “Users” as full access if you are using IIS 7.0 on Windows 7. If any dll reference is missing while running site in IIS, then you can change “Copy Local” to true from TFS 2010 Project to make it working.