CaptainBadgers
CaptainBadgers

Reputation: 3

How to get TFS build steps with the REST api?

I am currently automating TFS/VSTS for the lab module I am working on, AutomatedLab. So far, I am progressing quite well. All calls to the REST API are done in PowerShell with Invoke-RestMethod.

The one thing I am not figuring out is: How can I get a list of build steps that I can use in my builds? The API documentation (https://www.visualstudio.com/en-us/docs/integrate/api/overview) contains nothing that helped me. I also did not find anything related to my question on StackOverflow (assuming that I used the correct search terms).

While I can add a new build definition I cannot figure out how to properly add build steps like Run Script, Publish Test Results and so on.

My research so far:

Why do I need those task guids? To create new build definitions like it is documented at https://www.visualstudio.com/en-us/docs/integrate/api/build/definitions#create-a-build-definition

My code looks like this, and I have no properly automated way of inserting 1-n build steps:

function New-TfsBuildDefinition
{
    [CmdletBinding(DefaultParameterSetName = 'Cred')]
    param
    (
        [Parameter(Mandatory)]
        [string]
        $InstanceName,

        [Parameter(Mandatory)]
        [string]
        $CollectionName,

        [ValidateRange(1, 65535)]
        [uint32]
        $Port,

        [ValidateSet('1.0', '2.0')]
        [Version]
        $ApiVersion = '2.0',

        [Parameter(Mandatory)]
        [string]
        $ProjectName,

        [Parameter(Mandatory)]
        [string]
        $DefinitionName,

        [string]
        $QueueName,

        [switch]
        $UseSsl,

        [Parameter(Mandatory, ParameterSetName = 'Cred')]
        [pscredential]
        $Credential,

        [Parameter(Mandatory, ParameterSetName = 'Pat')]
        [string]
        $UserName,

        [Parameter(Mandatory, ParameterSetName = 'Pat')]
        [string]
        $PersonalAccessToken
    )

    $requestUrl = if ($UseSsl) {'https://' } else {'http://'}
    $requestUrl += '{0}/{1}/_apis/build/definitions?api-version={2}' -f $InstanceName, $CollectionName, $ApiVersion.ToString(2)

    if ( $Port )
    {
        $requestUrl += '{0}{1}/{2}/_apis/build/definitions?api-version={3}' -f $InstanceName, ":$Port", $CollectionName, $ApiVersion.ToString(2)
    }

    if ( $QueueName )
    {
        $parameters = Sync-Parameter -Command (Get-Command Get-TfsAgentQueue) -Parameters $PSBoundParameters
        $parameters.Remove('ApiVersion') # preview-API is called
        $queue = Get-TfsAgentQueue @parameters

        if (-not $queue)
        {
            $parameters = Sync-Parameter -Command (Get-Command New-TfsAgentQueue) -Parameters $PSBoundParameters
            $parameters.Remove('ApiVersion') # preview-API is called
            New-TfsAgentQueue @parameters
        }
    }
    else
    {
        $queue = Get-TfsAgentQueue | Select-Object -First 1
    }

    $buildDefinition = @{
        "name"       = $DefinitionName
        "type"       = "build"
        "quality"    = "definition"
        "queue"      = @{
            "id" = $queue.id
        }
        "build"      = @(
              # ABBREVIATED! I WOULD LIKE TO
              # ADD MY BUILD STEPS HERE LIKE I AM DOING
              # MANUALLY
        )
    }
}

Upvotes: 0

Views: 1413

Answers (1)

gvee
gvee

Reputation: 17161

It's not documented; but it's there!

https://<foo>.visualstudio.com/_apis/distributedtask/tasks?visibility%5B%5D=Build

Don't forget that you can use things like Chrome Developer Tools to see what requests are being made ;-)

Upvotes: 3

Related Questions