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

时间:2016-03-29 08:27:59

标签: rest api powershell teamcity-8.0

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

  
      
  • 我有一个名为 DeploySoftware 的部署配置。
  •   
  • 它有10个构建步骤(运行> 数据库脚本运行环境脚本部署Web服务部署Windows服务部署此部署等。)
  •   
  • 我运行一次,但部署此
  • 失败   
  • 我想从部署此开始再次运行它,使用脚本禁用之前的所有步骤。
  •   

我有一个配置有30个构建步骤,所以如果它在步骤28失败(我知道另一个运行很可能会工作)我想从步骤28开始再次运行它。否则它是45分钟的运行步骤在我到达需要运行的步骤之前已经成功完成。

我不需要脚本来运行构建(尽管这很好),或者在运行后更改配置(我希望这会对禁用脚本进行简单的调整)。

脚本可以是PowerShell,C#,VB.NET,VBA,Ruby - 几乎任何我可以快速修改和运行的东西。

1 个答案:

答案 0 :(得分:5)

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

希望这有帮助