-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
03_azure-pipelines-ci-cd.yml
389 lines (343 loc) · 13 KB
/
03_azure-pipelines-ci-cd.yml
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
pr:
branches:
include:
- dev
paths:
exclude:
- docs/*
- README.md
trigger:
branches:
include:
- master
paths:
exclude:
- docs/*
- README.md
- 01_azure-pipelines-ci.yml
- 02_azure-pipelines-ci.yml
- 03_azure-pipelines-ci-cd.yml
parameters:
- name: runCompletePipeline
displayName: Run All Tasks ?
type: boolean
default: true
stages:
- stage: Build_Stage
displayName: Build Apps
jobs:
- job: WebApp
displayName: Build Web App
pool:
name: Hosted Windows 2019 with VS2019
vmImage: 'windows-2019'
variables:
BuildConfiguration: release
steps:
- task: UseDotNet@2
displayName: Install .NET 6 sdk
inputs:
packageType: sdk
version: 6.0.x
installationPath: $(Agent.ToolsDirectory)/dotnet
- task: DotNetCoreCLI@2
displayName: Restore Nuget Packages
inputs:
command: restore
projects: '**/WebApp.csproj'
- task: WhiteSource Bolt@20
displayName: Scan Nuget Packages
enabled: ${{ parameters.runCompletePipeline }}
- task: PowerShell@2
displayName: Prepare for Sonar Cloud
enabled: ${{ parameters.runCompletePipeline }}
inputs:
targetType: 'inline'
script: |
$paths = Get-ChildItem -include *.csproj -Recurse
foreach($pathobject in $paths)
{
$path = $pathobject.fullname
$doc = New-Object System.Xml.XmlDocument
$doc.Load($path)
$child = $doc.CreateElement("ProjectGuid")
$child.InnerText = [guid]::NewGuid().ToString().ToUpper()
$node = $doc.SelectSingleNode("//Project/PropertyGroup")
$node.AppendChild($child)
$doc.Save($path)
}
workingDirectory: 'WebApp'
- task: SonarCloudPrepare@1
displayName: Prepare analysis on SonarCloud
enabled: ${{ parameters.runCompletePipeline }}
inputs:
SonarCloud: SonarCloud
organization: houssemdellai
projectKey: WebAppWithDatabaseDemo
- task: DotNetCoreCLI@2
displayName: Build WebApp
inputs:
projects: '**/WebApp.csproj'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI@2
displayName: Run Unit Tests
enabled: ${{ parameters.runCompletePipeline }}
inputs:
command: test
projects: '**/*UnitTest*.csproj'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI@2
displayName: Create WebApp.zip
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)'
zipAfterPublish: True
- task: SonarCloudAnalyze@1
displayName: Run Code Analysis
enabled: ${{ parameters.runCompletePipeline }}
- task: SonarCloudPublish@1
displayName: Publish Quality Gate Result
enabled: ${{ parameters.runCompletePipeline }}
- task: PublishBuildArtifacts@1
displayName: Publish Artifact (WebApp.zip)
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: drop
- job: Database
displayName: Build Database
pool:
vmImage: 'windows-2019'
demands: msbuild
steps:
- task: MSBuild@1
displayName: Build WebApp.Database.sqlproj
inputs:
solution: WebApp.Database/WebApp.Database.sqlproj
msbuildArguments: '/p:OutDir=$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
displayName: Publish Artifact (Dacpac)
inputs:
ArtifactName: dacpac
- job: Selenium
displayName: Build UI Tests
pool:
vmImage: 'windows-2019'
demands: msbuild
steps:
- task: NuGetToolInstaller@0
displayName: Use NuGet 4.3.0
- task: NuGetCommand@2
displayName: Restoore NuGet Packages
inputs:
restoreSolution: WebAppWithDatabase.sln
- task: MSBuild@1
displayName: Build SeleniumUiTests.csproj
inputs:
solution: SeleniumUiTests/SeleniumUiTests.csproj
msbuildArguments: '/p:OutDir=$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: UI-Test'
inputs:
ArtifactName: ui-tests
- job: Infrastructure
displayName: Publish Infra files
pool:
vmImage: 'ubuntu-latest'
steps:
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: ARM templates'
inputs:
PathtoPublish: AzureResourceGroupDeployment
ArtifactName: arm
- stage: Dev_Stage
displayName: Create & Deploy to Dev
dependsOn: Build_Stage
variables:
azureSubscription: 'WebAppWithDatabaseDemo_spn' # couldn't be referenced
ResourceGroupName: '$(Prefix)-$(Release.EnvironmentName)-$(UniqueId)-RG'
Database.Admin: 'houssem'
Database.Password: '@Aa123456' # to be secured in Key Vault
Database.Name: 'EmployeesDB'
WebAppName: '$(Prefix)-$(Release.EnvironmentName)-$(UniqueId)'
WebAppNameUrl: 'https://$(WebAppName).azurewebsites.net/'
SqlServerName: '$(Prefix)-sql-$(Release.EnvironmentName)-$(UniqueId)'
Prefix: 'tunis'
Release.EnvironmentName: 'dev'
UniqueId: '1280' #'$(Build.BuildId)'
hostingPlanName: '$(Prefix)-service-plan'
jobs:
- job: Create_DEV
displayName: Create DEV
pool:
vmImage: 'windows-2019'
steps:
- checkout: none
- task: DownloadBuildArtifacts@0
displayName: Download ARM templates
inputs:
artifactName: arm
downloadPath: $(System.DefaultWorkingDirectory)
- task: AzureResourceGroupDeployment@2
displayName: Validate ARM templates
enabled: ${{ parameters.runCompletePipeline }}
inputs:
azureSubscription: 'WebAppWithDatabaseDemo_spn'
action: 'Create Or Update Resource Group'
resourceGroupName: '$(ResourceGroupName)'
location: 'West Europe'
templateLocation: 'Linked artifact'
csmFile: '$(System.DefaultWorkingDirectory)/arm/WebSiteSQLDatabase.json'
csmParametersFile: '$(System.DefaultWorkingDirectory)/arm/WebSiteSQLDatabase.parameters.json'
overrideParameters: '-hostingPlanName $(hostingPlanName) -skuName "F1" -skuCapacity 1 -administratorLogin $(Database.Admin) -administratorLoginPassword $(Database.Password) -databaseName $(Database.Name) -collation "SQL_Latin1_General_CP1_CI_AS" -edition "Basic" -maxSizeBytes "1073741824" -requestedServiceObjectiveName "Basic" -webSiteName $(WebAppName) -sqlserverName $(SqlServerName)'
deploymentMode: 'Validation'
- task: AzSKARMTemplateChecker@4
displayName: Scan ARM templates using AzSK
enabled: ${{ parameters.runCompletePipeline }}
continueOnError: true
inputs:
ARMTemplateFilePath: '$(System.DefaultWorkingDirectory)/arm/WebSiteSQLDatabase.json'
ARMTemplateParameterFilePath: '$(System.DefaultWorkingDirectory)/arm/WebSiteSQLDatabase.parameters.json'
# scan ARM template using TTK
- task: RunARMTTKTests@1
displayName: Scan ARM templates using TTK
inputs:
templatelocation: '$(System.DefaultWorkingDirectory)/arm/'
resultLocation: '$(System.DefaultWorkingDirectory)/arm/results'
allTemplatesMain: false
cliOutputResults: true
ignoreExitCode: true
# includeTests: 'VM Images Should Use Latest Version,Resources Should Have Location'
# skipTests: 'VM Images Should Use Latest Version,Resources Should Have Location'
- task: PublishTestResults@2
displayName: Publish TTK Test Results
inputs:
testResultsFormat: 'NUnit'
testResultsFiles: '$(System.DefaultWorkingDirectory)\arm\results\*-armttk.xml'
condition: always()
- task: AzureCLI@2
displayName: Preview Template Changes (What-If)
inputs:
workingDirectory: $(System.DefaultWorkingDirectory)/arm/
azureSubscription: 'WebAppWithDatabaseDemo_spn'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: 'az deployment group what-if --resource-group $(ResourceGroupName) --name rollout01 --template-file WebSiteSQLDatabase.json --parameters WebSiteSQLDatabase.parameters.json'
arguments: '-hostingPlanName $(hostingPlanName) -skuName "F1" -skuCapacity 1 -administratorLogin $(Database.Admin) -administratorLoginPassword $(Database.Password) -databaseName $(Database.Name) -collation "SQL_Latin1_General_CP1_CI_AS" -edition "Basic" -maxSizeBytes "1073741824" -requestedServiceObjectiveName "Basic" -webSiteName $(WebAppName) -sqlserverName $(SqlServerName)'
- task: AzureResourceManagerTemplateDeployment@3
displayName: Deploy ARM templates
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: 'WebAppWithDatabaseDemo_spn'
subscriptionId: '4b72ed90-7ca3-4e76-8d0f-31a2c0bee7a3'
action: 'Create Or Update Resource Group'
resourceGroupName: '$(ResourceGroupName)'
location: 'West Europe'
templateLocation: 'Linked artifact'
csmFile: '$(System.DefaultWorkingDirectory)/arm/WebSiteSQLDatabase.json'
csmParametersFile: '$(System.DefaultWorkingDirectory)/arm/WebSiteSQLDatabase.parameters.json'
overrideParameters: '-hostingPlanName $(hostingPlanName) -skuName "F1" -skuCapacity 1 -administratorLogin $(Database.Admin) -administratorLoginPassword $(Database.Password) -databaseName $(Database.Name) -collation "SQL_Latin1_General_CP1_CI_AS" -edition "Basic" -maxSizeBytes "1073741824" -requestedServiceObjectiveName "Basic" -webSiteName $(WebAppName) -sqlserverName $(SqlServerName)'
deploymentMode: 'Complete'
- task: AzSKSVTs@4
displayName: Scan Azure Resources
enabled: ${{ parameters.runCompletePipeline }}
continueOnError: true
inputs:
ConnectedServiceNameARM: 'WebAppWithDatabaseDemo_spn'
GenerateMethodParameterSetSelection: 'ResourceGroupName'
ResourceGroupName: '$(ResourceGroupName)'
SubscriptionId: '4b72ed90-7ca3-4e76-8d0f-31a2c0bee7a3'
EnableGSSscan: true
- job: Deploy_DEV
displayName: Deploy Apps to DEV
dependsOn: Create_DEV
pool:
vmImage: 'windows-2019'
steps:
- checkout: none
# new JOB: Deploy App & Database
- task: DownloadBuildArtifacts@0
displayName: Download WebApp.zip
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
itemPattern: '**/WebApp.zip'
downloadPath: '$(System.DefaultWorkingDirectory)'
- task: AzureRmWebAppDeployment@3
displayName: Deploy WebApp to Azure
inputs:
azureSubscription: 'WebAppWithDatabaseDemo_spn'
appType: 'app'
WebAppName: '$(WebAppName)'
Package: '$(System.DefaultWorkingDirectory)/drop/WebApp.zip'
TakeAppOfflineFlag: true
JSONFiles: '**/appsettings.json'
- task: DownloadBuildArtifacts@0
displayName: Download DacPac
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'dacpac'
itemPattern: '**/*.dacpac'
downloadPath: '$(System.DefaultWorkingDirectory)'
- task: SqlAzureDacpacDeployment@1
displayName: Deploy DacPac to SQL Azure
inputs:
azureSubscription: 'WebAppWithDatabaseDemo_spn'
AuthenticationType: 'server'
ServerName: '$(SqlServerName).database.windows.net,1433'
DatabaseName: '$(Database.Name)'
SqlUsername: '$(Database.Admin)'
SqlPassword: '$(Database.Password)'
deployType: 'DacpacTask'
DeploymentAction: 'Publish'
DacpacFile: '$(System.DefaultWorkingDirectory)/dacpac/WebApp.Database.dacpac'
IpDetectionMethod: 'AutoDetect'
# new JOB: Selenium
- job: Test_DEV
displayName: Run Selenium tests in DEV
dependsOn: Deploy_DEV
pool:
vmImage: 'windows-2019'
steps:
- checkout: none
- task: DownloadBuildArtifacts@0
displayName: Download Selenium Tests
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'ui-tests'
itemPattern: ''
downloadPath: '$(System.DefaultWorkingDirectory)'
- task: VSTest@2
displayName: Run Selenium UI Tests
inputs:
testSelector: 'testAssemblies'
searchFolder: '$(System.DefaultWorkingDirectory)/ui-tests'
runSettingsFile: '$(System.DefaultWorkingDirectory)/ui-tests/.runsettings'
overrideTestrunParameters: '-webAppUrl $(WebAppNameUrl)'
testAssemblyVer2: |
**\*Test*.dll
!**\*TestAdapter.dll
!**\obj\**
runInParallel: false
codeCoverageEnabled: true
- stage: Test_Stage
displayName: Test Stage
jobs:
- job:
displayName: Deploy to Test
steps:
- task: CmdLine@2
inputs:
script: 'ls'
- stage: Prod_Stage
displayName: Prod Stage
jobs:
- job:
displayName: Deploy to Prod
steps:
- task: CmdLine@2
inputs:
script: 'ls'