-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjenkinsfile.old.groovy
255 lines (237 loc) · 10.2 KB
/
jenkinsfile.old.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
properties([
parameters([
choice(choices: ['patch','minor','major'], name: 'patchVersion', description: 'What needs to be bump?'),
string(defaultValue:'', description: 'Force set newVersion or leave empty', name: 'newVersion', trim: false),
choice(choices: ['local', 'global'], name: 'releaseType', description: 'It\'s local release or global?'),
])
])
SERVICENAME = 'hydro-serving-ui'
REGISTRYURL = 'hydrosphere'
SERVICEIMAGENAME = 'hydro-serving-ui'
GITHUBREPO = "github.com/Hydrospheredata/hydro-serving-ui.git"
def checkoutRepo(String repo){
if (env.CHANGE_FORK != null){
git changelog: false, credentialsId: 'HydroRobot_AccessToken', poll: false, url: repo, branch: 'master'
sh script: "git fetch origin pull/$CHANGE_ID/head:$BRANCH_NAME"
sh script: "git checkout $BRANCH_NAME"
}else 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
if (env.CHANGE_ID != null ){
version = sh(script: "git rev-parse $CHANGE_BRANCH", returnStdout: true ,label: "get version").trim()
} else {
version = sh(script: "git rev-parse $BRANCH_NAME", returnStdout: true ,label: "get version").trim()
}
}
return version
}catch(err){
return "$err"
}
}
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 buildDocker(){
//run build command and store build tag
tagVersion = getVersion()
sh script: "docker build -t hydrosphere/$SERVICEIMAGENAME:$tagVersion .", label: "Run build docker task";
sh script: "docker tag hydrosphere/$SERVICEIMAGENAME:$tagVersion hydrosphere/$SERVICEIMAGENAME:latest", label: "Retag docker to latest";
}
def pushDocker(String registryUrl, String dockerImage){
//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 tag hydrosphere/$dockerImage $registryUrl/$dockerImage",label: "set tag to docker image"
sh script: "docker push $registryUrl/$dockerImage",label: "push docker image to registry"
}
}
def updateDockerCompose(String newVersion){
dir('docker-compose'){
//Change template
sh script: "sed -i \"s/.*image:.*/ image: hydrosphere\\/hydro-serving-ui:$newVersion/g\" hydro-serving-ui.service.template", label: "sed hydro-serving-ui 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\\/hydro-serving-ui:$newVersion/g\" ui/values.yaml", label: "sed hydro-serving-ui version"
sh script: "sed -i \"s/.*serving-ui:.*/ full: hydrosphere\\/hydro-serving-ui:$newVersion/g\" dev.yaml", label: "sed hydro-serving-ui dev stage version"
//Refresh readme for chart
sh script: "frigate gen ui --no-credits > ui/README.md"
//Lint
dir('ui'){
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"
}
}
}
//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}'"
}
}
//Send message in slack
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}'"
}
}
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 \"[email protected]\"", label: "Set user email"
checkoutRepo("https://github.com/Hydrospheredata/$SERVICENAME" + '.git')
if (env.CHANGE_FORK != null){
AUTHOR = env.CHANGE_FORK
} else {
AUTHOR = sh(script:"git log -1 --pretty=format:'%an'", returnStdout: true, label: "get last commit author").trim()
}
}
stage('Test'){
if (env.CHANGE_ID != null){
buildDocker()
}
}
stage('Release'){
if (BRANCH_NAME == 'master' || BRANCH_NAME == 'main'){
if (params.releaseType == 'global'){
oldVersion = getVersion()
bumpVersion(getVersion(),params.newVersion,params.patchVersion,'version')
newVersion = getVersion()
} else {
newVersion = getVersion()
}
buildDocker()
pushDocker(REGISTRYURL, SERVICEIMAGENAME+":$newVersion")
pushDocker(REGISTRYURL, SERVICEIMAGENAME+":latest")
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:[email protected]/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:[email protected]/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
}
}