Update-TFSBuildDefinition.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Author: Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 26-Apr-2016.

<#
.SYNOPSIS
    Update build definition
.EXAMPLE
    Update-TFSBuildDefinition -JsonFile BuildXYZ.json
 
    Update the build definition (create a new revision) using the data in the JSON file 'BuildXYZ'.
.NOTES
    Build definition property "revision" must point to the latest one in order for import to succeed.
    For this reason revision in the JSON file is ignored and remote revision is used instead.
#>

function Update-TFSBuildDefinition {
    [CmdletBinding()]
    param (
        [string] $JsonFile
    )
    check_credential

    if (!(Test-Path $JsonFile)) {throw "File doesn't exist: $JsonFile" }
    $json = gc $JsonFile -ea Stop | ConvertFrom-Json

    #Don't use id from the jsonfile, find by name on TFS
    $remote = Get-TFSBuildDefinition $json.name
    $json.id       = $remote.id
    $json.revision = $remote.revision
    Write-Verbose "Using remote build definition attributes: id $($json.id), revision $($json.revision)"

    $uri = "$proj_uri/_apis/build/definitions/$($json.id)?revision=$($remote.revision)&api-version=" + $global:tfs.api_version
    Write-Verbose "URI: $uri"

    $body   = $json | ConvertTo-Json -Depth 100
    $params = @{ Uri = $uri; Method = 'Put'; Body = $body; ContentType = 'application/json'  }
    invoke_rest $params
}