-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjenkinsfile.disable.groovy
271 lines (250 loc) · 11.4 KB
/
jenkinsfile.disable.groovy
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
properties([
parameters([
choice(choices: ['patch','minor','major'], name: 'patchVersion', description: 'What needs to be bump?'),
string(defaultValue:'', description: 'Force set newVersion x.x.x.x or leave empty', name: 'newVersion', trim: false),
string(defaultValue:'', description: 'Set grpcVersion or leave empty', name: 'grpcVersion', trim: false),
string(defaultValue:'', description: 'Set sdkVersion or leave empty', name: 'sdkVersion', trim: false),
choice(choices: ['local', 'global'], name: 'releaseType', description: 'It\'s local release or global?'),
])
])
SERVICENAME = 'hydro-auto-od'
SEARCHPATH = './requirements.txt'
SEARCHSDK = 'hydrosdk'
SEARCHGRPC = 'hydro-serving-grpc'
TESTCMD = 'sbt --batch test'
REGISTRYURL = 'hydrosphere'
SERVICEIMAGENAME = 'auto-od'
def checkoutRepo(String repo){
if (env.CHANGE_ID != null ){
git changelog: false, credentialsId: 'HydroRobot_AccessToken', poll: false, url: repo, branch: env.CHANGE_BRANCH
} else {
git changelog: false, credentialsId: 'HydroRobot_AccessToken', poll: false, url: repo, branch: env.BRANCH_NAME
}
}
def getVersion(){
try{
if (params.releaseType == 'global'){
//remove only quotes
version = sh(script: "cat \"version\" | sed 's/\\\"/\\\\\"/g'", returnStdout: true ,label: "get version").trim()
} else {
//Set version as commit SHA
version = sh(script: "git rev-parse HEAD", returnStdout: true ,label: "get version").trim()
}
return version
}catch(err){
return "$err"
}
}
def slackMessage(){
withCredentials([string(credentialsId: 'slack_message_url', variable: 'slack_url')]) {
//beautiful block
def json = """
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "$SERVICENAME: release - ${currentBuild.currentResult}!",
"emoji": true
}
},
{
"type": "section",
"block_id": "section567",
"text": {
"type": "mrkdwn",
"text": "Build info:\n Project: $JOB_NAME\n Author: $AUTHOR\n SHA: $newVersion"
},
"accessory": {
"type": "image",
"image_url": "https://res-5.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_170,w_170,f_auto,b_white,q_auto:eco/oxpejnx8k2ixo0bhfsbo",
"alt_text": "Hydrospere loves you!"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "You can see the assembly details by clicking on the button"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Details",
"emoji": true
},
"value": "Details",
"url": "${env.BUILD_URL}",
"action_id": "button-action"
}
}
]
}
"""
//Send message
sh label:"send slack message",script:"curl -X POST \"$slack_url\" -H \"Content-type: application/json\" --data '${json}'"
}
}
def bumpVersion(String currentVersion,String newVersion, String patch, String path){
sh script: """cat <<EOF> ${WORKSPACE}/bumpversion.cfg
[bumpversion]
current_version = 0.0.0
commit = False
tag = False
parse = (?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)
serialize =
{major}.{minor}.{patch}
EOF""", label: "Set bumpversion configfile"
if (newVersion != null && newVersion != ''){ //TODO: needs verify valid semver
sh("echo $newVersion > version")
}else{
sh("bumpversion $patch $path --config-file '${WORKSPACE}/bumpversion.cfg' --allow-dirty --verbose --current-version '$currentVersion'")
}
}
def bumpGrpc(String newVersion, String search, String patch, String path){
sh script: "cat $path | grep '$search' > tmp", label: "Store search value in tmp file"
currentVersion = sh(script: "cat tmp | cut -d'%' -f4 | sed 's/\"//g' | sed 's/,//g' | sed 's/^.*=//g'", returnStdout: true, label: "Get current version").trim()
sh script: "sed -i -E \"s/$currentVersion/$newVersion/\" tmp", label: "Bump temp version"
sh script: "sed -i 's/\\\"/\\\\\"/g' tmp", label: "remove quote and space from version"
sh script: "sed -i \"s/.*$search.*/\$(cat tmp)/g\" $path", label: "Change version"
sh script: "rm -rf tmp", label: "Remove temp file"
}
//Run test
def runTest(){
sh script: "$TESTCMD", label: "Run test task"
}
//Create github release
def releaseService(String xVersion, String yVersion){
withCredentials([usernamePassword(credentialsId: 'HydroRobot_AccessToken', passwordVariable: 'password', usernameVariable: 'username')]) {
//Set global git
sh script: "git diff", label: "show diff"
sh script: "git commit -a -m 'Bump to $yVersion'", label: "commit to git"
sh script: "git push --set-upstream origin master", label: "push all file to git"
sh script: "git tag -a $yVersion -m 'Bump $xVersion to $yVersion version'",label: "set git tag"
sh script: "git push --set-upstream origin master --tags",label: "push tag and create release"
//Create release from tag
sh script: "curl -X POST -H \"Accept: application/vnd.github.v3+json\" -H \"Authorization: token ${password}\" https://api.github.com/repos/Hydrospheredata/${SERVICENAME}/releases -d '{\"tag_name\":\"${yVersion}\",\"name\": \"${yVersion}\",\"body\": \"Bump to ${yVersion}\",\"draft\": false,\"prerelease\": false}'"
}
}
def buildDocker(String registryUrl){
//run build command and store build tag
tagVersion = getVersion()
sh script: "docker build -t $registryUrl/$SERVICEIMAGENAME:$tagVersion .", label: "Run build docker task";
sh script: "docker tag $registryUrl/$SERVICEIMAGENAME:$tagVersion $registryUrl/$SERVICEIMAGENAME:latest", label: "Run build docker task";
}
def pushDocker(String registryUrl){
//push docker image to registryUrl
withCredentials([usernamePassword(credentialsId: 'hydrorobot_docker_creds', passwordVariable: 'password', usernameVariable: 'username')]) {
sh script: "docker login --username $username --password $password"
sh script: "docker push $registryUrl/$SERVICEIMAGENAME:$tagVersion",label: "push docker image to registry"
sh script: "docker push $registryUrl/$SERVICEIMAGENAME:latest",label: "push docker image to registry"
}
}
def updateDockerCompose(String newVersion){
dir('docker-compose'){
//Change template
sh script: "sed -i \"s/.*image:.*/ image: hydrosphere\\/$SERVICEIMAGENAME:$newVersion/g\" ${SERVICENAME}.service.template", label: "sed $SERVICENAME version"
//Merge compose into 1 file
composeMerge = "docker-compose"
composeService = sh label: "Get all template", returnStdout: true, script: "ls *.template"
list = composeService.split( "\\r?\\n" )
for(l in list){
composeMerge = composeMerge + " -f $l"
}
composeMerge = composeMerge + " config > ../docker-compose.yaml"
sh script: "$composeMerge", label:"Merge compose file"
//sh script: "cp docker-compose.yaml ../docker-compose.yaml"
}
}
def updateHelmChart(String newVersion){
dir('helm'){
//Change template
sh script: "sed -i \"s/.*full:.*/ full: hydrosphere\\/${SERVICEIMAGENAME}:$newVersion/g\" ${SERVICEIMAGENAME}/values.yaml", label: "sed ${SERVICEIMAGENAME} version"
sh script: "sed -i \"s/.*${SERVICEIMAGENAME}.*/ full: hydrosphere\\/${SERVICEIMAGENAME}:${newVersion}/g\" dev.yaml", label: "sed ${SERVICEIMAGENAME} dev stage version"
//Refresh readme for chart
sh script: "frigate gen ${SERVICEIMAGENAME} --no-credits > ${SERVICEIMAGENAME}/README.md"
dir('auto-od'){
sh script: "helm dep up", label: "Dependency update"
sh script: "helm lint .", label: "Lint auto-od chart"
sh script: "helm template -n serving --namespace hydrosphere . > test.yaml", label: "save template to file"
sh script: "polaris audit --audit-path test.yaml -f yaml", label: "lint template by polaris"
sh script: "polaris audit --audit-path test.yaml -f score", label: "get polaris score"
sh script: "rm -rf test.yaml", label: "remove test.yaml"
}
dir('serving'){
sh script: "helm dep up", label: "Dependency update"
sh script: "helm lint .", label: "Lint all charts"
sh script: "helm template -n serving --namespace hydrosphere . > test.yaml", label: "save template to file"
sh script: "polaris audit --audit-path test.yaml -f yaml", label: "lint template by polaris"
sh script: "polaris audit --audit-path test.yaml -f score", label: "get polaris score"
sh script: "rm -rf test.yaml", label: "remove test.yaml"
}
}
}
node('hydrocentral') {
try {
stage('SCM'){
//Set commit author
sh script: "git config --global user.name \"HydroRobot\"", label: "Set username"
sh script: "git config --global user.email \"robot@hydrosphere.io\"", label: "Set user email"
checkoutRepo("https://github.com/Hydrospheredata/$SERVICENAME" + '.git')
AUTHOR = sh(script:"git log -1 --pretty=format:'%an'", returnStdout: true, label: "get last commit author").trim()
if (params.grpcVersion == ''){
//Set grpcVersion
grpcVersion = sh(script: "curl -Ls https://pypi.org/pypi/hydro-serving-grpc/json | jq -r .info.version", returnStdout: true, label: "get grpc version").trim()
}
if (params.sdkVersion == ''){
//Set sdkVersion
sdkVersion = sh(script: "curl -Ls https://pypi.org/pypi/hydrosdk/json | jq -r .info.version", returnStdout: true, label: "get sdk version").trim()
}
}
stage('Test'){
if (env.CHANGE_ID != null){
buildDocker(REGISTRYURL)
}
}
stage('Release'){
if (BRANCH_NAME == 'master' || BRANCH_NAME == 'main'){ //Run only manual from master{
if (params.releaseType == 'global'){
oldVersion = getVersion()
bumpVersion(getVersion(),params.newVersion,params.patchVersion,'version')
newVersion = getVersion()
bumpGrpc(sdkVersion,SEARCHSDK, params.patchVersion,SEARCHPATH)
bumpGrpc(grpcVersion,SEARCHGRPC, params.patchVersion,SEARCHPATH)
} else {
newVersion = getVersion()
}
buildDocker(REGISTRYURL)
pushDocker(REGISTRYURL)
//Update helm and docker-compose if release
if (params.releaseType == 'global'){
releaseService(oldVersion, newVersion)
} else {
dir('release'){
//bump only image tag
withCredentials([usernamePassword(credentialsId: 'HydroRobot_AccessToken', passwordVariable: 'Githubpassword', usernameVariable: 'Githubusername')]) {
git changelog: false, credentialsId: 'HydroRobot_AccessToken', url: "https://$Githubusername:$Githubpassword@github.com/Hydrospheredata/hydro-serving.git"
updateHelmChart("$newVersion")
updateDockerCompose("$newVersion")
sh script: "git commit --allow-empty -a -m 'Releasing $SERVICENAME:$newVersion'",label: "commit to git chart repo"
sh script: "git push https://$Githubusername:$Githubpassword@github.com/Hydrospheredata/hydro-serving.git --set-upstream master",label: "push to git"
}
}
}
}
}
//post if success
if (params.releaseType == 'local' && BRANCH_NAME == 'master'){
slackMessage()
}
} catch (e) {
//post if failure
currentBuild.result = 'FAILURE'
if (params.releaseType == 'local' && BRANCH_NAME == 'master'){
slackMessage()
}
throw e
}
}