-
Notifications
You must be signed in to change notification settings - Fork 6
/
Jenkinsfile
216 lines (196 loc) · 9.74 KB
/
Jenkinsfile
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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
pipeline {
agent any
parameters {
string(name: "ACCOUNT_ID", description: "", defaultValue: "")
string(name: "REGION", description: "", defaultValue: "")
string(name: "ROLE_NAME", description: "", defaultValue: "")
string(name: "SAGEMAKER_MODEL_PACKAGE_GROUP", description: "", defaultValue: "")
string(name: "SAGEMAKER_INSTANCE_COUNT", description: "", defaultValue: "1")
string(name: "SAGEMAKER_INSTANCE_TYPE", description: "", defaultValue: "ml.m5.xlarge")
}
stages {
stage("Get last Approved model") {
steps {
withAWS(credentials: "jenkins-aws-profile") {
script {
def model_arn = sh returnStdout: true, script: """
aws sagemaker list-model-packages \
--model-package-group-name ${params.SAGEMAKER_MODEL_PACKAGE_GROUP} \
--model-approval-status Approved \
--sort-order Descending \
--sort-by CreationTime \
--max-items 1 \
--region ${params.REGION} \
| grep -o '"ModelPackageArn": "[^"]*' \
| grep -o '[^"]*\$'
"""
model_arn = model_arn.trim()
model_arn = model_arn.replaceAll("\\s","")
def model_package_name = model_arn.tokenize("/")
env.model_package_name = model_package_name[-2] + "-" + model_package_name[-1]
echo "Model Package Name ${env.model_package_name}"
if(model_arn != "") {
env.model_package_arn = model_arn
} else {
error "No SageMaker Model trained available in the status Approved"
}
}
}
}
}
stage("Deploy SageMaker Model") {
steps {
withAWS(credentials: "jenkins-aws-profile") {
script {
def image_uri = sh returnStdout: true, script: """
aws sagemaker describe-model-package \
--model-package-name ${env.model_package_arn} \
--region ${params.REGION} \
| grep -o '"Image": "[^"]*' \
| grep -o '[^"]*\$'
"""
image_uri = image_uri.trim()
env.image_uri = image_uri.replaceAll("\\s","")
echo "Image URI ${env.image_uri}"
def model_url = sh returnStdout: true, script: """
aws sagemaker describe-model-package \
--model-package-name ${env.model_package_arn} \
--region ${params.REGION} \
| grep -o '"ModelDataUrl": "[^"]*' \
| grep -o '[^"]*\$'
"""
echo "Model URL ${model_url}"
model_url = model_url.trim()
env.model_url = model_url.replaceAll("\\s","")
echo "Model URL ${env.model_url}"
}
}
}
}
stage("Check SageMaker Model") {
steps {
withAWS(credentials: "jenkins-aws-profile") {
script {
def status = sh returnStatus: true, script: """
aws sagemaker describe-model \
--model-name ${env.model_package_name} \
--region ${params.REGION}
"""
if(status != 0) {
// If not exists, create model and endpoint
env.created = "false"
echo "SageMaker Model ${env.model_package_name} not found. Creating it"
} else {
env.created = "true"
echo "SageMaker Model ${env.model_package_name} found"
}
}
}
}
}
stage("Create SageMaker Model") {
steps {
withAWS(credentials: "jenkins-aws-profile") {
script {
if(env.created == "false") {
sh """
aws sagemaker create-model \
--model-name ${env.model_package_name} \
--primary-container ContainerHostname=${env.BUILD_ID},Image=""" + env.image_uri + """,ModelDataUrl=""" + env.model_url + """,Mode='SingleModel' \
--execution-role-arn arn:aws:iam::${params.ACCOUNT_ID}:role/${params.ROLE_NAME} \
--region ${params.REGION}
"""
} else {
echo "SageMaker Model ${env.model_package_name} already created"
}
}
}
}
}
stage("Deploy SageMaker Endpoint config") {
steps {
withAWS(credentials: "jenkins-aws-profile") {
script {
if(env.created == "false") {
sh """
aws sagemaker create-endpoint-config \
--endpoint-config-name ${env.model_package_name} \
--production-variants VariantName='single-model',ModelName=${env.model_package_name},InstanceType='${params.SAGEMAKER_INSTANCE_TYPE}',InitialVariantWeight=1,InitialInstanceCount=${params.SAGEMAKER_INSTANCE_COUNT} \
--region ${params.REGION}
"""
} else {
echo "SageMaker Endpoint Config ${env.model_package_name} already created"
}
}
}
}
}
stage("Check SageMaker Endpoint") {
steps {
withAWS(credentials: "jenkins-aws-profile") {
script {
def status = sh returnStatus: true, script: """
aws sagemaker describe-endpoint \
--endpoint-name ${params.SAGEMAKER_MODEL_PACKAGE_GROUP} \
--region ${params.REGION}
"""
if(status != 0) {
// If not exists, create model and endpoint
env.created_endpoint = "false"
echo "SageMaker Endpoint ${params.SAGEMAKER_MODEL_PACKAGE_GROUP} not found. Creating it"
} else {
env.created_endpoint = "true"
echo "SageMaker Endpoint ${params.SAGEMAKER_MODEL_PACKAGE_GROUP} found"
}
}
}
}
}
stage("Deploy SageMaker Endpoint") {
steps {
withAWS(credentials: "jenkins-aws-profile") {
script {
if(env.created_endpoint == "false") {
echo "Creating SageMaker Endpoint ${params.SAGEMAKER_MODEL_PACKAGE_GROUP}"
sh """
aws sagemaker create-endpoint \
--endpoint-name ${params.SAGEMAKER_MODEL_PACKAGE_GROUP} \
--endpoint-config-name ${env.model_package_name} \
--region ${params.REGION}
"""
} else {
if(env.created == "false") {
echo "Updating SageMaker Endpoint ${params.SAGEMAKER_MODEL_PACKAGE_GROUP}"
sh """
aws sagemaker update-endpoint \
--endpoint-name ${params.SAGEMAKER_MODEL_PACKAGE_GROUP} \
--endpoint-config-name ${env.model_package_name} \
--region ${params.REGION}
"""
} else {
echo "SageMaker Endpoint ${params.SAGEMAKER_MODEL_PACKAGE_GROUP} doesn't changed"
}
}
}
}
}
}
}
}