Merged PR 671092: Changes for build entity property for BXL telemetry.

Added buildEntity property to gather information about the build queue(CB env) and build pipeline(ADO env).
In CB this information is obtained from "cloudBuildQueue"  name passed via traceInfo
In ADO this information is obtained from the ADO predefined variable "SYSTEM_DEFINITIONID".
Added two unit test cases, one to test if the changes are effective in ADO environment.
The other is to test the changes for a CB environment and also to check if the user passed value overrides the ADO set value or not.
Used the name "buildEntity" to represent both build queue and build pipeline. But suggestions are welcomed.
Another thought here is that. Since we already have **cloudBuildQueue** as a part of the env string in the telemetry. Would it be enough to just add another property called **pipeline** to represent this information for ADO. That way we can avoid duplication of information if it is CloudBuild.

Related work items: #1948523
This commit is contained in:
Sahiti Chandramouli 2022-07-27 22:00:25 +00:00
Родитель 5578856a34
Коммит 00c7a87c3f
2 изменённых файлов: 81 добавлений и 7 удалений

Просмотреть файл

@ -17,6 +17,7 @@ namespace BuildXL.Utilities.Configuration
/// infra - identify the environment in which the build is run(CloudBuild, Azure DevOps).
/// org - identify the orgnization triggering the build.
/// codebase - identifies the code or product that's being built. This will typically be the name of the Git repository.
/// buildEntity - identifies the BuildEntity(queue/pipeline) used to build and deploy the codebase.
/// </remarks>
public class CaptureBuildInfo
{
@ -30,6 +31,19 @@ namespace BuildXL.Utilities.Configuration
/// </summary>
public const string OrgKey = "org";
///<summary>
/// Codebase - identifies the code or product that's being built. This will typically be the name of the Git repository.
/// It will vary in other source control mechanisms.
/// </summary>
private const string CodeBaseKey = "codebase";
///<summary>
/// BuildEntity - identifies the build structure used to build the code.
/// In CloudBuild this build structure refers to a build queue.
/// In ADO this build structure refers to a build pipeline
/// </summary>
private const string BuildEntityKey = "buildEntity";
/// <summary>
/// ADO predefined variable to obtain the URI of the ADO organization.
/// In CB the same environment variable is set in the GenericBuildRunner.
@ -43,16 +57,20 @@ namespace BuildXL.Utilities.Configuration
/// </summary>
public const string AdoEnvVariableForInfra = "BUILD_DEFINITIONNAME";
///<summary>
/// Codebase - identifies the code or product that's being built. This will typically be the name of the Git repository.
/// It will vary in other source control mechanisms.
/// </summary>
private const string CodeBaseKey = "codebase";
/// <summary>
/// ADO pre-defined environment variable to obtain the name of the repository triggering the build.
/// </summary>
public static readonly string AdoPreDefinedVariableForCodebase = "BUILD_REPOSITORY_NAME";
public const string AdoPreDefinedVariableForCodebase = "BUILD_REPOSITORY_NAME";
/// <summary>
/// ADO pre-defined environment variable to obtain the id of the pipeline which was used to build the code.
/// </summary>
public const string AdoPreDefinedVariableForBuildEntity = "SYSTEM_DEFINITIONID";
/// <summary>
/// Represents the cloudbuildqueue property which is passed as a cmd line argument in a CB environment.
/// </summary>
public const string CloudBuildQueue = "cloudBuildQueue";
/// <summary>
/// This is the primary method in the class which is called by ComputeEnvironment(), to capture the build properties.
@ -86,6 +104,15 @@ namespace BuildXL.Utilities.Configuration
}
}
if (!traceInfoProperties.ContainsKey(BuildEntityKey))
{
string buildEntityPropertyValue = GetBuildEntity(traceInfoProperties);
if (!string.IsNullOrEmpty(buildEntityPropertyValue))
{
traceInfoProperties.Add(BuildEntityKey, buildEntityPropertyValue);
}
}
return traceInfoProperties;
}
@ -151,5 +178,26 @@ namespace BuildXL.Utilities.Configuration
return null;
}
/// <summary>
/// This method is used to set the build property called BuildEntity in the EnvString for telemetry purpose in an ADO environment.
/// In CB env the information about the buildqueue is passed via traceInfo. This info is used to set the property buildEntity to the buildQueue name.
/// This method captures the required information from the ADO pre-defined variable "System_DefinitionId"
/// This variable gives the id of the pipeline that is used to build the codebase.
/// </summary>
/// <returns></returns>
private static string GetBuildEntity(Dictionary<string, string> traceInfoProperties)
{
if (traceInfoProperties.ContainsKey(CloudBuildQueue))
{
return traceInfoProperties.GetValueOrDefault(CloudBuildQueue);
}
if (Environment.GetEnvironmentVariables().Contains(AdoPreDefinedVariableForBuildEntity))
{
return Environment.GetEnvironmentVariable(AdoPreDefinedVariableForBuildEntity);
}
return null;
}
}
}

Просмотреть файл

@ -158,6 +158,32 @@ namespace Test.BuildXL.Utilities.Configuration
XAssert.IsFalse(AssertEnvStringContainsTelemetryEnvProperty("codebase=TestADO", envString));
}
/// <summary>
/// This test is to check if the "buildEntity" property is set to the pipeline id in an ADO env when "SYSTEM_DEFINITIONID" is present as an environment variable.
/// </summary>
[Fact]
public static void TestBuildEntityPropertyADO()
{
ICommandLineConfiguration configuration = new CommandLineConfiguration();
string[] envString = ComputeEnvBlockForTesting(configuration, CaptureBuildInfo.AdoPreDefinedVariableForBuildEntity, EnvVarExpectedValue);
XAssert.IsTrue(AssertEnvStringContainsTelemetryEnvProperty("buildEntity=TestADO", envString));
}
/// <summary>
/// This test is to check if the "buildEntity" property has been to set the build queue passed via traceInfo in the CB environment.
/// This test also tests the scenario when the buildEntity property has been passed via traceInfo in the "CloudBuild" environment and the presence of the environment variable "SYSTEM_DEFINITIONID".
/// In this case the buildEntity property value obtained from the traceInfo argument should be set in the envString for buildEntity. The ado defined value should be overriden by the traceInfo buildentity value.
/// </summary>
[Fact]
public void TestBuildEntityPropertyCloudBuild()
{
string traceInfoArgs = "/traceInfo:cloudBuildQueue=TestCB";
ICommandLineConfiguration configuration = AddTraceInfoArguments(traceInfoArgs);
string[] envString = ComputeEnvBlockForTesting(configuration, CaptureBuildInfo.AdoPreDefinedVariableForBuildEntity, EnvVarExpectedValue);
XAssert.IsTrue(AssertEnvStringContainsTelemetryEnvProperty("buildEntity=TestCB", envString));
XAssert.IsFalse(AssertEnvStringContainsTelemetryEnvProperty("buildEntity=TestADO", envString));
}
/// <summary>
/// This is a helper method to avoid memory leaks with respect to the environment variables that are tested
/// Check if there any duplicates are present in the environment string.