用于在 TeamCity 中禁用构建步骤或在特定步骤启动 运行 的脚本

Script to disable build steps in TeamCity or start run at a specific step

我想在 TeamCity 配置中禁用多个构建步骤。 例如:

  • I have a deploy configuration called DeploySoftware.
  • It has 10 build steps (Run >DB scripts, Run environment scripts, Deploy Web Service, Deploy Windows Service, Deploy This, Deploy That, etc).
  • I run it once and it fails on Deploy This.
  • I want to run it again starting with Deploy This, OR disable all the previous steps to that one by using a script.

我有一个配置有 30 个构建步骤,所以如果它在第 28 步失败(而且我知道另一个 运行 很可能会工作)我想 运行 从第 Step 开始28. 否则,在我到达需要 运行.

的步骤之前,已经成功完成了 运行ning 个步骤的 45 分钟

我不需要脚本来 运行 构建(尽管那样很好),或者在它 运行 后更改配置(我希望这将是一个简单的调整到禁用脚本)。

脚本可以是 PowerShell、C#、VB.NET、VBA、Ruby - 几乎所有我可以快速修改的内容和 运行.

以下脚本将禁用或启用从 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

希望对您有所帮助