【问题标题】:Set build template for all build definitions为所有构建定义设置构建模板
【发布时间】:2014-06-17 22:22:41
【问题描述】:

我正在尝试使用 powershell 来利用 TFS API 来修改一些构建定义。我正在尝试更新定义以使用特定的构建模板。到目前为止,我已经能够将此构建模板设置为我的默认模板,但我似乎无法弄清楚如何使用这个新模板更新构建定义。

非常感谢任何帮助。这是我到目前为止的脚本:

$TFS2013_Server = "http://localhost:8080/tfs"
$teamProject = "OOTB_Scrum_2013.2"
$serverPath = "$/OOTB_Scrum_2013.2/BuildProcessTemplates/BuildProcessSource/Templates/Custom Template.xaml"

# VS 2013
$tfsClientVersion = ", Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($TFS2013_Server) 

$tfs.EnsureAuthenticated()
if (!$tfs.HasAuthenticated)
{
Write-Host "Failed to authenticate to TFS"

exit
}
Write-Host "Connected to Team Foundation Server [" $TFS2013_Server "]"

$versionControl =     $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]) 
$buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer]) 

$templates = $buildServer.QueryProcessTemplates($teamProject);

        foreach ($pt in $templates)
        {
            if ($pt.TemplateType -eq "Default")
            {
                Write-Host "Current Template Type: $($pt.TemplateType)"
                #Write-Host "Current Server Path: $($pt.serverPath)"
                $pt.TemplateType = "Custom"
                $pt.Save()

                Write-Host "New Template Type: $($pt.TemplateType)"
                #Write-Host "New Server Path: $($pt.serverPath)"                    
            }
        }

        foreach ($pt in $templates)
        {
            if ($pt.ServerPath -eq $serverPath)
            {
                Write-Host "Template found."
                Write-Host "Changing type for the template to Default."
                $pt.TemplateType = "Default"
                $pt.Save()
                #return
            }

        }


        foreach ($def in $defs)
        {
        Write-Host "Current Server Path: $($def.Process.ServerPath)..." 
        $def.Process.ServerPath = $customBuildTemplate
        $def.Save()

        Write-Host "New Server Path: $($def.Process.ServerPath)..."
        }

【问题讨论】:

    标签: powershell tfs tfsbuild


    【解决方案1】:

    你的代码应该是这样的

    $templateName = [IO.Path]::GetFileName($newTemplateServerPath)
    $processTemplate =
            $Buildserver.QueryProcessTemplates($TeamProject) |
            where { [IO.Path]::GetFileName($_.ServerPath) -eq $templateName }
    if (!$processTemplate)
    {
        # build process template missing at destination, use default
        $processTemplate =
                $Buildserver.QueryProcessTemplates($TeamProject) |
                where { $_.TemplateType -eq [Microsoft.TeamFoundation.Build.Client.ProcessTemplateType]::Default }
    }#if
    $buildDefinitionToChange.Process = $processTemplate
    $buildDefinitionToChange.Save()
    

    【讨论】:

    • 好的,所以按照你的例子,我添加了这个,它似乎正在工作。我不认为我错过了什么? $defs = $Buildserver.QueryBuildDefinitions($TeamProject) foreach ($def in $defs) { $def.Process = $processTemplate Write-Host "正在保存构建定义:$($def.Name)..." $def.Save () Write-Host "$($def.Name) 已保存。" }
    • 感谢您的帮助,朱利奥!
    猜你喜欢
    • 2017-03-26
    • 2016-06-07
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多