在 tfs 构建中获取构建代理文件夹路径

Get build agent folder path in tfs build

如何使用批处理文件访问 TFS 构建代理文件夹路径?

我正在从构建工作流调用 runscript 工具(调用 windows 批处理文件)。 我尝试使用环境变量 BUILD_REPOSITORY_LOCALPATH ($(BUILD_REPOSITORY_LOCALPATH), $env:BUILD_REPOSITORY_LOCALPATH) 但它们没有给出任何结果。 在这方面需要一些帮助。

您要查找的变量是TF_BUILD_SOURCESDIRECTORY。请参考XAML build documentation.

TF_BUILD_SOURCESDIRECTORY:构建代理工作目录的源子目录。该目录包含您的源代码。例如:C:\Build\BuildBot3\CoolApp\CIBuild\src.

如果你想得到C:\TFS_Build\src\V9,它只是本地路径:你已经将服务器路径映射到你机器上的路径。没有任何内置的 TF_BUILD environment variables 可以满足您的要求。

您可以使用TFS API获取相关信息,首先获取构建服务器工作空间的工作空间信息,然后执行获取选项,示例代码供您参考:

// Get the workspace information for the build server's workspace
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(sourcesDirectory);

// Get the TFS Team Project Collection information from the workspace cache
// information then load the TFS workspace itself.
var server = new TfsTeamProjectCollection(workspaceInfo.serverUri);
var workspace = workspaceInfo.GetWorkspace(server);

Once you have a workspace, you can query it for the path mappings. It will do the necessary translation from server to local path based on your workspace mappings. For example:

workspace.GetServerItemForLocalItem("C:\TFS_Build\src\V9");

and

workspace.GetLocalItemForServerItem("$/DEV/V9");

This mechanism will only work, however, if your build definition actually sets up the workspace to include these files.

更多细节请参考这个类似的问题:


来自 OP 的更新:

From sourceDirectory folder i drill down to find my solution project file, the path containing my solution project is the local directory path i need

我为此使用了另一种解决方法,而不是通过工作区获取。我从 sourceDirectory 文件夹下钻找到我的解决方案项目文件,包含我的解决方案项目的路径是我需要的本地目录路径

我从批处理文件调用我的 exe 并将 %TF_BUILD_SOURCESDIRECTORY% 作为参数传递。

string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory) 
// targetdirectory would be the input parameter from batch file`

for (int i = 0; i < subdirectoryEntries.Length ; i++)
{
     // My root folder always contains a specific folder with name MyFolder 
     // and a file Myfile.sln

     if (subdirectoryEntries[i].ToString().ToLower().Contains(@"MyFolder")) 
     {
          Console.Writeline("My source code path is " + targetDirectory);
     }   

     //Similarly I check for Myfile.sln and then get my path.             
}

这可能是一种非常粗糙的方式,这对我有用。