-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTFS_PauseBuildsPerEnvironment.ps1
More file actions
151 lines (133 loc) · 5.7 KB
/
TFS_PauseBuildsPerEnvironment.ps1
File metadata and controls
151 lines (133 loc) · 5.7 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<#
.DESCRIPTION
Gets all build definitions for specified environment name,
enables or disables the CI triggers in order to prevent builds from
being queued during demo's.
.PARAMETER Action
Enable or Disable build definition CI triggers
.PARAMETER EnvironmentName
Name of environment to disable/enable builds for
.PARAMETER PAT
Personal Access token. It's recommended to use a service account and pass via encrypted build definition variable.
.Notes
-These parameters are outside of functions in order to be passed by TFS build definition variables
-Triggers aren't actually "disabled", just flipped Include/Exclude on path filter
#>
[CmdletBinding()]
Param(
[Parameter(Position=0,Mandatory)]
[ValidateSet("Enable","Disable")]
[string]$script:Action,
[Parameter(Position=1,Mandatory)]
[ValidateSet("Environment1","Environment2","Environment3")] # Case is ignored by default
[string]$script:EnvironmentName,
[Parameter(Position=2,Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$script:PAT
)
# Base64-encodes the Personal Access Token (PAT) appropriately
# This is required to pass PAT through HTTP header
$script:User = "" # Not needed when using PAT, can be set to anything
$script:Base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$PAT)))
Function Get-BuildDefinitionsByEnvironment {
<#
.OUTPUT
DefinitionIDs
#>
[cmdletbinding()]
Param(
)
Try{
# https://docs.microsoft.com/en-us/rest/api/vsts/build/definitions/get
[uri] $script:Uri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + $env:SYSTEM_TEAMPROJECT + "/_apis/build/definitions?type=build&name=*$EnvironmentName*"
# Invoke the REST call and capture the response
$Response = Invoke-RestMethod -Uri $Uri `
-Method Get `
-Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)} `
"Get-BuildDefinitionsByEnvironment returned response: $Response"
"Found build definitions:"
$Response.value.name
$script:DefinitionIDs = $Response.value.id # Scope variable to "script" to be used in other functions
}
Catch{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
$responseBody
Exit 1
}
}
Function Update-BuildDefinitionTriggerPath {
[cmdletbinding()]
Param(
)
Try{
# https://docs.microsoft.com/en-us/rest/api/vsts/build/definitions/update
[uri] $script:Uri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + $env:SYSTEM_TEAMPROJECT + "/_apis/build/definitions/$DefinitionID" + "?api-version=3.0"
# Get definition to update, use response for json body to update definition
$Definition = Invoke-RestMethod -Uri $Uri `
-Method Get `
-Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)}
If($Definition.triggers.pathFilters){
$DefinitionTriggerPaths = $Definition.triggers.pathFilters
}
Else{
"No path filter, moving on to next definition"
continue # move to next definition in foreach loop
}
# Convert response to JSON to be used in Put below
$Definition = $Definition | ConvertTo-Json -Depth 100
ForEach($Path in $DefinitionTriggerPaths){
# Replace first character in path filter: + include, - exclude
# Example: +$/ED/SCM
Switch($Action.ToLower()){
"enable" {
$PathAfter = $Path.Replace("-$/","+$/")
}
"disable" {
$PathAfter = $Path.Replace("+$/","-$/")
}
default {
Write-Error "Not a valid action. Actions available: Enable, Disable"
}
}
# replace old trigger path with new
$Definition = $Definition.Replace("$Path","$PathAfter")
}
# Use updated response to update definition
$UpdatedDefinition = Invoke-RestMethod -Uri $Uri `
-Method Put `
-ContentType application/json `
-Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)} `
-Body $Definition
"Trigger updated:"
$UpdatedDefinition.triggers
}
Catch{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
$responseBody
Exit 1
}
}
Try
{
"Getting build definitions for environment: $EnvironmentName..."
Get-BuildDefinitionsByEnvironment
"$Action build definition triggers..."
ForEach($DefinitionID in $DefinitionIDs){
"$Action trigger for $DefinitionID"
Update-BuildDefinitionTriggerPath
}
}
Catch
{
Write-Error $_
Exit 1
}