forked from dsccommunity/DscWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31 New Release Pipeline CommonTasks.ps1
230 lines (218 loc) · 8.62 KB
/
31 New Release Pipeline CommonTasks.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
$projectName = 'CommonTasks'
$projectGitUrl = 'https://github.com/DscCommunity/CommonTasks'
$collectionName = 'AutomatedLab'
$lab = Get-Lab
$domain = $lab.Domains[0]
$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
$devOpsRole = $devOpsServer.Roles | Where-Object Name -eq 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)/$(Build.DefinitionName)/$(Build.Repository.Name)'
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)\$(Build.DefinitionName)\SourcesDirectory"
.\Build.ps1 -Tasks Init, SetPsModulePath, Deploy, TestReleaseAcceptance -Repository PowerShell
'@
}
}
@{
enabled = $true
name = 'Publish Build Acceptance Test Results'
condition = 'always()'
taskid = '0b0f01ed-7dde-43ff-9cbb-e48954daf9b1'
version = '*'
inputs = @{
testRunner = 'NUnit'
testResultsFiles = '**/AcceptanceTestResults.xml'
searchFolder = '$(System.DefaultWorkingDirectory)'
}
}
)
$releaseEnvironments = @(
@{
id = 3
name = "PowerShell Repository"
rank = 1
owner = @{
displayName = 'Install'
id = '196672db-49dd-4968-8c52-a94e43186ffd'
uniqueName = 'Install'
}
variables = @{
RepositoryUri = @{ value = $nugetFeed.NugetV2Url }
NugetApiKey = @{ value = $nugetFeed.NugetApiKey }
}
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 = $false
badgeEnabled = $false
autoLinkWorkItems = $false
}
demands = @()
conditions = @(
@{
name = 'ReleaseStarted'
conditionType = 1
value = ''
}
)
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
$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)
}
$refs = (Invoke-RestMethod @param).value.name
Invoke-LabCommand -ActivityName 'Set RepositoryUri and create Build Pipeline' -ScriptBlock {
Set-Location -Path C:\Git\CommonTasks
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
New-TfsReleaseDefinition -ProjectName $projectName -InstanceName $devOpsHostName -Port $devOpsPort -ReleaseName "$($projectName) CD" -Environments $releaseEnvironments -Credential $devOpsCred -CollectionName $collectionName -UseSsl -SkipCertificateCheck
Write-ScreenInfo done