-
Notifications
You must be signed in to change notification settings - Fork 456
/
Invoke-PipelinesForBranch.ps1
380 lines (282 loc) · 15.4 KB
/
Invoke-PipelinesForBranch.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#region helper functions
<#
.SYNOPSIS
Invoke a given GitHub workflow
.DESCRIPTION
Long description
.PARAMETER PersonalAccessToken
Mandatory. The GitHub PAT to leverage when interacting with the GitHub API.
.PARAMETER GitHubRepositoryOwner
Mandatory. The repository's organization.
.PARAMETER GitHubRepositoryName
Mandatory. The name of the repository to trigger the workflows in.
.PARAMETER WorkflowFileName
Mandatory. The name of the workflow to trigger
.PARAMETER TargetBranch
Optional. The branch to trigger the pipeline for.
.PARAMETER GitHubPipelineInputs
Optional. Input parameters to pass into the pipeline. Must match the names of the runtime parameters in the yaml file(s)
.PARAMETER WorkflowFilePath
Required. The path to the workflow.
.PARAMETER InvokeForDiff
Optional. Trigger workflows only for those who's module files have changed (based on diff of branch to main)
.EXAMPLE
Invoke-GitHubWorkflow -PersonalAccessToken '<Placeholder>' -GitHubRepositoryOwner 'Azure' -GitHubRepositoryName 'ResourceModules' -WorkflowFileName 'ms.analysisservices.servers.yml' -TargetBranch 'main' -GitHubPipelineInputs @{ prerelease = 'false'; staticValidation = 'true'; deploymentValidation = 'true'; removeDeployment = 'true' }
Trigger the workflow 'ms.analysisservices.servers.yml' with branch 'main' in repository 'Azure/ResourceModules'.
#>
function Invoke-GitHubWorkflow {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[string] $PersonalAccessToken,
[Parameter(Mandatory = $true)]
[string] $GitHubRepositoryOwner,
[Parameter(Mandatory = $true)]
[string] $GitHubRepositoryName,
[Parameter(Mandatory = $false)]
[hashtable] $GitHubPipelineInputs = @{},
[Parameter(Mandatory = $true)]
[string] $WorkflowFilePath,
[Parameter(Mandatory = $false)]
[string] $TargetBranch = 'main'
)
# Load used function
. (Join-Path (Split-Path $PSScriptRoot -Parent) 'pipelines' 'sharedScripts' 'Get-GitHubWorkflowDefaultInput.ps1')
$workflowFileName = Split-Path $WorkflowFilePath -Leaf
$requestInputObject = @{
Method = 'POST'
Uri = "https://api.github.com/repos/$GitHubRepositoryOwner/$GitHubRepositoryName/actions/workflows/$workflowFileName/dispatches"
Headers = @{
Authorization = "Bearer $PersonalAccessToken"
}
Body = @{
ref = $TargetBranch
inputs = $GitHubPipelineInputs
} | ConvertTo-Json
}
if ($PSCmdlet.ShouldProcess("GitHub workflow [$workflowFileName] for branch [$TargetBranch]", 'Invoke')) {
try {
$response = Invoke-RestMethod @requestInputObject -Verbose:$false
} catch {
Write-Error "Request failed for [$workflowFileName]. Reponse: [$_]"
}
if ($response) {
Write-Error "Request failed for [$workflowFileName]. Reponse: [$response]"
return $false
}
}
return $true
}
<#
.SYNOPSIS
Get a list of all GitHub module workflows
.DESCRIPTION
Get a list of all GitHub module workflows. Does not return all properties but only the relevant ones.
.PARAMETER PersonalAccessToken
Mandatory. The GitHub PAT to leverage when interacting with the GitHub API.
.PARAMETER GitHubRepositoryOwner
Mandatory. The repository's organization.
.PARAMETER GitHubRepositoryName
Mandatory. The name of the repository to fetch the workflows from.
.PARAMETER Filter
Optional. A filter to apply when fetching the workflows. By default we fetch all module workflows (ms.*).
.EXAMPLE
Get-GitHubModuleWorkflowList -PersonalAccessToken '<Placeholder>' -GitHubRepositoryOwner 'Azure' -GitHubRepositoryName 'ResourceModules'
Get all module workflows from repository 'Azure/ResourceModules'
#>
function Get-GitHubModuleWorkflowList {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $PersonalAccessToken,
[Parameter(Mandatory = $true)]
[string] $GitHubRepositoryOwner,
[Parameter(Mandatory = $true)]
[string] $GitHubRepositoryName,
[Parameter(Mandatory = $false)]
[string] $Filter = 'ms.*'
)
$allWorkflows = @()
$page = 1
do {
$requestInputObject = @{
Method = 'GET'
Uri = "https://api.github.com/repos/$GitHubRepositoryOwner/$GitHubRepositoryName/actions/workflows?per_page=100&page=$page"
Headers = @{
Authorization = "Bearer $PersonalAccessToken"
}
}
$response = Invoke-RestMethod @requestInputObject
if (-not $response.workflows) {
Write-Error "Request failed. Reponse: [$response]"
}
$allWorkflows += $response.workflows | Select-Object -Property @('id', 'name', 'path', 'badge_url') | Where-Object { (Split-Path $_.path -Leaf) -like $Filter }
$expectedPages = [math]::ceiling($response.total_count / 100)
$page++
} while ($page -le $expectedPages)
return $allWorkflows
}
#endregion
<#
.SYNOPSIS
Trigger all pipelines for either Azure DevOps or GitHub
.DESCRIPTION
Trigger all pipelines for either Azure DevOps or GitHub. By default, pipelines are filtered to CARML module pipelines.
Note, for Azure DevOps you'll need the 'azure-devops' extension: `az extension add --upgrade -n azure-devops`
.PARAMETER PersonalAccessToken
Mandatory. The PAT to use to interact with either GitHub / Azure DevOps.
.PARAMETER TargetBranch
Mandatory. The branch to run the pipelines for (e.g. `main`).
.PARAMETER PipelineFilter
Optional. The pipeline files to filter down to. By default only files with a name that starts with 'ms.*' are considered. E.g. 'ms.network*'.
.PARAMETER Environment
Optional. The environment to run the pipelines for. By default it's GitHub.
.PARAMETER GeneratePipelineBadges
Optional. Generate pipeline status badges for the given pipeline configuration.
.PARAMETER RepositoryRoot
Optional. The root of the repository. Used to load related functions in their folder path.
.PARAMETER GitHubRepositoryOwner
Optional. The GitHub organization to run the workfows in. Required if the chosen environment is `GitHub`. Defaults to 'Azure'.
.PARAMETER GitHubRepositoryName
Optional. The GitHub repository to run the workfows in. Required if the chosen environment is `GitHub`. Defaults to 'ResourceModules'.
.PARAMETER AzureDevOpsOrganizationName
Optional. The Azure DevOps organization to run the pipelines in. Required if the chosen environment is `AzureDevOps`.
.PARAMETER AzureDevOpsProjectName
Optional. The Azure DevOps project to run the pipelines in. Required if the chosen environment is `AzureDevOps`.
.PARAMETER AzureDevOpsPipelineFolderPath
Optional. The folder in Azure DevOps the pipelines are registerd in. Required if the chosen environment is `AzureDevOps`. Defaults to 'CARML-Modules'.
.EXAMPLE
Invoke-PipelinesForBranch -PersonalAccessToken '<Placeholder>' -TargetBranch 'feature/branch' -Environment 'GitHub' -PipelineFilter 'ms.network.*' -GitHubPipelineInputs @{ prerelease = 'false'; staticValidation = 'true'; deploymentValidation = 'true'; removeDeployment = 'true' }
Run all GitHub workflows that start with 'ms.network.*' using branch 'feature/branch'. Also returns all GitHub status badges.
.EXAMPLE
Invoke-PipelinesForBranch -PersonalAccessToken '<Placeholder>' -TargetBranch 'feature/branch' -Environment 'AzureDevOps' -PipelineFilter 'ms.network.*' -AzureDevOpsOrganizationName 'contoso' -AzureDevOpsProjectName 'Sanchez' -AzureDevOpsPipelineFolderPath 'CARML-Modules'
Run all Azure DevOps pipelines that start with 'ms.network.*' using branch 'feature/branch'. Also returns all Azure DevOps pipeline status badges.
#>
function Invoke-PipelinesForBranch {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[string] $PersonalAccessToken,
[Parameter(Mandatory = $true)]
[string] $TargetBranch,
[Parameter(Mandatory = $false)]
[string] $PipelineFilter = 'ms.*',
[Parameter(Mandatory = $false)]
[switch] $InvokeForDiff,
[Parameter(Mandatory = $false)]
[ValidateSet('GitHub', 'AzureDevOps')]
[string] $Environment = 'GitHub',
[Parameter(Mandatory = $false)]
[bool] $GeneratePipelineBadges = $true,
[Parameter(Mandatory = $false)]
[string] $RepositoryRoot = (Split-Path (Split-Path $PSScriptRoot -Parent)),
[Parameter(Mandatory = $false, ParameterSetName = 'GitHub')]
[string] $GitHubRepositoryOwner = 'Azure',
[Parameter(Mandatory = $false, ParameterSetName = 'GitHub')]
[string] $GitHubRepositoryName = 'ResourceModules',
[Parameter(Mandatory = $false, ParameterSetName = 'GitHub')]
[hashtable] $GitHubPipelineInputs = @{
prerelease = 'false'
deploymentValidation = 'false'
removeDeployment = 'true'
},
[Parameter(Mandatory = $false, ParameterSetName = 'AzureDevOps')]
[string] $AzureDevOpsOrganizationName = '',
[Parameter(Mandatory = $false, ParameterSetName = 'AzureDevOps')]
[string] $AzureDevOpsProjectName,
[Parameter(Mandatory = $false, ParameterSetName = 'AzureDevOps')]
[string] $AzureDevOpsPipelineFolderPath = 'CARML-Modules'
)
if ($Environment -eq 'GitHub') {
$baseInputObject = @{
PersonalAccessToken = $PersonalAccessToken
GitHubRepositoryOwner = $GitHubRepositoryOwner
GitHubRepositoryName = $GitHubRepositoryName
}
Write-Verbose 'Fetching current GitHub workflows' -Verbose
$workflows = Get-GitHubModuleWorkflowList @baseInputObject -Filter $PipelineFilter
Write-Verbose ('Fetched [{0}] workflows' -f $workflows.Count) -Verbose
if ($InvokeForDiff) {
# Load used function
. (Join-Path $RepositoryRoot 'utilities' 'tools' 'helper' 'Get-PipelineFileName.ps1')
# Get diff
$diff = git diff 'main' --name-only
# Identify pipeline names
$pipelineNames = [System.Collections.ArrayList]@()
$pipelineNames = $diff | ForEach-Object {
$folderPath = Split-Path $_ -Parent
$resourceTypeIdentifier = ($folderPath -split 'modules[\/|\\]{1}')[1] -replace '\\', '/'
if ($resourceTypeIdentifier.Length -gt 0) {
$pipelineFileName = Get-PipelineFileName -ResourceIdentifier $resourceTypeIdentifier
if ($pipelineFileName -match $PipelineFilter) {
$pipelineFileName
}
}
} | Select-Object -Unique
# Filter workflows
$workflows = $workflows | Where-Object {
$pipelineNames -contains (Split-Path $_.path -Leaf)
}
Write-Verbose ("As per 'diff', filtered workflows down to [{0}]" -f $workflows.Count) -Verbose
}
$gitHubWorkflowBadges = [System.Collections.ArrayList]@()
Write-Verbose "Triggering GitHub workflows for branch [$TargetBranch]" -Verbose
foreach ($workflow in $workflows) {
$workflowName = $workflow.name
$workflowFilePath = $workflow.path
$WorkflowFileName = Split-Path $Workflow.path -Leaf
if (Test-Path (Join-Path $RepositoryRoot $workflowFilePath)) {
if ($PSCmdlet.ShouldProcess("GitHub workflow [$WorkflowFileName] for branch [$TargetBranch]", 'Invoke')) {
$null = Invoke-GitHubWorkflow @baseInputObject -TargetBranch $TargetBranch -WorkflowFilePath (Join-Path $RepositoryRoot $workflowFilePath) -GitHubPipelineInputs $GitHubPipelineInputs
}
} else {
Write-Warning ('Warning: Workflow [{0}] is registered, but no workflow file in the target branch [{1}] available' -f (Join-Path $RepositoryRoot $workflowFilePath), $TargetBranch) -Verbose
}
# Generate pipeline badges
if ($GeneratePipelineBadges) {
$encodedBranch = [uri]::EscapeDataString($TargetBranch)
$workflowUrl = "https://github.com/$GitHubRepositoryOwner/$GitHubRepositoryName/actions/workflows/$workflowFileName"
$gitHubWorkflowBadges += "[![$workflowName]($workflowUrl/badge.svg?branch=$encodedBranch)]($workflowUrl)"
}
}
if ($gitHubWorkflowBadges.Count -gt 0) {
Write-Verbose 'GitHub Workflow Badges' -Verbose
Write-Verbose '======================' -Verbose
Write-Verbose ($gitHubWorkflowBadges | Sort-Object | Out-String) -Verbose
}
}
if ($Environment -eq 'AzureDevOps') {
$azureDevOpsOrgUrl = "https://dev.azure.com/$AzureDevOpsOrganizationName/"
# Login into Azure DevOps project with a PAT
$PersonalAccessToken | az devops login
# Set default Azure DevOps configuration (to not continously specify it on every command)
az devops configure --defaults organization=$azureDevOpsOrgUrl project=$AzureDevOpsProjectName --use-git-aliases $true
Write-Verbose "Get and list all [$AzureDevOpsOrganizationName/$AzureDevOpsProjectName] Azure DevOps pipelines in folder [$AzureDevOpsPipelineFolderPath]"
$azurePipelines = az pipelines list --folder-path $AzureDevOpsPipelineFolderPath | ConvertFrom-Json
Write-Verbose 'Fetching details' # Required as we need the original file path for filtering (which is only available when fetching the pipeline directly)
$detailedAzurePipelines = $azurePipelines | ForEach-Object -ThrottleLimit 10 -Parallel {
Write-Verbose ('Fetching detailed information for pipeline [{0}]' -f $PSItem.name)
az pipelines show --organization $USING:azureDevOpsOrgUrl --project $USING:AzureDevOpsProjectName --id $PSItem.id | ConvertFrom-Json
}
$modulePipelines = $detailedAzurePipelines | Where-Object { (Split-Path $_.process.yamlFileName -Leaf) -like $PipelineFilter } | Sort-Object -Property 'Name'
Write-Verbose "Triggering Azure DevOps pipelines for branch [$TargetBranch]" -Verbose
$modulePipelines | ForEach-Object -ThrottleLimit 10 -Parallel {
if ($Using:WhatIfPreference) {
Write-Verbose ("Would performing the operation `"Invoke`" on target `"GitHub workflow [{0}] for branch [{1}]`"." -f $PSItem.Name, $USING:TargetBranch) -Verbose
} else {
$null = az pipelines run --branch $USING:TargetBranch --id $PSItem.id --organization $USING:azureDevOpsOrgUrl --project $USING:AzureDevOpsProjectName
}
}
if ($GeneratePipelineBadges) {
foreach ($modulePipeline in $modulePipelines) {
# Generate pipeline badges
$pipelineDefinitionId = $modulePipeline.id
$encodedPipelineName = [uri]::EscapeDataString($modulePipeline.Name)
$encodedBranch = [uri]::EscapeDataString($TargetBranch)
$primaryUrl = 'https://dev.azure.com/{0}/{1}/_apis/build/status/{2}/{3}?branchName={4}' -f $AzureDevOpsOrganizationName, $AzureDevOpsProjectName, $AzureDevOpsPipelineFolderPath, $encodedPipelineName, $encodedBranch
$secondaryUrl = 'https://dev.azure.com/{0}/{1}/_build/latest?definitionId={2}&branchName={3}' -f $AzureDevOpsOrganizationName, $AzureDevOpsProjectName, $pipelineDefinitionId, $encodedBranch
Write-Verbose "[![Build Status]($primaryUrl)]($secondaryUrl)" -Verbose
}
}
}
}