forked from dsccommunity/DscWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
32 New Release Pipeline DscWorkshop.ps1
558 lines (532 loc) · 20.4 KB
/
32 New Release Pipeline DscWorkshop.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
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
556
557
558
$projectName = 'DscWorkshop'
$projectGitUrl = 'https://github.com/DscCommunity/DscWorkshop'
$collectionName = 'AutomatedLab'
$lab = Get-Lab
$devOpsServer = Get-LabVM -Role AzDevOps
$devOpsHostName = if ($lab.DefaultVirtualizationEngine -eq 'Azure') { $devOpsServer.AzureConnectionInfo.DnsName } else { $devOpsServer.FQDN }
$nugetServer = Get-LabVM -Role AzDevOps
$nugetFeed = Get-LabTfsFeed -ComputerName $nugetServer -FeedName PowerShell
$pullServer = Get-LabVM -Role DSCPullServer
$hypervHost = Get-LabVM -Role HyperV
$devOpsRole = $devOpsServer.Roles | Where-Object Name -like AzDevOps
$devOpsCred = $devOpsServer.GetCredential($lab)
$devOpsPort = $originalPort = 8080
if ($devOpsRole.Properties.ContainsKey('Port')) {
$devOpsPort = $devOpsRole.Properties['Port']
}
if ($lab.DefaultVirtualizationEngine -eq 'Azure') {
$devOpsPort = (Get-LabAzureLoadBalancedPort -DestinationPort $devOpsPort -ComputerName $devOpsServer).Port
}
# Which will make use of Azure DevOps, clone the stuff, add the necessary build step, publish the test results and so on
# You will see two remotes, Origin (Our code on GitHub) and Azure DevOps (Our code pushed to your lab)
Write-ScreenInfo 'Creating Azure DevOps project and cloning from GitHub...' -NoNewLine
New-LabReleasePipeline -ProjectName $projectName -SourceRepository $projectGitUrl -CodeUploadMethod FileCopy
$tfsAgentQueue = Get-TfsAgentQueue -InstanceName $devOpsHostName -Port $devOpsPort -Credential $devOpsCred -ProjectName $projectName -CollectionName $collectionName -QueueName Default -UseSsl -SkipCertificateCheck
#region Release Definitions
$releaseSteps = @(
@{
taskId = '5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c'
version = '2.*'
name = 'Copy Files to: Artifacte Share'
enabled = $true
condition = 'succeeded()'
inputs = @{
SourceFolder = '$(System.DefaultWorkingDirectory)/$(Release.PrimaryArtifactSourceAlias)'
Contents = '**'
TargetFolder = '\\{0}\Artifacts\$(Build.DefinitionName)\$(Build.BuildNumber)\$(Build.Repository.Name)' -f $devOpsServer.FQDN
}
}
@{
enabled = $true
name = 'Register PowerShell Gallery'
taskId = 'e213ff0f-5d5c-4791-802d-52ea3e7be1f1'
version = '2.*'
inputs = @{
targetType = 'inline'
script = @'
#always make sure the local PowerShell Gallery is registered correctly
$uri = '$(RepositoryUri)'
$name = 'PowerShell'
$r = Get-PSRepository -Name $name -ErrorAction SilentlyContinue
if (-not $r -or $r.SourceLocation -ne $uri -or $r.PublishLocation -ne $uri) {
Write-Host "The Source or PublishLocation of the repository '$name' is not correct or the repository is not registered"
Unregister-PSRepository -Name $name -ErrorAction SilentlyContinue
Register-PSRepository -Name $name -SourceLocation $uri -PublishLocation $uri -InstallationPolicy Trusted
Get-PSRepository
}
'@
}
}
@{
enabled = $true
name = "Print Environment Variables"
taskid = 'e213ff0f-5d5c-4791-802d-52ea3e7be1f1'
version = '2.*'
inputs = @{
targetType = "inline"
script = 'dir -Path env:'
}
}
@{
enabled = $true
name = "Execute Build.ps1 for Deployment"
taskId = 'e213ff0f-5d5c-4791-802d-52ea3e7be1f1'
version = '2.*'
inputs = @{
targetType = 'inline'
script = @'
Write-Host $(System.DefaultWorkingDirectory)
Set-Location -Path '$(System.DefaultWorkingDirectory)\$(Release.PrimaryArtifactSourceAlias)\SourcesDirectory\DSC'
.\Build.ps1 -Tasks Init, SetPsModulePath, Deploy, TestBuildAcceptance -Repository PowerShell
'@
}
}
@{
enabled = $true
name = 'Publish Build Acceptance Test Results'
condition = 'always()'
taskid = '0b0f01ed-7dde-43ff-9cbb-e48954daf9b1'
version = '*'
inputs = @{
testRunner = 'NUnit'
testResultsFiles = '**/BuildAcceptanceTestResults.xml'
searchFolder = '$(System.DefaultWorkingDirectory)'
}
}
@{
taskId = '3b5693d4-5777-4fee-862a-bd2b7a374c68'
version = '3.*'
name = 'Create Dev Test Machines'
enabled = $false
inputs = @{
Machines = $hypervHost.FQDN
UserName = '$(InstallUserName)'
UserPassword = '$(InstallUserPassword)'
ScriptType = 'Inline'
InlineScript = @'
[System.Environment]::SetEnvironmentVariable('AUTOMATEDLAB_TELEMETRY_OPTOUT', '0')
Import-Module -Name AutomatedLab
$dc = Get-ADDomainController
$netAdapter = Get-NetAdapter -Name 'vEthernet (AlExternal)' -ErrorAction SilentlyContinue
if (-not $netAdapter)
{
$netAdapter = Get-NetAdapter -Name Ethernet -ErrorAction SilentlyContinue
}
$ip = $netAdapter | Get-NetIPAddress -AddressFamily IPv4
$network = [AutomatedLab.IPNetwork]"$($ip.IPAddress)/$($ip.PrefixLength)"
#--------------------------------
New-LabDefinition -Name Lab1 -DefaultVirtualizationEngine HyperV
$os = Get-LabAvailableOperatingSystem | Where-Object { $_.OperatingSystemName -like '*Datacenter*' -and $_.OperatingSystemName -like '*2019*' -and $_.OperatingSystemName -like '*Desktop*' }
$PSDefaultParameterValues = @{
'Add-LabMachineDefinition:OperatingSystem'= $os
'Add-LabMachineDefinition:Memory'= 1GB
'Add-LabMachineDefinition:DomainName'= $dc.Domain
'Add-LabMachineDefinition:DnsServer1' = $dc.IPv4Address
'Add-LabMachineDefinition:Gateway' = (Get-NetIPConfiguration).IPv4DefaultGateway.NextHop
}
Add-LabVirtualNetworkDefinition -Name AlExternal -AddressSpace "$($ip.IPAddress)/$($ip.PrefixLength)" -HyperVProperties @{ SwitchType = 'External'; AdapterName = 'Ethernet' }
$param = @{
Name = $dc.Name
Roles = 'RootDC'
IpAddress = $dc.IPv4Address
SkipDeployment = $true
}
Add-LabMachineDefinition @param
$param = @{
Name = 'DSCFile01'
Roles = 'FileServer'
IpAddress = [AutomatedLab.IPNetwork]::ListIPAddress($network)[100]
}
Add-LabMachineDefinition @param
$param = @{
Name = 'DSCWeb01'
Roles = 'WebServer'
IpAddress = [AutomatedLab.IPNetwork]::ListIPAddress($network)[101]
}
Add-LabMachineDefinition @param
Install-Lab
Show-LabDeploymentSummary -Detailed
'@
CommunicationProtocol = 'Http'
AuthenticationMechanism = 'Credssp'
NewPsSessionOptionArguments = '-SkipCACheck -IdleTimeout 7200000 -OperationTimeout 0 -OutputBufferingMode Block'
}
}
@{
enabled = $false
name = 'Wait'
taskId = 'e213ff0f-5d5c-4791-802d-52ea3e7be1f1'
version = '2.*'
inputs = @{
targetType = 'inline'
script = @'
Start-Sleep -Seconds 30
'@
}
}
@{
taskId = '3b5693d4-5777-4fee-862a-bd2b7a374c68'
version = '3.*'
name = 'Remove Dev Test Machines'
enabled = $false
inputs = @{
Machines = $hypervHost.FQDN
UserName = '$(InstallUserName)'
UserPassword = '$(InstallUserPassword)'
ScriptType = 'Inline'
InlineScript = @'
[System.Environment]::SetEnvironmentVariable('AUTOMATEDLAB_TELEMETRY_OPTOUT', '0')
Import-Module -Name AutomatedLab
Remove-Lab -Name Lab1 -Confirm:$false
'@
CommunicationProtocol = 'Http'
AuthenticationMechanism = 'Credssp'
NewPsSessionOptionArguments = '-SkipCACheck -IdleTimeout 7200000 -OperationTimeout 0 -OutputBufferingMode Block'
}
}
)
$releaseEnvironments = @(
@{
id = 3
name = 'Dev'
rank = 1
owner = @{
displayName = 'Install'
id = '196672db-49dd-4968-8c52-a94e43186ffd'
uniqueName = 'Install'
}
variables = @{
RepositoryUri = @{ value = $nugetFeed.NugetV2Url }
InstallUserName = @{ value = $devOpsCred.UserName }
InstallUserPassword = @{ value = $devOpsCred.GetNetworkCredential().Password }
DscConfiguration = @{ value = "\\$($pullServer.FQDN)\DscConfiguration" }
DscModules = @{ value = "\\$($pullServer.FQDN)\DscModules" }
}
preDeployApprovals = @{
approvals = @(
@{
rank = 1
isAutomated = $true
isNotificationOn = $false
id = 7
}
)
}
deployStep = @{ id = 10 }
postDeployApprovals = @{
approvals = @(
@{
rank = 1
isAutomated = $true
isNotificationOn = $false
id = 11
}
)
}
deployPhases = @(
@{
deploymentInput = @{
parallelExecution = @{ parallelExecutionType = 'none' }
skipArtifactsDownload = $false
artifactsDownloadInput = @{ downloadInputs = $() }
queueId = $tfsAgentQueue.id
demands = @()
enableAccessToken = $false
timeoutInMinutes = 0
jobCancelTimeoutInMinutes = 1
condition = 'succeeded()'
overrideInputs = @{ }
}
rank = 1
phaseType = 1
name = 'Run on agent'
workflowTasks = $releaseSteps
}
)
environmentOptions = @{
emailNotificationType = 'OnlyOnFailure'
emailRecipients = 'release.environment.owner;release.creator'
skipArtifactsDownload = $false
timeoutInMinutes = 0
enableAccessToken = $false
publishDeploymentStatus = $true
badgeEnabled = $false
autoLinkWorkItems = $false
}
demands = @()
conditions = @(
@{
name = 'ReleaseStarted'
conditionType = 1
}
)
executionPolicy = @{
concurrencyCount = 0
queueDepthCount = 0
}
schedules = @()
retentionPolicy = @{
daysToKeep = 30
releasesToKeep = 3
retainBuild = $true
}
processParameters = @{ }
properties = @{ }
preDeploymentGates = @{
id = 0
gatesOptions = $null
gates = @()
}
postDeploymentGates = @{
id = 0
gatesOptions = $null
gates = @()
}
}
@{
id = 4
name = 'Test'
rank = 2
owner = @{
displayName = 'Install'
id = '196672db-49dd-4968-8c52-a94e43186ffd'
uniqueName = 'Install'
}
variables = @{
RepositoryUri = @{ value = $nugetFeed.NugetV2Url }
InstallUserName = @{ value = $devOpsCred.UserName }
InstallUserPassword = @{ value = $devOpsCred.GetNetworkCredential().Password }
DscConfiguration = @{ value = "\\$($pullServer.FQDN)\DscConfiguration" }
DscModules = @{ value = "\\$($pullServer.FQDN)\DscModules" }
}
preDeployApprovals = @{
approvals = @(
@{
rank = 1
isAutomated = $true
isNotificationOn = $false
id = 7
}
)
}
deployStep = @{ id = 10 }
postDeployApprovals = @{
approvals = @(
@{
rank = 1
isAutomated = $true
isNotificationOn = $false
id = 11
}
)
}
deployPhases = @(
@{
deploymentInput = @{
parallelExecution = @{ parallelExecutionType = 'none' }
skipArtifactsDownload = $false
artifactsDownloadInput = @{ downloadInputs = $() }
queueId = $tfsAgentQueue.id
demands = @()
enableAccessToken = $false
timeoutInMinutes = 0
jobCancelTimeoutInMinutes = 1
condition = 'succeeded()'
overrideInputs = @{ }
}
rank = 1
phaseType = 1
name = 'Run on agent'
workflowTasks = $releaseSteps | Select-Object -Skip 1
}
)
environmentOptions = @{
emailNotificationType = 'OnlyOnFailure'
emailRecipients = 'release.environment.owner;release.creator'
skipArtifactsDownload = $false
timeoutInMinutes = 0
enableAccessToken = $false
publishDeploymentStatus = $true
badgeEnabled = $false
autoLinkWorkItems = $false
}
demands = @()
conditions = @(
@{
name = 'Dev'
conditionType = 2
value = 4
}
@{
name = 'DscWorkshop CI'
conditionType = 4
value = '{"sourceBranch":"master","tags":[],"useBuildDefinitionBranch":false}'
}
)
executionPolicy = @{
concurrencyCount = 0
queueDepthCount = 0
}
schedules = @()
retentionPolicy = @{
daysToKeep = 30
releasesToKeep = 3
retainBuild = $true
}
processParameters = @{ }
properties = @{ }
preDeploymentGates = @{
id = 0
gatesOptions = $null
gates = @()
}
postDeploymentGates = @{
id = 0
gatesOptions = $null
gates = @()
}
}
@{
id = 5
name = "Prod"
rank = 3
owner = @{
displayName = 'Install'
id = '196672db-49dd-4968-8c52-a94e43186ffd'
uniqueName = 'Install'
}
variables = @{
RepositoryUri = @{ value = $nugetFeed.NugetV2Url }
InstallUserName = @{ value = $devOpsCred.UserName }
InstallUserPassword = @{ value = $devOpsCred.GetNetworkCredential().Password }
DscConfiguration = @{ value = "\\$($pullServer.FQDN)\DscConfiguration" }
DscModules = @{ value = "\\$($pullServer.FQDN)\DscModules" }
}
preDeployApprovals = @{
approvals = @(
@{
rank = 1
isAutomated = $false
isNotificationOn = $false
approver = @{
displayName = 'Install'
id = '196672db-49dd-4968-8c52-a94e43186ffd'
uniqueName = $devOpsCred.UserName
}
}
)
}
deployStep = @{ id = 10 }
postDeployApprovals = @{
approvals = @(
@{
rank = 1
isAutomated = $true
isNotificationOn = $false
id = 11
}
)
}
deployPhases = @(
@{
deploymentInput = @{
parallelExecution = @{ parallelExecutionType = 'none' }
skipArtifactsDownload = $false
artifactsDownloadInput = @{ downloadInputs = $() }
queueId = $tfsAgentQueue.id
demands = @()
enableAccessToken = $false
timeoutInMinutes = 0
jobCancelTimeoutInMinutes = 1
condition = 'succeeded()'
overrideInputs = @{ }
}
rank = 1
phaseType = 1
name = 'Run on agent'
workflowTasks = $releaseSteps | Select-Object -Skip 1
}
)
environmentOptions = @{
emailNotificationType = 'OnlyOnFailure'
emailRecipients = 'release.environment.owner;release.creator'
skipArtifactsDownload = $false
timeoutInMinutes = 0
enableAccessToken = $false
publishDeploymentStatus = $true
badgeEnabled = $false
autoLinkWorkItems = $false
}
demands = @()
conditions = @(
@{
name = 'Test'
conditionType = 2
value = 4
}
@{
name = 'DscWorkshop CI'
conditionType = 4
value = '{"sourceBranch":"master","tags":[],"useBuildDefinitionBranch":false}'
}
)
executionPolicy = @{
concurrencyCount = 0
queueDepthCount = 0
}
schedules = @()
retentionPolicy = @{
daysToKeep = 30
releasesToKeep = 3
retainBuild = $true
}
processParameters = @{ }
properties = @{ }
preDeploymentGates = @{
id = 0
gatesOptions = $null
gates = @()
}
postDeploymentGates = @{
id = 0
gatesOptions = $null
gates = @()
}
}
)
#endregion Build and Release Definitions
$repo = Get-TfsGitRepository -InstanceName $devOpsHostName -Port $devOpsPort -CollectionName $collectionName -ProjectName $projectName -Credential $devOpsCred -UseSsl -SkipCertificateCheck
$param = @{
Uri = "https://$($devOpsHostName):$devOpsPort/$collectionName/_apis/git/repositories/{$($repo.id)}/refs?api-version=4.1"
Credential = $devOpsCred
}
if ($PSVersionTable.PSEdition -eq 'Core') {
$param.Add('SkipCertificateCheck', $true)
}
Invoke-LabCommand -ActivityName 'Set RepositoryUri and create Build Pipeline' -ScriptBlock {
Set-Location -Path C:\Git\DscWorkshop
git checkout dev *>$null
$c = Get-Content '.\azure-pipelines On-Prem.yml' -Raw
$c = $c -replace ' RepositoryUri: ggggg', " RepositoryUri: $($nugetFeed.NugetV2Url)"
$c | Set-Content '.\azure-pipelines.yml'
git add .
git commit -m 'Set RepositoryUri and create Build Pipeline'
git push 2>$null
} -ComputerName $devOpsServer -Variable (Get-Variable -Name nugetFeed)
Start-Sleep -Seconds 10
$releaseParameters = @{
ProjectName = $projectName
InstanceName = $devOpsHostName
Port = $devOpsPort
ReleaseName = "$($projectName) CD"
Environments = $releaseEnvironments
Credential = $devOpsCred
CollectionName = $collectionName
UseSsl = $true
SkipCertificateCheck = $true
}
New-TfsReleaseDefinition @releaseParameters
Write-ScreenInfo done
# in case you screw something up
Checkpoint-LabVM -All -SnapshotName AfterPipelines
Write-Host "3. - Creating Snapshot 'AfterPipelines'" -ForegroundColor Magenta