【问题标题】:Clone VSTS Build Definition in C#在 C# 中克隆 VSTS 构建定义
【发布时间】:2019-06-28 16:11:52
【问题描述】:

我正在使用 BuildHttpClient 的 .GetDefinitionAsync 和 .CreateDefinitionAsync 来克隆 VSTS 构建定义。这工作正常,但我想在项目的根文件夹以外的不同文件夹中创建构建定义。我可以通过“管理文件夹”链接网络添加文件夹,但如何以编程方式执行此操作?

我尝试了以下方法:

    buildDef = JsonConvert.DeserializeObject<BuildDefinition>(json);
    buildDef.BuildNumberFormat = buildNumFormat;
    buildDef.Name = newBuildDefName;

    Uri tfsURI = new Uri(CollectionReleaseURL);
    buildDef.Url = tfsURI.ToString();
    await buildClient.CreateDefinitionAsync(buildDef, project);

Collection 发布的地方是路径: CollectionReleaseURL = @"http://my.tfs.server8080/tfs/DefaultCollection/Project/Product/Release";

但是当我运行程序时,它只是将新的构建定义放在以下文件夹中: @"http://my.tfs.server8080/tfs/DefaultCollection/Project"

如何将构建定义克隆到 ../Product/Release 文件夹?

更新:我正在使用以下内容来克隆构建定义,但它没有复制构建步骤!有谁知道这是为什么?

private static async Task CloneBuildDefAsync(int buildDefId, string newBuildDefName, string buildNumFormat, string sourceControlPath, string URLpath)
{
    var buildClient = createClient();
    var buildDef = (await buildClient.GetDefinitionAsync(project, buildDefId)) as BuildDefinition;

    buildDef.Project = null;

    var json = JsonConvert.SerializeObject(buildDef);
    json = json.Replace(sourceControlMainline, sourceControlPath);
    buildDef = JsonConvert.DeserializeObject<BuildDefinition>(json);

    buildDef.BuildNumberFormat = buildNumFormat;
    buildDef.Name = newBuildDefName;
    buildDef.Path = URLpath;

    await buildClient.CreateDefinitionAsync(buildDef, project);
}

似乎复制了除了构建步骤之外的所有内容,其他所有内容都被克隆并完美更新:

【问题讨论】:

    标签: c# tfs build azure-devops tfs-sdk


    【解决方案1】:

    您想将Path property 设置为您想要的文件夹。

    PowerShell 中的克隆示例:

    $uri = 'https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}'
    
    $result = Invoke-RestMethod -Method Get -Uri $uri -UseDefaultCredentials
    $result.path = '\NewFolder\Location'
    $result.name = "Testing"
    
    $body = $result | ConvertTo-Json -Depth 7
    
    Invoke-RestMethod -Method POST -uri 'https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=4.0' -UseDefaultCredentials -Body $body -ContentType 'application/json'
    

    像这样创建一个构建文件夹结构:

    【讨论】:

    • 谢谢马特 - 我最终使用了你的解决方案。我正在使用它来自动在源代码控制中创建一个分支,并将 Mainlines 构建定义克隆到分支,还将创建一个“创建发布”构建定义,它将调用这个 powershell 脚本。奇迹般有效。现在我将创建一个 powershell 脚本来创建源代码控制分支,因为我也在 c# 中编写了它。
    【解决方案2】:

    如果您使用 Microsoft.TeamFoundationServer.Client,您可以只设置路径:

    BuildDefinition cpBuild = BuildClient.GetDefinitionAsync(teamProjectName, 8).Result;
    
    BuildDefinition build = new BuildDefinition();
    build.Path = "New Build Folder";
    build.Name = "new Build";
    build.BuildNumberFormat = cpBuild.BuildNumberFormat;
    build.Repository = cpBuild.Repository;
    build.Process = cpBuild.Process;
    var newBuild = BuildClient.CreateDefinitionAsync(build, teamProjectName).Result;
    

    【讨论】:

    • 当我尝试这个时,我在得到以下结果之前已经尝试过:'BuildDefinition' 不包含'Path' 的定义并且没有'Path' 的扩展......
    • 我用的是最新版本的Microsoft.TeamFoundationServer.Client15.131.1
    • 太棒了,谢谢 Shamrai。一旦我将 .Client 从 14.89.0 升级到 15.131.1,我就可以设置路径...谢谢。
    • 我已经更新了我的问题 - 你知道为什么没有复制构建步骤吗?
    • 您的示例对我来说很好(除了我不使用 json 替换)。但是我在我的 json 中找不到构建部分,我看到了 Process 部分。 Microsoft.TeamFoundation.Build.WebApi.BuildDefinition 也不包含构建成员。
    【解决方案3】:

    这是我用来完成此任务的方法:

    https://gist.github.com/HockeyJustin/c1cb4e543806c16cab6e2c2322f5d830

    $thisBuildDef.Name = $Clone_Name
    $thisBuildDef.path = $BuildDefURL  # Relative to the Project name; like "Release/2019"
    $thisBuildDef.buildNumberFormat = $BuildNumberFormat
    
    # Update source control path to new branch
    $defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100
    $defAsJson = $defAsJson.Replace($sourceControlMainline, $sourceControlBranch)
    
    $Uri = "$(TFSCollection)/$(TFSProject)/_apis/build/definitions?api-version=2.0"
    
    $newBuildDef = Invoke-RestMethod -Uri $Uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop
    

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      相关资源
      最近更新 更多