-
Notifications
You must be signed in to change notification settings - Fork 61
/
azure-pipelines.yml
585 lines (549 loc) · 23.6 KB
/
azure-pipelines.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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
variables:
databricks.host: https://westeurope.azuredatabricks.net
databricks.notebook.path: /Shared/MLFlow
databricks.cluster.name: ML
databricks.cluster.id:
databricks.cluster.spark_version: 7.3.x-cpu-ml-scala2.12
databricks.cluster.node_type_id: Standard_DS3_v2
databricks.cluster.driver_node_type_id: Standard_DS3_v2
databricks.cluster.autotermination_minutes: 15
databricks.cluster.workers.min: 1
databricks.cluster.workers.max: 4
databricks.job.train.name: Wine Quality (Train)
databricks.job.train.id:
azureml.sdk: azureml-sdk[databricks]==1.23.0
databricks.job.buildimage.name: Wine Quality (Build Container Image)
azureml.image.id:
databricks.job.deploytoaci.name: Wine Quality (Deploy To ACI)
databricks.job.deploytoaci.id:
databricks.job.deploytoaks.name: Wine Quality (Deploy To AKS)
databricks.job.deploytoaks.id:
stages:
- stage: Build
displayName: 'Train, Evaluate & Register Model'
jobs:
- job: Train
displayName: 'Train, Evaluate & Register Model'
pool:
vmImage: 'ubuntu-18.04'
steps:
- task: UsePythonVersion@0
displayName: 'Use Python 3.6'
inputs:
versionSpec: '3.6'
addToPath: true
architecture: 'x64'
- task: Bash@3
displayName: 'Install Databricks CLI'
inputs:
targetType: 'inline'
script: 'pip install -U databricks-cli'
- task: Bash@3
displayName: 'Configure Databricks CLI'
inputs:
targetType: 'inline'
script: |
# We need to write the pipe the conf into databricks configure --token since
# that command only takes inputs from stdin.
conf=`cat << EOM
$(databricks.host)
$(databricks.token)
EOM`
# For password auth there are three lines expected
# hostname, username, password
echo "$conf" | databricks configure --token
- task: Bash@3
displayName: 'Create Notebook Path'
inputs:
targetType: 'inline'
script: 'databricks workspace mkdirs "$(databricks.notebook.path)"'
- task: Bash@3
displayName: 'Import Notebooks'
inputs:
targetType: 'inline'
script: 'databricks workspace import_dir --overwrite notebooks "$(databricks.notebook.path)"'
- task: Bash@3
displayName: 'Create / Get Cluster'
inputs:
targetType: 'inline'
script: |
cluster_id=$(databricks clusters list | grep "$(databricks.cluster.name)" | awk '{print $1}')
if [ -z "$cluster_id" ]
then
JSON=`cat << EOM
{
"cluster_name": "$(databricks.cluster.name)",
"spark_version": "$(databricks.cluster.spark_version)",
"spark_conf": {
"spark.databricks.delta.preview.enabled": "true"
},
"node_type_id": "$(databricks.cluster.node_type_id)",
"driver_node_type_id": "$(databricks.cluster.driver_node_type_id)",
"spark_env_vars": {
"PYSPARK_PYTHON": "/databricks/python3/bin/python3"
},
"autotermination_minutes": $(databricks.cluster.autotermination_minutes),
"enable_elastic_disk": true,
"autoscale": {
"min_workers": $(databricks.cluster.workers.min),
"max_workers": $(databricks.cluster.workers.max)
},
"init_scripts_safe_mode": false
}
EOM`
cluster_id=$(databricks clusters create --json "$JSON" | jq -r ".cluster_id")
sleep 10
fi
echo "##vso[task.setvariable variable=databricks.cluster.id;]$cluster_id"
- task: Bash@3
displayName: 'Start Cluster'
inputs:
targetType: 'inline'
script: |
echo "Checking Cluster State (Cluster ID: $(databricks.cluster.id))..."
cluster_state=$(databricks clusters get --cluster-id "$(databricks.cluster.id)" | jq -r ".state")
echo "Cluster State: $cluster_state"
if [ $cluster_state == "TERMINATED" ]
then
echo "Starting Databricks Cluster..."
databricks clusters start --cluster-id "$(databricks.cluster.id)"
sleep 30
cluster_state=$(databricks clusters get --cluster-id "$(databricks.cluster.id)" | jq -r ".state")
echo "Cluster State: $cluster_state"
fi
while [ $cluster_state == "PENDING" ]
do
sleep 30
cluster_state=$(databricks clusters get --cluster-id "$(databricks.cluster.id)" | jq -r ".state")
echo "Cluster State: $cluster_state"
done
if [ $cluster_state == "RUNNING" ]
then
exit 0
else
exit 1
fi
- task: Bash@3
displayName: 'Install Azure ML SDK'
inputs:
targetType: 'inline'
script: |
library_status=$(databricks libraries list --cluster-id $(databricks.cluster.id) | jq -c '.library_statuses[] | select( .library.pypi.package == "$(azureml.sdk)" ) | .status' -r)
if [ -z "$library_status" ]
then
echo "Installing $(azureml.sdk) library to $(databricks.cluster.id)..."
databricks libraries install --cluster-id "$(databricks.cluster.id)" --pypi-package "$(azureml.sdk)"
sleep 10
library_status=$(databricks libraries list --cluster-id $(databricks.cluster.id) | jq -c '.library_statuses[] | select( .library.pypi.package == "$(azureml.sdk)" ) | .status' -r)
echo "Library Status: $library_status"
fi
while [ $library_status == "PENDING" -o $library_status == "INSTALLING" ]
do
sleep 30
library_status=$(databricks libraries list --cluster-id $(databricks.cluster.id) | jq -c '.library_statuses[] | select( .library.pypi.package == "$(azureml.sdk)" ) | .status' -r)
echo "Library Status: $library_status"
done
if [ $library_status == "INSTALLED" ]
then
exit 0
else
exit 1
fi
- task: Bash@3
displayName: 'Create / Get Training Job'
inputs:
targetType: 'inline'
script: |
job_id=$(databricks jobs list | grep "$(databricks.job.train.name)" | awk '{print $1}')
if [ -z "$job_id" ]
then
echo "Creating $(databricks.job.train.name) job..."
JSON=`cat << EOM
{
"notebook_task": {
"notebook_path": "$(databricks.notebook.path)/train",
"base_parameters": {
"alpha": "0.5",
"l1_ratio": "0.5"
}
},
"existing_cluster_id": "$(databricks.cluster.id)",
"name": "$(databricks.job.train.name)",
"max_concurrent_runs": 3,
"timeout_seconds": 86400,
"libraries": [],
"email_notifications": {}
}
EOM`
job_id=$(databricks jobs create --json "$JSON" | jq ".job_id")
fi
echo "##vso[task.setvariable variable=databricks.job.train.id;]$job_id"
- task: Bash@3
displayName: 'Run Training Jobs'
inputs:
targetType: 'inline'
script: |
echo "Running job with ID $(databricks.job.train.id) with alpha=0.5, l1_ratio=0.5..."
run_id1=$(databricks jobs run-now --job-id $(databricks.job.train.id) --notebook-params '{ "alpha": "0.5", "l1_ratio": "0.5" }' | jq ".run_id")
echo " Run ID: $run_id1"
run_state=$(databricks runs get --run-id $run_id1 | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id1): $run_state"
while [ $run_state == "RUNNING" -o $run_state == "PENDING" ]
do
sleep 30
run_state=$(databricks runs get --run-id $run_id1 | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id1): $run_state"
done
result_state1=$(databricks runs get --run-id $run_id1 | jq -r ".state.result_state")
state_message1=$(databricks runs get --run-id $run_id1 | jq -r ".state.state_message")
echo "Result State (ID $run_id1): $result_state1, Message: $state_message1"
echo "Running job with ID $(databricks.job.train.id) with alpha=0.3, l1_ratio=0.3..."
run_id2=$(databricks jobs run-now --job-id $(databricks.job.train.id) --notebook-params '{ "alpha": "0.3", "l1_ratio": "0.3" }' | jq ".run_id")
echo " Run ID: $run_id2"
echo "Running job with ID $(databricks.job.train.id) with alpha=0.1, l1_ratio=0.1..."
run_id3=$(databricks jobs run-now --job-id $(databricks.job.train.id) --notebook-params '{ "alpha": "0.1", "l1_ratio": "0.1" }' | jq ".run_id")
echo " Run ID: $run_id3"
run_state=$(databricks runs get --run-id $run_id2 | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id2): $run_state"
while [ $run_state == "RUNNING" -o $run_state == "PENDING" ]
do
sleep 30
run_state=$(databricks runs get --run-id $run_id2 | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id2): $run_state"
done
result_state2=$(databricks runs get --run-id $run_id2 | jq -r ".state.result_state")
state_message2=$(databricks runs get --run-id $run_id2 | jq -r ".state.state_message")
echo "Result State (ID $run_id2): $result_state2, Message: $state_message2"
run_state=$(databricks runs get --run-id $run_id3 | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id3): $run_state"
while [ $run_state == "RUNNING" -o $run_state == "PENDING" ]
do
sleep 30
run_state=$(databricks runs get --run-id $run_id3 | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id3): $run_state"
done
result_state3=$(databricks runs get --run-id $run_id3 | jq -r ".state.result_state")
state_message3=$(databricks runs get --run-id $run_id3 | jq -r ".state.state_message")
echo "Result State (ID $run_id3): $result_state3, Message: $state_message3"
if [ $result_state1 == "SUCCESS" -a $result_state2 == "SUCCESS" -a $result_state3 == "SUCCESS" ]
then
exit 0
else
exit 1
fi
- task: Bash@3
displayName: 'Build Container Image'
inputs:
targetType: 'inline'
script: |
JSON=`cat << EOM
{
"notebook_task": {
"notebook_path": "$(databricks.notebook.path)/serving_build_container_image"
},
"existing_cluster_id": "$(databricks.cluster.id)",
"run_name": "$(databricks.job.buildimage.name)",
"max_concurrent_runs": 1,
"timeout_seconds": 86400,
"libraries": [],
"email_notifications": {}
}
EOM`
echo "Building Container Image ..."
run_id=$(databricks runs submit --json "$JSON" | jq ".run_id")
echo " Run ID: $run_id"
run_state=$(databricks runs get --run-id $run_id | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id): $run_state"
while [ $run_state == "RUNNING" -o $run_state == "PENDING" ]
do
sleep 30
run_state=$(databricks runs get --run-id $run_id | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id): $run_state"
done
result_state=$(databricks runs get --run-id $run_id | jq -r ".state.result_state")
state_message=$(databricks runs get --run-id $run_id | jq -r ".state.state_message")
echo "Result State (ID $run_id): $result_state, Message: $state_message"
if [ $result_state == "SUCCESS" ]
then
mkdir -p metadata
databricks runs get-output --run-id $run_id | jq -r .notebook_output.result | tee metadata/image.json
exit 0
else
exit 1
fi
- task: CopyFiles@2
displayName: 'Copy Files to Artifact Staging Directory'
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: '**/metadata/*'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: drop'
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
- stage: Staging
displayName: 'Deploy to Staging'
dependsOn: Build
condition: succeeded()
jobs:
# track deployments on the environment
- deployment: DeployToACI
displayName: 'Deploy to Azure Container Instance'
pool:
vmImage: 'ubuntu-18.04'
# creates an environment if it doesn’t exist
environment: 'wine-quality-staging'
strategy:
# default deployment strategy
runOnce:
deploy:
steps:
- download: none
- task: DownloadBuildArtifacts@0
displayName: 'Download Artifact: drop'
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: '$(System.ArtifactsDirectory)'
- task: UsePythonVersion@0
displayName: 'Use Python 3.6'
inputs:
versionSpec: '3.6'
addToPath: true
architecture: 'x64'
- task: Bash@3
displayName: 'Install Databricks CLI'
inputs:
targetType: 'inline'
script: 'pip install -U databricks-cli'
- task: Bash@3
displayName: 'Configure Databricks CLI'
inputs:
targetType: 'inline'
script: |
# We need to write the pipe the conf into databricks configure --token since
# that command only takes inputs from stdin.
conf=`cat << EOM
$(databricks.host)
$(databricks.token)
EOM`
# For password auth there are three lines expected
# hostname, username, password
echo "$conf" | databricks configure --token
- task: Bash@3
displayName: 'Get Cluster ID'
inputs:
targetType: 'inline'
script: |
cluster_id=$(databricks clusters list | grep "$(databricks.cluster.name)" | awk '{print $1}')
if [ -z "$cluster_id" ]
then
echo "ERROR: Unable to get Cluster ID"
exit 1
fi
echo "##vso[task.setvariable variable=databricks.cluster.id;]$cluster_id"
- task: Bash@3
displayName: 'Create / Get Deploy ACI Job'
inputs:
targetType: 'inline'
script: |
job_id=$(databricks jobs list | grep "$(databricks.job.deploytoaci.name)" | awk '{print $1}')
if [ -z "$job_id" ]
then
JSON=`cat << EOM
{
"notebook_task": {
"notebook_path": "$(databricks.notebook.path)/serving_deploy_to_aci",
"base_parameters": {
"model_image_id": ""
}
},
"existing_cluster_id": "$(databricks.cluster.id)",
"name": "$(databricks.job.deploytoaci.name)",
"max_concurrent_runs": 1,
"timeout_seconds": 86400,
"libraries": [],
"email_notifications": {}
}
EOM`
job_id=$(databricks jobs create --json "$JSON" | jq ".job_id")
fi
echo "##vso[task.setvariable variable=databricks.job.deploytoaci.id;]$job_id"
- task: Bash@3
displayName: 'Get Image ID'
inputs:
targetType: 'inline'
script: |
echo "Retrieving Image ID..."
model_image_id=$(cat image.json | jq -r ".model_image_id")
if [ -z "$model_image_id" ]
then
echo "ERROR: Unable to get Image ID"
exit 1
fi
echo " Image ID: $model_image_id"
echo "##vso[task.setvariable variable=azureml.image.id;]$model_image_id"
workingDirectory: '$(System.ArtifactsDirectory)/drop/metadata'
- task: Bash@3
displayName: 'Deploy To ACI'
inputs:
targetType: 'inline'
script: |
echo "Running job with ID $(databricks.job.deploytoaci.id) with model_image_id=$(azureml.image.id)..."
run_id=$(databricks jobs run-now --job-id $(databricks.job.deploytoaci.id) --notebook-params '{ "model_image_id": "$(azureml.image.id)" }' | jq ".run_id")
echo " Run ID: $run_id"
run_state=$(databricks runs get --run-id $run_id | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id): $run_state"
while [ $run_state == "RUNNING" -o $run_state == "PENDING" ]
do
sleep 30
run_state=$(databricks runs get --run-id $run_id | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id): $run_state"
done
result_state=$(databricks runs get --run-id $run_id | jq -r ".state.result_state")
state_message=$(databricks runs get --run-id $run_id | jq -r ".state.state_message")
echo "Result State (ID $run_id): $result_state, Message: $state_message"
if [ $result_state == "SUCCESS" ]
then
exit 0
else
exit 1
fi
- stage: Production
displayName: 'Deploy to Production'
dependsOn: Staging
condition: succeeded()
jobs:
# track deployments on the environment
- deployment: DeployToAKS
displayName: 'Deploy to Azure Kubernetes Service'
pool:
vmImage: 'ubuntu-18.04'
# creates an environment if it doesn’t exist
environment: 'wine-quality-production'
strategy:
# default deployment strategy
runOnce:
deploy:
steps:
- download: none
- task: DownloadBuildArtifacts@0
displayName: 'Download Artifact: drop'
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: '$(System.ArtifactsDirectory)'
- task: UsePythonVersion@0
displayName: 'Use Python 3.6'
inputs:
versionSpec: '3.6'
addToPath: true
architecture: 'x64'
- task: Bash@3
displayName: 'Install Databricks CLI'
inputs:
targetType: 'inline'
script: 'pip install -U databricks-cli'
- task: Bash@3
displayName: 'Configure Databricks CLI'
inputs:
targetType: 'inline'
script: |
# We need to write the pipe the conf into databricks configure --token since
# that command only takes inputs from stdin.
conf=`cat << EOM
$(databricks.host)
$(databricks.token)
EOM`
# For password auth there are three lines expected
# hostname, username, password
echo "$conf" | databricks configure --token
- task: Bash@3
displayName: 'Get Cluster ID'
inputs:
targetType: 'inline'
script: |
cluster_id=$(databricks clusters list | grep "$(databricks.cluster.name)" | awk '{print $1}')
if [ -z "$cluster_id" ]
then
echo "ERROR: Unable to get Cluster ID"
exit 1
fi
echo "##vso[task.setvariable variable=databricks.cluster.id;]$cluster_id"
- task: Bash@3
displayName: 'Create / Get Deploy AKS Job'
inputs:
targetType: 'inline'
script: |
job_id=$(databricks jobs list | grep "$(databricks.job.deploytoaks.name)" | awk '{print $1}')
if [ -z "$job_id" ]
then
JSON=`cat << EOM
{
"notebook_task": {
"notebook_path": "$(databricks.notebook.path)/serving_deploy_to_aks",
"base_parameters": {
"model_image_id": ""
}
},
"existing_cluster_id": "$(databricks.cluster.id)",
"name": "$(databricks.job.deploytoaks.name)",
"max_concurrent_runs": 1,
"timeout_seconds": 86400,
"libraries": [],
"email_notifications": {}
}
EOM`
job_id=$(databricks jobs create --json "$JSON" | jq ".job_id")
fi
echo "##vso[task.setvariable variable=databricks.job.deploytoaks.id;]$job_id"
- task: Bash@3
displayName: 'Get Image ID'
inputs:
targetType: 'inline'
script: |
echo "Retrieving Image ID..."
model_image_id=$(cat image.json | jq -r ".model_image_id")
if [ -z "$model_image_id" ]
then
echo "ERROR: Unable to get Image ID"
exit 1
fi
echo " Image ID: $model_image_id"
echo "##vso[task.setvariable variable=azureml.image.id;]$model_image_id"
workingDirectory: '$(System.ArtifactsDirectory)/drop/metadata'
- task: Bash@3
displayName: 'Deploy To AKS'
inputs:
targetType: 'inline'
script: |
echo "Running job with ID $(databricks.job.deploytoaks.id) with model_id=$(azureml.image.id)..."
run_id=$(databricks jobs run-now --job-id $(databricks.job.deploytoaks.id) --notebook-params '{ "model_image_id": "$(azureml.image.id)" }' | jq ".run_id")
echo " Run ID: $run_id"
run_state=$(databricks runs get --run-id $run_id | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id): $run_state"
while [ $run_state == "RUNNING" -o $run_state == "PENDING" ]
do
sleep 30
run_state=$(databricks runs get --run-id $run_id | jq -r ".state.life_cycle_state")
echo "Run State (ID $run_id): $run_state"
done
result_state=$(databricks runs get --run-id $run_id | jq -r ".state.result_state")
state_message=$(databricks runs get --run-id $run_id | jq -r ".state.state_message")
echo "Result State (ID $run_id): $result_state, Message: $state_message"
if [ $result_state == "SUCCESS" ]
then
exit 0
else
exit 1
fi