-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNew-AzPipeline.ps1
More file actions
419 lines (330 loc) · 13.8 KB
/
Copy pathNew-AzPipeline.ps1
File metadata and controls
419 lines (330 loc) · 13.8 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#Requires -Version 7.0
<#
.SYNOPSIS
Create Azure Pipelines and Pull Request Build Validation checks.
.DESCRIPTION
Logs in to Azure DevOps with a Personal Access Token, discovers every
'pipeline.yml' file under a source path, and creates an Azure Pipeline for
each one whose name does not already exist in the target folder. Optionally
creates a branch Build Validation policy for each new pipeline.
When run inside an Azure Pipeline, set the AZURE_DEVOPS_EXT_PAT environment
variable to $(System.AccessToken); az devops login consumes it because tty is
not available in a pipeline run.
Prerequisites:
- Azure CLI 2.13.0 or later.
- Azure CLI 'azure-devops' extension 0.18.0 or later (auto-installed).
- A repository for which the pipeline needs to be configured.
- The '<ProjectName>' Build Service must have 'Edit build pipeline'
permission. See
https://learn.microsoft.com/azure/devops/pipelines/policies/permissions
.PARAMETER OrganizationName
Required. The name of the Azure DevOps organization.
.PARAMETER ProjectName
Required. The name of the Azure DevOps project.
.PARAMETER RepositoryName
Required. Repository for which the pipeline needs to be configured.
.PARAMETER PAT
Required. The access token with appropriate permissions to create Azure
Pipelines, supplied as a SecureString. The System.AccessToken from an Azure
Pipeline run usually has sufficient permissions. See
https://learn.microsoft.com/azure/devops/pipelines/process/access-tokens
.PARAMETER BranchName
Optional. Branch name for which the pipelines will be configured.
Default: 'main'.
.PARAMETER PipelineTargetPath
Optional. Path of the folder where the pipeline needs to be created.
.PARAMETER PipelineSourcePath
Optional. Path of the pipeline YAML file(s) used for creating Azure Pipelines.
All 'pipeline.yml' files under the given folder are searched and created
accordingly. Default is the current execution path.
.PARAMETER CreateBuildValidation
Optional. Also create a Pull Request Build Validation policy for each new
pipeline.
.INPUTS
None. This script does not accept pipeline input.
.OUTPUTS
None. Creates Azure Pipelines and (optionally) Build Validation policies as a
side effect.
.EXAMPLE
$pat = Read-Host -AsSecureString
./New-AzPipeline.ps1 -OrganizationName graef.io -ProjectName Project1 -RepositoryName Repository1 -PAT $pat
Create all pipelines for the project 'graef.io/Project1' using a PAT. The
pipelines are configured to use the default branch 'main' and the given
repository. Each 'pipeline.yml' under the source path produces one Azure
Pipeline named after its parent folder.
.NOTES
Author: Sebastian Gräf
Repo: https://github.com/segraef/Scripts
Version history is tracked in git, not in this header.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory, HelpMessage = 'Azure DevOps Organization: <OrganizationName>')]
[ValidateNotNullOrEmpty()]
[string]$OrganizationName,
[Parameter(Mandatory, HelpMessage = 'Azure DevOps Project: <ProjectName>')]
[ValidateNotNullOrEmpty()]
[string]$ProjectName,
[Parameter(Mandatory, HelpMessage = 'Azure DevOps Repository: <RepositoryName>')]
[ValidateNotNullOrEmpty()]
[string]$RepositoryName,
[Parameter(Mandatory, HelpMessage = 'Azure DevOps Personal Access Token: <PAT>')]
[ValidateNotNull()]
[securestring]$PAT,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$BranchName = 'main',
[Parameter()]
[string]$PipelineTargetPath,
[Parameter()]
[string]$PipelineSourcePath,
[Parameter()]
[switch]$CreateBuildValidation
)
#region Initialisation
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/Write-Log.psm1" -Force
#endregion
#region Functions
function Connect-AzDevOpsCli {
<#
.SYNOPSIS
Install the Azure DevOps CLI extension and log in with a PAT.
.DESCRIPTION
Ensures dynamic extension install is enabled, adds/upgrades the
azure-devops extension, verifies the CLI is available, logs in using the
supplied PAT via the AZURE_DEVOPS_EXT_PAT environment variable, and sets
the default organization and project.
.PARAMETER OrganizationUrl
The full Azure DevOps organization URL (https://dev.azure.com/<org>/).
.PARAMETER ProjectName
The Azure DevOps project name to set as the default.
.PARAMETER PAT
The Personal Access Token as a SecureString.
.EXAMPLE
Connect-AzDevOpsCli -OrganizationUrl 'https://dev.azure.com/graef.io/' -ProjectName Project1 -PAT $pat
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$OrganizationUrl,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ProjectName,
[Parameter(Mandatory)]
[ValidateNotNull()]
[securestring]$PAT
)
Write-Log 'Installing Azure CLI extension devops.'
az config set extension.use_dynamic_install=yes_without_prompt # allow installing extensions without prompt
az extension add --upgrade -n azure-devops
Write-Log 'Checking availability of Azure CLI and the Azure DevOps CLI extension.'
az | Out-Null
az devops -h | Out-Null
Write-Log "Logging in to Azure DevOps project at $OrganizationUrl$ProjectName with a PAT."
$plainPat = [System.Net.NetworkCredential]::new('', $PAT).Password
$env:AZURE_DEVOPS_EXT_PAT = $plainPat
Write-Output $plainPat | az devops login
Write-Log "Setting default Azure DevOps configuration to $OrganizationUrl and $ProjectName."
az devops configure --defaults organization="$OrganizationUrl" project="$ProjectName" --use-git-aliases true
}
function Get-ExistingAzPipeline {
<#
.SYNOPSIS
List existing Azure Pipelines in the target folder.
.DESCRIPTION
Queries Azure DevOps for the pipelines that already exist under the target
folder so they can be skipped during creation.
.PARAMETER OrganizationUrl
The full Azure DevOps organization URL.
.PARAMETER ProjectName
The Azure DevOps project name.
.PARAMETER PipelineTargetPath
The folder path under which to list pipelines.
.EXAMPLE
Get-ExistingAzPipeline -OrganizationUrl $url -ProjectName Project1 -PipelineTargetPath '/'
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$OrganizationUrl,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ProjectName,
[Parameter()]
[string]$PipelineTargetPath
)
Write-Log "Listing all Azure Pipelines in $PipelineTargetPath."
$azurePipelines = az pipelines list --organization $OrganizationUrl --project $ProjectName --folder-path $PipelineTargetPath |
ConvertFrom-Json |
Sort-Object name
Write-Log "Found $($azurePipelines.Count) Azure Pipeline(s) in $ProjectName."
Write-Output $azurePipelines
}
function Get-YamlPipelineDefinition {
<#
.SYNOPSIS
Discover 'pipeline.yml' files and build pipeline definition objects.
.DESCRIPTION
Recursively searches the source path for 'pipeline.yml' files and, for
each one, builds a PSCustomObject describing the pipeline to create
(project, repository, branch, folder, relative YAML path, and the name
derived from the parent folder).
.PARAMETER PipelineSourcePath
The folder under which to search for 'pipeline.yml' files. Resolved
relative to the current location.
.PARAMETER ProjectName
The Azure DevOps project name.
.PARAMETER RepositoryName
The repository name for which the pipelines are configured.
.PARAMETER BranchName
The branch name for which the pipelines are configured.
.PARAMETER PipelineTargetPath
The folder path where pipelines are created.
.EXAMPLE
Get-YamlPipelineDefinition -PipelineSourcePath './pipelines' -ProjectName P1 -RepositoryName R1 -BranchName main -PipelineTargetPath '/'
#>
[CmdletBinding()]
param
(
[Parameter()]
[string]$PipelineSourcePath,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ProjectName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$RepositoryName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$BranchName,
[Parameter()]
[string]$PipelineTargetPath
)
$sourcePath = if ([string]::IsNullOrWhiteSpace($PipelineSourcePath)) { '.' } else { $PipelineSourcePath }
$resolvedSourcePath = (Resolve-Path -Path $sourcePath).Path
Write-Log "Identifying relevant Azure Pipelines under $resolvedSourcePath."
$ymlPipelines = Get-ChildItem -Path $resolvedSourcePath -Recurse -File -Filter 'pipeline.yml' |
Sort-Object FullName
Write-Log "Found $($ymlPipelines.Count) YAML Pipeline(s) in $resolvedSourcePath."
$pipelinesArray = @()
foreach ($pipeline in $ymlPipelines) {
$ymlPath = [IO.Path]::GetRelativePath($resolvedSourcePath, $pipeline.FullName).Replace('\', '/')
$parentFolderName = Split-Path -Path (Split-Path -Path $pipeline.FullName -Parent) -Leaf
$pipelineName = $parentFolderName # used as the pipeline name
$pipeObj = [PSCustomObject]@{
ProjectName = $ProjectName
RepositoryName = $RepositoryName
BranchName = $BranchName
FolderPath = $PipelineTargetPath
ymlPath = $ymlPath
parentFolderName = $parentFolderName
pipelineName = $pipelineName
}
$pipelinesArray += $pipeObj
}
Write-Output $pipelinesArray
}
function New-AzPipelineDefinition {
<#
.SYNOPSIS
Create a single Azure Pipeline and, optionally, its Build Validation.
.DESCRIPTION
Creates one Azure Pipeline from a discovered definition object. When
requested, also creates a branch Build Validation policy scoped to the
pipeline's path.
.PARAMETER Pipeline
The pipeline definition object produced by Get-YamlPipelineDefinition.
.PARAMETER OrganizationUrl
The full Azure DevOps organization URL.
.PARAMETER CreateBuildValidation
Also create a Pull Request Build Validation policy for the new pipeline.
.EXAMPLE
New-AzPipelineDefinition -Pipeline $def -OrganizationUrl $url -CreateBuildValidation
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNull()]
[psobject]$Pipeline,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$OrganizationUrl,
[Parameter()]
[switch]$CreateBuildValidation
)
if (-not $PSCmdlet.ShouldProcess($Pipeline.pipelineName, 'Create Azure Pipeline')) {
return
}
Write-Log "Creating Azure pipeline $($Pipeline.pipelineName)."
$pipelineResult = az pipelines create --project "$($Pipeline.ProjectName)" `
--organization "$OrganizationUrl" `
--repository "$($Pipeline.RepositoryName)" `
--repository-type tfsgit `
--branch "$($Pipeline.BranchName)" `
--folder-path "$($Pipeline.FolderPath)" `
--name "$($Pipeline.pipelineName)" `
--yml-path "$($Pipeline.ymlPath)" `
--skip-run
$pipelineObject = $pipelineResult | ConvertFrom-Json
if ($CreateBuildValidation) {
$pathFilter = $Pipeline.ymlPath -replace 'pipeline.yml', '*'
Write-Log "Configuring branch Build Validation for $($Pipeline.pipelineName)."
az repos policy build create `
--blocking true `
--branch "$($Pipeline.BranchName)" `
--build-definition-id $pipelineObject.id `
--display-name "Check $($Pipeline.pipelineName)" `
--manual-queue-only true `
--queue-on-source-update-only true `
--valid-duration 1440 `
--path-filter $pathFilter `
--repository-id $pipelineObject.repository.id `
--enabled true
}
}
#endregion
#region Execution
Write-Log "Executing $($MyInvocation.MyCommand.Name)."
try {
$orgUrl = "https://dev.azure.com/$OrganizationName/"
Connect-AzDevOpsCli -OrganizationUrl $orgUrl -ProjectName $ProjectName -PAT $PAT
$azurePipelines = Get-ExistingAzPipeline -OrganizationUrl $orgUrl -ProjectName $ProjectName -PipelineTargetPath $PipelineTargetPath
$pipelinesArray = Get-YamlPipelineDefinition `
-PipelineSourcePath $PipelineSourcePath `
-ProjectName $ProjectName `
-RepositoryName $RepositoryName `
-BranchName $BranchName `
-PipelineTargetPath $PipelineTargetPath
$pipelinesToBeSkipped = $pipelinesArray | Where-Object { $_.pipelineName -in $azurePipelines.name }
$pipelinesToBeUpdated = $pipelinesArray | Where-Object { $_.pipelineName -notin $azurePipelines.name }
if ($pipelinesToBeUpdated.Count -eq 0) {
Write-Log 'No Pipelines have been identified. Exiting.'
return
}
Write-Log "$($pipelinesToBeUpdated.Count) Pipeline(s) have been identified to be updated."
Write-Log "$($pipelinesToBeSkipped.Count) Pipeline(s) will be skipped."
foreach ($pipeline in $pipelinesToBeUpdated) {
New-AzPipelineDefinition -Pipeline $pipeline -OrganizationUrl $orgUrl -CreateBuildValidation:$CreateBuildValidation
}
Write-Log "$($pipelinesToBeUpdated.Count) Azure pipeline(s) created!"
if ($CreateBuildValidation) {
Write-Log "$($pipelinesToBeUpdated.Count) Pull Request Build Validation(s) created!"
}
Write-Log "$($pipelinesToBeSkipped.Count) Azure pipeline(s) skipped!"
$url = $orgUrl + $ProjectName + '/' + '_build?definitionScope=%5C' + $PipelineTargetPath
Write-Log "Please check your Azure Pipelines here: $url"
}
catch {
Write-Log -Message ('Reason: [{0}]' -f $_.Exception.Message) -ErrorRecord $_
throw
}
Write-Log "Finished $($MyInvocation.MyCommand.Name)."
#endregion