【问题标题】:Script to disable build steps in TeamCity or start run at a specific step用于禁用 TeamCity 中的构建步骤或在特定步骤开始运行的脚本
【发布时间】:2016-07-16 16:51:45
【问题描述】:

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

  • 我有一个名为 DeploySoftware 的部署配置。
  • 它有 10 个构建步骤(运行 >DB 脚本运行环境脚本部署 Web 服务部署 Windows 服务、部署这个部署那个等)。
  • 我运行了一次,但在 Deploy This 上失败了。
  • 我想从 Deploy This 开始再次运行它,使用脚本禁用之前的所有步骤。

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

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

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

【问题讨论】:

    标签: rest api powershell teamcity-8.0


    【解决方案1】:

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

    希望对你有帮助

    【讨论】:

    • 谢谢,太好了。我不得不增加超时(非常慢/繁忙的服务器),但这非常有效。
    • 这是一个写得很漂亮的脚本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2015-04-19
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多