Search code examples
restapipowershellteamcity-8.0

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


I want to disable several build steps in a TeamCity configuration. For example:

  • 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.

One config I have has 30 build steps, so if it fails at Step 28 (and I know that another run will most likely work) I want to run it again starting with Step 28. Otherwise it's 45 minutes of running steps that have already successfully complete, before I get to the step that needs to be run.

I don't need the script to run the build (although that'd be nice), or change the config back once it's run (I expect that would be a simple tweak to the disable script).

The script could be PowerShell, C#, VB.NET, VBA, Ruby - pretty much anything that I can quickly amend and run.


Solution

  • The following script will disable or enable the build steps from 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
    

    Usage:

    This will disable the build steps 1 => 5

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

    This will enable the build steps 1 => 5

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

    Hope this helps