-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathbatch-generation.yml
More file actions
555 lines (481 loc) · 19.7 KB
/
batch-generation.yml
File metadata and controls
555 lines (481 loc) · 19.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
parameters:
- name: TestChangedModulesOnly
type: boolean
default: true
# Selected modules to generate, build, analyze and test
- name: SelectedTargetModules
type: string
default: 'None'
- name: NotificationReceiversOverride
type: string
default: 'None'
# branch from when creating the generation branch
- name: GenerationBaseBranch
type: string
default: 'main'
- name: PrepareModulesOnGenerationBranch
type: boolean
default: false
variables:
IntermediateStepTimeoutInMinutes: 30
GenerateTimeoutInMinutes: 120
BuildTimeoutInMinutes: 120
AnalysisTimeoutInMinutes: 120
TestTimeoutInMinutes: 180
MaxParallelGenerateJobs: 15
MaxParallelBuildJobs: 15
MaxParallelAnalyzeJobs: 5
MaxParallelTestWindowsJobs: 5
MaxParallelTestLinuxJobs: 5
MaxParallelTestMacJobs: 5
WindowsAgentPoolName: pool-windows-2022
LinuxAgentPoolName: pool-ubuntu-2404
MacOSAgentPoolName: 'Azure Pipelines'
MacOSAgentPoolVMImage: macOS-latest
trigger: none
resources:
pipelines:
- pipeline: autorestRelease
project: release
source: "autorest.powershell - Publish on commit - OneBranch"
trigger: true
stages:
- stage: Generate
jobs:
- job: prepare
displayName: Generate Build Targets Matrix
timeoutInMinutes: ${{ variables.IntermediateStepTimeoutInMinutes }}
pool: ${{ variables.WindowsAgentPoolName }}
steps:
- task: PowerShell@2
name: showVariables
displayName: 'Show Variables'
inputs:
targetType: inline
pwsh: true
script: |
Write-Host "Pipeline variable SkippedModules: $($env:SkippedModules)"
Write-Host "Pipeline variable FailedJobNotificationReceivers: $($env:FailedJobNotificationReceivers)"
Write-Host "Parameter TestChangedModulesOnly: ${{ parameters.TestChangedModulesOnly }}"
Write-Host "Parameter SelectedTargetModules: ${{ parameters.SelectedTargetModules }}"
Write-Host "Parameter NotificationReceiversOverride: ${{ parameters.NotificationReceiversOverride }}"
Write-Host "Parameter GenerationBaseBranch: ${{ parameters.GenerationBaseBranch }}"
Write-Host "Parameter PrepareModulesOnGenerationBranch: ${{ parameters.PrepareModulesOnGenerationBranch }}"
# TODO: (Bernard) Uncomment the no checkout step after automatically install repo into agents
# - checkout: none
- checkout: self
fetchDepth: 1
fetchTags: false
- template: util/get-github-pat-steps.yml
- task: PowerShell@2
name: checkout
displayName: 'Create Batch Generation Branch'
inputs:
targetType: inline
pwsh: true
script: |
$date = Get-Date -Format "yy-MM-dd"
$generationBranch = "batch-generation/branch-$date-$(Build.BuildId)"
$token = "$(GitHubToken)"
$createBranchPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'create-branch.ps1'
& $createBranchPath -Owner 'Azure' -Repo 'azure-powershell' -BaseBranch "${{ parameters.GenerationBaseBranch }}" -NewBranch $generationBranch -Token $token
Write-Host "##vso[task.setvariable variable=GenerationBranch;isOutput=true]$generationBranch"
- task: PowerShell@2
name: version
displayName: 'Get latest Autorest Version'
inputs:
targetType: inline
pwsh: true
script: |
$autorestPowershellUrl = "https://registry.npmjs.org/@autorest/powershell"
$response = Invoke-RestMethod -Uri $autorestPowershellUrl -Method Get
$latestAutorestVersion = $response.'dist-tags'.latest
Write-Host "Latest version of Autorest Powershell is $latestAutorestVersion"
Write-Host "##vso[task.setvariable variable=LatestAutorestVersion;isOutput=true]$latestAutorestVersion"
- task: PowerShell@2
name: mtrx
displayName: 'Generate Build Targets Matrix'
inputs:
targetType: inline
pwsh: true
script: |
if ($env:PREPARE_MODULES_ON_GENERATION_BRANCH -eq 'true') {
Write-Host "Checkout to GenerationBranch: $(checkout.GenerationBranch)"
git fetch origin "$(checkout.GenerationBranch)"
git checkout "$(checkout.GenerationBranch)"
}
$prepareModulesPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'prepare.ps1'
& $prepareModulesPath -RepoRoot "$(Build.SourcesDirectory)" -MaxParallelJobs "${{ variables.MaxParallelGenerateJobs }}"
env:
SKIPPED_MODULES: $(SkippedModules)
SELECTED_TARGET_MODULES: ${{ parameters.SelectedTargetModules }}
PREPARE_MODULES_ON_GENERATION_BRANCH: ${{ parameters.PrepareModulesOnGenerationBranch }}
- task: PublishPipelineArtifact@1
displayName: 'Upload generated targets'
inputs:
targetPath: artifacts
artifact: 'prepare'
- job: generate
displayName: "Batch Generate: "
dependsOn: prepare
condition: and(
ne(dependencies.prepare.outputs['mtrx.generationTargets'], ''),
ne(dependencies.prepare.outputs['mtrx.generationTargets'], '{}')
)
variables:
GenerationBranch: $[dependencies.prepare.outputs['checkout.GenerationBranch']]
LatestAutorestVersion: $[dependencies.prepare.outputs['version.LatestAutorestVersion']]
timeoutInMinutes: ${{ variables.GenerateTimeoutInMinutes }}
pool: ${{ variables.WindowsAgentPoolName }}
strategy:
matrix: $[ dependencies.prepare.outputs['mtrx.generationTargets'] ]
maxParallel: ${{ variables.MaxParallelGenerateJobs }}
steps:
- checkout: self
persistCredentials: true
fetchTags: false
- task: DownloadPipelineArtifact@2
inputs:
artifactName: 'prepare'
targetPath: artifacts
- task: PowerShell@2
name: generate
displayName: 'Regenerate Modules'
inputs:
targetType: inline
pwsh: true
script: |
git fetch origin "$(GenerationBranch)"
git checkout "$(GenerationBranch)"
$batchGenerateModulesPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'batch-generate-modules.ps1'
& $batchGenerateModulesPath -MatrixKey "$(MatrixKey)" -RepoRoot "$(Build.SourcesDirectory)" -AutorestVersion "$(LatestAutorestVersion)"
workingDirectory: $(Build.SourcesDirectory)
- task: PublishPipelineArtifact@1
displayName: 'Save patch and generation report'
inputs:
targetPath: artifacts
artifact: 'generate-$(MatrixKey)'
condition: always()
- stage: Build
dependsOn: Generate
condition: always()
variables:
GenerationBranch: $[stageDependencies.Generate.prepare.outputs['checkout.GenerationBranch']]
LatestAutorestVersion: $[stageDependencies.Generate.prepare.outputs['version.LatestAutorestVersion']]
jobs:
- job: filter
displayName: 'Filter Changed Modules'
timeoutInMinutes: ${{ variables.IntermediateStepTimeoutInMinutes }}
pool: ${{ variables.WindowsAgentPoolName }}
steps:
- checkout: self
persistCredentials: true
fetchTags: false
- task: PowerShell@2
name: checkout
displayName: 'Checkout Generation Branch'
inputs:
targetType: inline
pwsh: true
script: |
git fetch origin "$(GenerationBranch)"
git checkout "$(GenerationBranch)"
- task: DownloadPipelineArtifact@2
inputs:
artifactName: 'prepare'
targetPath: artifacts
- download: current
patterns: '**/changed-*.patch'
displayName: 'Download all .patch artifacts'
- task: PowerShell@2
name: apply
displayName: 'Apply all patches'
inputs:
targetType: inline
pwsh: true
script: |
git config user.email "65331932+azure-powershell-bot@users.noreply.github.com"
git config user.name "azure-powershell-bot"
Write-Host "Applying all .patch files to "$(GenerationBranch)"..."
$patchFiles = Get-ChildItem -Path "$env:PIPELINE_WORKSPACE" -Recurse -Filter *.patch
foreach ($patch in $patchFiles) {
Write-Host "Applying patch: $($patch.FullName)"
git apply --whitespace=fix "$($patch.FullName)"
}
git add .
git commit -m "Autorest Upgrade - $(LatestAutorestVersion)"
git push origin "$(GenerationBranch)"
- task: PowerShell@2
name: mtrx
displayName: 'Generate Test Targets Matrix'
inputs:
targetType: inline
pwsh: true
script: |
$base = git merge-base HEAD origin/main
$changedFiles = git diff --name-only $base HEAD
$sourceBranchName = "$(Build.SourceBranch)".Replace('refs/heads/', '')
git checkout $sourceBranchName
$filterModulesPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'filter.ps1'
& $filterModulesPath `
-MaxParallelBuildJobs "${{ variables.MaxParallelBuildJobs }}" `
-MaxParallelAnalyzeJobs "${{ variables.MaxParallelAnalyzeJobs }}" `
-MaxParallelTestWindowsJobs "${{ variables.MaxParallelTestWindowsJobs }}" `
-MaxParallelTestLinuxJobs "${{ variables.MaxParallelTestLinuxJobs }}" `
-MaxParallelTestMacJobs "${{ variables.MaxParallelTestMacJobs }}" `
-ChangedFiles $changedFiles `
-RepoRoot "$(Build.SourcesDirectory)"
env:
TEST_CHANGED_MODULES_ONLY: ${{ parameters.TestChangedModulesOnly }}
- task: PublishPipelineArtifact@1
displayName: 'Upload filtered targets'
inputs:
targetPath: artifacts
artifact: 'filter'
- job: build
displayName: "Build:"
dependsOn: filter
condition: and(
ne(dependencies.filter.outputs['mtrx.buildTargets'], ''),
ne(dependencies.filter.outputs['mtrx.buildTargets'], '{}')
)
timeoutInMinutes: ${{ variables.BuildTimeoutInMinutes }}
pool: ${{ variables.WindowsAgentPoolName }}
strategy:
matrix: $[ dependencies.filter.outputs['mtrx.buildTargets'] ]
maxParallel: ${{ variables.MaxParallelBuildJobs }}
steps:
- checkout: self
fetchDepth: 1
fetchTags: false
- task: DownloadPipelineArtifact@2
inputs:
artifactName: 'filter'
targetPath: artifacts
- task: PowerShell@2
name: build
displayName: 'Build Targets'
inputs:
targetType: inline
pwsh: true
script: |
git fetch origin "$(GenerationBranch)"
git checkout "$(GenerationBranch)"
$buildModulesPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'build-modules.ps1'
& $buildModulesPath -MatrixKey "$(MatrixKey)" -RepoRoot "$(Build.SourcesDirectory)"
workingDirectory: $(Build.SourcesDirectory)
- task: PublishPipelineArtifact@1
displayName: 'Save build artifacts'
inputs:
targetPath: artifacts
artifact: 'build-$(MatrixKey)'
condition: always()
- stage: Test
dependsOn: Build
condition: always()
jobs:
- job: collect
displayName: "Collect Artifacts"
timeoutInMinutes: ${{ variables.IntermediateStepTimeoutInMinutes }}
pool: ${{ variables.WindowsAgentPoolName }}
steps:
- checkout: none
- download: current
patterns: |
**/Debug/**
**/StaticAnalysis/**
displayName: 'Download build artifacts'
- task: DownloadPipelineArtifact@2
inputs:
artifactName: 'filter'
targetPath: artifacts
- task: PowerShell@2
name: collect
displayName: 'Collect modules artifacts'
inputs:
targetType: inline
pwsh: true
script: |
$workspace = "$env:PIPELINE_WORKSPACE"
$repoRoot = "$(Build.SourcesDirectory)"
$artifactsRoot = Join-Path $repoRoot "artifacts"
$debugArtifactDestPath = Join-Path $artifactsRoot "Debug"
New-Item -ItemType Directory -Force -Path $debugArtifactDestPath | Out-Null
$copiedModules = @{}
$StaticAnalysisCopied = $false
Get-ChildItem -Path $workspace -Directory | ForEach-Object {
$debugArtifactSrcPath = Join-Path $_.FullName "Debug"
$StaticAnalysisSrcDirectory = Join-Path $_.FullName 'StaticAnalysis'
if (Test-Path $debugArtifactSrcPath) {
Get-ChildItem -Path $debugArtifactSrcPath -Directory | ForEach-Object {
$moduleName = $_.Name
if (-not $copiedModules.ContainsKey($moduleName)) {
$destPath = Join-Path $debugArtifactDestPath $moduleName
Copy-Item -Path $_.FullName -Destination $destPath -Recurse
$copiedModules[$moduleName] = $true
Write-Host "Copied $moduleName from $debugArtifactSrcPath"
} else {
Write-Host "Skipped $moduleName from $debugArtifactSrcPath (already copied)"
}
}
}
if ((Test-Path $StaticAnalysisSrcDirectory) -and (-not $StaticAnalysisCopied)) {
$destPath = Join-Path $artifactsRoot 'StaticAnalysis'
Copy-Item -Path $StaticAnalysisSrcDirectory -Destination $destPath -Recurse
$StaticAnalysisCopied = $true
Write-Host "Copied StaticAnalysis from $StaticAnalysisSrcDirectory"
}
}
Get-ChildItem -Path $artifactsRoot -Directory | ForEach-Object {
Write-Host "Artifact Directory - $($_.Name)"
}
Get-ChildItem -Path $debugArtifactDestPath -Directory | ForEach-Object {
Write-Host "Debug Directory - $($_.Name)"
}
- task: PublishPipelineArtifact@1
displayName: 'Upload collected artifacts'
inputs:
targetPath: artifacts
artifact: 'collect'
- job: analyze
displayName: "Analyze:"
dependsOn:
- collect
condition: and(
ne(stageDependencies.Build.filter.outputs['mtrx.analyzeTargets'], ''),
ne(stageDependencies.Build.filter.outputs['mtrx.analyzeTargets'], '{}')
)
timeoutInMinutes: ${{ variables.AnalysisTimeoutInMinutes }}
pool: ${{ variables.WindowsAgentPoolName }}
strategy:
matrix: $[ stageDependencies.Build.filter.outputs['mtrx.analyzeTargets'] ]
maxParallel: ${{ variables.MaxParallelAnalyzeJobs }}
steps:
- checkout: self
fetchDepth: 1
fetchTags: false
- task: DownloadPipelineArtifact@2
inputs:
artifact: collect
path: $(Build.SourcesDirectory)/artifacts
displayName: 'Download collected artifact to artifacts folder'
- task: NodeTool@0
displayName: Install autorest
inputs:
versionSpec: '14.17.1'
command: custom
verbose: false
customCommand: install autorest@latest
- task: PowerShell@2
displayName: Setup environment for autorest
inputs:
targetType: inline
script: "$env:NODE_OPTIONS=\"--max-old-space-size=65536\""
pwsh: true
- task: UseDotNet@2
displayName: 'Use .NET SDK for Static Analysis'
inputs:
packageType: sdk
version: 8.x
- task: PowerShell@2
name: analyze
displayName: 'Analyze modules'
inputs:
targetType: inline
pwsh: true
script: |
Write-Host "Matrix Key: $(MatrixKey)"
$analyseModulesPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'analyse-modules.ps1'
& $analyseModulesPath -MatrixKey "$(MatrixKey)" -RepoRoot "$(Build.SourcesDirectory)"
- task: PublishPipelineArtifact@1
displayName: 'Save Analyse Report'
inputs:
targetPath: artifacts
artifact: 'analyse-$(MatrixKey)'
condition: always()
- template: util/batch-generation-test-job.yml
parameters:
jobName: 'test_windows'
OSName: 'Windows'
timeoutInMinutes: ${{ variables.TestTimeoutInMinutes }}
agentPoolName: ${{ variables.WindowsAgentPoolName }}
maxParallel: ${{ variables.MaxParallelTestWindowsJobs }}
- template: util/batch-generation-test-job.yml
parameters:
jobName: 'test_linux'
OSName: 'Linux'
timeoutInMinutes: ${{ variables.TestTimeoutInMinutes }}
agentPoolName: ${{ variables.LinuxAgentPoolName }}
maxParallel: ${{ variables.MaxParallelTestLinuxJobs }}
- template: util/batch-generation-test-job.yml
parameters:
jobName: 'test_mac'
OSName: 'MacOS'
timeoutInMinutes: ${{ variables.TestTimeoutInMinutes }}
agentPoolName: ${{ variables.MacOSAgentPoolName }}
agentPoolVMImage: ${{ variables.MacOSAgentPoolVMImage }}
maxParallel: ${{ variables.MaxParallelTestMacJobs }}
- stage: Report
condition: always()
jobs:
- job: report
displayName: 'Report'
timeoutInMinutes: ${{ variables.IntermediateStepTimeoutInMinutes }}
pool: ${{ variables.WindowsAgentPoolName }}
steps:
- checkout: self
fetchDepth: 1
fetchTags: false
- task: DownloadPipelineArtifact@2
inputs:
artifactName: 'prepare'
targetPath: artifacts
- task: DownloadPipelineArtifact@2
inputs:
artifactName: 'filter'
targetPath: artifacts
- download: current
patterns: |
**/AnalyseReport-*.json
**/GenerationReport-*.json
**/BuildReport-*.json
**/Test*Report-*.json
displayName: 'Download build artifacts'
- task: PowerShell@2
name: report
displayName: 'Report Results'
inputs:
targetType: inline
pwsh: true
script: |
$reportScriptPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'report.ps1'
& $reportScriptPath -RepoRoot "$(Build.SourcesDirectory)" -PipelineWorkspace "$env:PIPELINE_WORKSPACE"
- task: PublishPipelineArtifact@1
displayName: 'Save Reports'
inputs:
targetPath: artifacts
artifact: 'reports'
condition: always()
- stage: FailedJobNotification
dependsOn:
- Generate
- Build
- Test
- Report
condition: failed()
jobs:
- job: FailJobNotification
steps:
- task: PowerShell@2
name: notification
displayName: "teams notification on failed job"
inputs:
targetType: inline
pwsh: true
script: |
$notificationScriptPath = Join-Path "$(Build.SourcesDirectory)" '.azure-pipelines' 'PipelineSteps' 'BatchGeneration' 'notify-failed-job.ps1'
& $notificationScriptPath -RepoRoot "$(Build.SourcesDirectory)"
env:
TEAMS_URL: $(TEAMS_URL)
FailedJobNotificationReceivers: $(FailedJobNotificationReceivers)
NotificationReceiversOverride: ${{ parameters.NotificationReceiversOverride }}