简体   繁体   中英

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:

  • I can get a list of my build definitions. Those definitions have the property url which I can use to request the JSON data describing my build steps, eg http://dsc1tfs1:8080/tfs/AutomatedLab/1900e200-5238-4956-816c-b29c50163f93/_apis/build/Definitions/1
  • These build steps were created manually and need to be automated
  • The build steps from the aforementioned URL contain all the info I need

     enabled : True continueOnError : False alwaysRun : False displayName : PowerShell Script timeoutInMinutes : 0 task : @{id=e213ff0f-5d5c-4791-802d-52ea3e7be1f1; versionSpec=*; definitionType=task} inputs : @{scriptType=filePath; scriptName=initiate.ps1; arguments=-filename build; workingFolder=; failOnStandardError=true} enabled : True continueOnError : False alwaysRun : False displayName : Publish Test Results **/TEST-*.xml timeoutInMinutes : 0 task : @{id=0b0f01ed-7dde-43ff-9cbb-e48954daf9b1; versionSpec=*; definitionType=task} inputs : @{testRunner=NUnit; testResultsFiles=**/TEST-*.xml; mergeTestResults=false; testRunTitle=; platform=; configuration=; publishRunAttachments=true} enabled : True continueOnError : False alwaysRun : False displayName : Copy Publish Artifact: Deploy timeoutInMinutes : 0 task : @{id=1d341bb0-2106-458c-8422-d00bcea6512a; versionSpec=*; definitionType=task} inputs : @{CopyRoot=$(Build.ArtifactStagingDirectory)\\; Contents=initiate.ps1 **\\deploy.ps1 **\\Acceptance\\** **\\Integration\\**; ArtifactName=Deploy; ArtifactType=Container; TargetPath=\\\\my\\share\\$(Build.DefinitionName)\\$(Build.BuildNumber)} 
  • However, I cannot list all available build steps. I want those task guids!

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
        )
    }
}

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 ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM