Who requested this build?
In certain scenarios you may want to take different actions based on user who have requested the build (or may simply want to report that in log file). Though the Team Build stores the name of the user who has requested the build (and this is shown in the UI), the value is not passed to the build process.
However, do not get dishearten. There is a workaround - use the task code given below to get the RequestedBy value.
Steps to use -
- Copy/paste the code given below in GetRequestedBy.cs file.
- Build the code given below using following command line -
csc /r:Microsoft.Build.Framework.dll;Microsoft.Build.Utilities.dll;%SystemDrive%\Windows\assembly\GAC_32\Microsoft.TeamFoundation.Client\8.0.0.0__b03f5f7f11d50a3a\Microsoft.TeamFoundation.Client.dll;%SystemDrive%\Windows\assembly\GAC_32\Microsoft.TeamFoundation.Build.Common\8.0.0.0__b03f5f7f11d50a3a\Microsoft.TeamFoundation.Build.Common.dll; /t:library /out:Microsoft.TeamFoundation.Build.Samples.GetRequestedBy.dll GetRequestedBy.cs - Checkin the Microsoft.TeamFoundation.Build.Samples.GetRequestedBy.dll built above in the build type folder (i.e. the same location as TFSBuild.proj file).
- Add following line after the Project tag (at the start of TFSBuild.proj).
<UsingTask TaskName="Microsoft.TeamFoundation.Build.Samples.GetRequestedBy" AssemblyFile="$(MSBuildProjectDirectory)\Microsoft.TeamFoundation.Build.Samples.GetRequestedBy.dll" /> - Override BeforeEndToEndIteration in TFSBuild.proj to get the RequestedBy value.
<Target Name="BeforeEndToEndIteration">
<GetRequestedBy
TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
BuildUri="$(BuildURI)" >
<Output TaskParameter="RequestedBy" PropertyName="RequestedBy" />
</GetRequestedBy>
<Message Text="$(RequestedBy)" />
</Target> - Now, you can use RequestedBy property anywhere you want.
The complete solution is also available as attachment to this blog.
Thanks,
Gautam
/**===========================================================================
* File: GetRequestedBy.cs
* ----------------------------------------------------------------------------
*
* This file is part of the Microsoft Visual Studio Team System Samples.
*
* Copyright(C) 2004 Microsoft Corporation. All rights reserved.
*
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
* ============================================================================
*/
using System;
using System.Globalization;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.TeamFoundation.Build.Proxy;
using Microsoft.TeamFoundation.Client;
namespace Microsoft.TeamFoundation.Build.Samples
{
/// <summary>
/// This task will update the build number and Drop location on the server
/// Inputs are the Tfs name, build Uri, build number, and drop location.
/// errors are reported as message and get logged.
/// </summary>
public class GetRequestedBy: Task
{
[Required]
public string TeamFoundationServerUrl
{
set
{
m_tfsUrl = value;
}
get
{
return m_tfsUrl;
}
}
[Required]
public string BuildUri
{
set
{
m_buildUri= value;
}
get
{
return m_buildUri;
}
}
[Output]
public string RequestedBy
{
set
{
m_requestedBy = value;
}
get
{
return m_requestedBy;
}
}
override public bool Execute()
{
// Output parameters - can be helpful in debugging
string message = string.Format(CultureInfo.InvariantCulture,
"GetRequestedBy TeamFoundationServerUrl={0}, BuildUri={1}",
TeamFoundationServerUrl, BuildUri);
Log.LogMessage(MessageImportance.Normal, message);
try
{
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(m_tfsUrl);
BuildStore buildStore = (BuildStore)tfs.GetService(typeof(BuildStore));
RequestedBy = buildStore.GetBuildDetails(m_buildUri).RequestedBy;
}
catch (Exception ex)
{
// log the error
Log.LogError(ex.Message);
return false;
}
return true;
}
private string m_tfsUrl;
private string m_buildUri;
private string m_requestedBy;
}
}