禁用TeamCity中构建步骤或从特定步骤开始运行的脚本

4
我想禁用 TeamCity 配置中的几个构建步骤,例如:
  • 我有一个名为 “DeploySoftware” 的部署配置。
  • 它有 10 个构建步骤(Run > “DB scripts”,“Run environment scripts”,“Deploy Web Service”,“Deploy Windows Service”,“Deploy This”,“Deploy That”等)。
  • 我运行它一次,在“Deploy This”步骤上失败。
  • 我想再次运行它,从“Deploy This”开始,或使用脚本禁用到该步骤之前的所有步骤。
我的某个配置有 30 个构建步骤,因此如果在第 28 步失败(并且我知道另一个运行很可能会成功),我想从第 28 步开始重新运行。否则,我需要花费 45 分钟来运行已经成功完成的步骤,然后才能运行需要运行的步骤。
我不需要脚本来运行构建(虽然这很好),也不需要在运行后更改配置(我预计将简单修改禁用脚本)。脚本可以是 PowerShell、C#、VB.NET、VBA、Ruby 等任何可以快速修改和运行的语言。
1个回答

6
以下脚本将禁用或启用从1 => x的构建步骤。
# -----------------------------------------------
# Build Step Disabler
# -----------------------------------------------
#
# Ver   Who                  When      What
# 1.0   Evolve Software Ltd  29-03-16  Initial Version

# Script Input Parameters
param (
    [ValidateNotNullOrEmpty()]
    [string] $RestEndPoint = $(throw "-RestEndPoint is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApiUsername = $(throw "-ApiUsername is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApiPassword = $(throw "-ApiPassword is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [int] $FailedStep = $(throw "-FailedStep is mandatory, please provide a value."),
    [bool] $Disable = $True
)

function Main() 
{
    $CurrentScriptVersion = "1.0"
    $ApiCredentials = New-Object System.Management.Automation.PSCredential($ApiUsername, (ConvertTo-SecureString $ApiPassword -AsPlainText -Force))

    $ApiCredentials_ForHeader = $ApiUsername + ":" + $ApiPassword
    $ApiCredentialsBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($ApiCredentials_ForHeader))

    $ApiCredentialsHeader = @{};
    $ApiCredentialsHeader.Add("Authorization", "Basic $ApiCredentialsBase64")

    Write-Host "================== Build Step Disabler - Version"$CurrentScriptVersion": START =================="

    # Log input variables passed in
    Log-Variables
    Write-Host

    # Get the steps into XML
    [System.Xml.XmlDocument]$stepsResponse = Api-Get "$RestEndPoint/steps"
    $currentStep = 1;

    do {
        try {
            [System.Xml.XmlElement]$step = $stepsResponse.steps.step[$currentStep-1]

            if (!$step.id)
            {
                Write-Output "Build step id not found - Exiting"
                Exit 1
            }

            $stepId = $step.id
            Api-Put "$RestEndPoint/steps/$stepId/disabled" ($Disable).ToString().ToLower()
            $currentStep++
        } 
        catch [System.Exception] {
            Write-Output $_
            Write-Output "Unable to configure the build steps correctly"
            Exit 1
        }
    } 
    while ($currentStep -le $FailedStep)

    Write-Host "================== Build Step Disabler - Version"$CurrentScriptVersion": END =================="
}

function Log-Variables
{
    Write-Host "RestEndPoint: " $RestEndPoint
    Write-Host "FailedStep: " $FailedStep
    Write-Host "Disable: " $Disable
    Write-Host "Computername:" (gc env:computername)
}

function Api-Get($Url)
{
    Write-Host $Url
    return Invoke-RestMethod -Headers $ApiCredentialsHeader -Credential $ApiCredentials -Uri $Url -Method Get -ContentType "application/xml" -TimeoutSec 30;
}

function Api-Put($Url, $Data)
{
    Write-Host $Url
    return Invoke-RestMethod -Headers $ApiCredentialsHeader -Credential $ApiCredentials -Uri $Url -Method Put -ContentType "text/plain" -Body $Data -TimeoutSec 30 -DisableKeepAlive;
}    

Main

用法:

这将禁用构建步骤1 => 5

script.ps1 "http://teamcity/httpAuth/app/rest/buildTypes/DeploySoftware" username password 5

这将启用构建步骤1 => 5

script.ps1 "http://teamcity/httpAuth/app/rest/buildTypes/DeploySoftware" username password 5 $false

希望这能帮到您。

谢谢,太好了。我不得不增加超时时间(非常慢/繁忙的服务器),但这个方法完美地解决了问题。 - grrrrinaldi
那是一份写得非常漂亮的脚本。 - ErikE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接