代码之家  ›  专栏  ›  技术社区  ›  Shayki Abramczyk Cece Dong - MSFT

从TFS签入将当前代码生成到另一个分支

  •  0
  • Shayki Abramczyk Cece Dong - MSFT  · 技术社区  · 6 年前

    我们有TFS 2017,我们使用TFVC。

    我们有vNext构建定义,其中包含来自多个分支的许多源代码映射(构建需要所有代码)。

    在构建结束时(如果他成功地通过了),我想从代理文件夹中获取所有本地代码(使用本地文件夹结构),并将所有代码签入到分离分支(例如“release branch”),并保留文件夹结构。

    • 创建新文件夹
    • 在此文件夹中创建新工作区
    • 将文件夹映射到“release branch”

    需要考虑的一点是:我们有许多构建定义,我们希望为所有构建都这样做,因此对于每个构建,我们希望在“release branch”上创建一个文件夹,并将代码签入其中。所以我需要检查文件夹是否存在,如果是-签入,是不是-创建一个文件夹并签入。

    我该怎么做(tf.exe?)

    更新:

    我成功了 tf.exe

    tf.exe文件

    1 回复  |  直到 6 年前
        1
  •  0
  •   Shayki Abramczyk Cece Dong - MSFT    6 年前

    我通过在每个成功的构建中创建一个分支来解决这个问题(不是最好的方法,但不是CI,我们没有很多构建)。

    因为 tf barnch

    Param(
    [string]$path
    )
    $newCodeFolderPath = "$($env:Agent_BuildDirectory)\newCode"
    $tempWorkspacePath =  "$($env:Agent_BuildDirectory)\tempWorkspace"
    
    New-Item -Path $newCodeFolderPath -ItemType directory
    
    Copy-Item -Path "$($env:Build_SourcesDirectory)/*" -Recurse -Destination $newCodeFolderPath 
    
    New-Item -Path $tempWorkspacePath -ItemType directory
    
    cd $tempWorkspacePath 
    
    $tfExe = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe"
    
    & $tfExe workspace /collection:{TfsCollection} /new "TempWorkspace" /noprompt
    
    & $tfExe workfold "$($path)/Release/$($env:Build_BuildNumber)" $tempWorkspacePath 
    
    Copy-Item -Path "$($newCodeFolderPath)/*" -Recurse -Destination $tempWorkspacePath 
    
    & $tfExe add * /recursive /noignore
    
    & $tfExe checkin /recursive /comment:"from vNext build"
    
    & $tfExe workspace /delete /collection:{TfsCollection} "Tempworkspace"
    
    cd c:/
    Remove-Item -Path $newCodeFolderPath -Force -Recurse
    Remove-Item -Path $tempWorkspacePath -Force -Recurse
    
    $releaseBranchPath = "$($path)/Release/$($env:Build_BuildNumber)
    Write-Host ("##vso[task.setvariable variable=releaseBranchPath;]$releaseBranchPath")
    

    在build PowerShell任务中,我传递 $path 变量,例如 $/MyProject/V1/Name

    之后,我需要将文件夹转换为一个分支,因此我用C#编写了一个小工具(您需要TFS API库):

    try
    {
      string collectionUrl = {myCollection};
      string releaseBranchPath = Environment.GetEnvironmentVariable("releaseBranchPath");
      var tp = TfsTeamProjectCollection(new Uri (collectionUrl));
      var versionControl = tp.GetService<VersionControlServer>();
      versionControl.CreateaBranchPbject(new BranchProperties(new ItemIdentifier(releaseBranchPath)));
    }
    catch (Execption e)
    {
      Console.WriteLine(e.Message);
    }
    

    我编译了它然后运行 .exe 在PowerShell任务之后使用命令行任务。