-
Notifications
You must be signed in to change notification settings - Fork 3
/
jenkinsfile.old.groovy
269 lines (248 loc) · 9.49 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
properties([
parameters([
choice(choices: ['patch','minor','major','tag','addon'], name: 'patchVersion', description: 'What needs to be bump?'),
string(defaultValue:'', description: 'Force set newVersion or leave empty', name: 'newVersion', trim: false),
choice(choices: ['false', 'true'], name: 'release', description: 'Release python package?'),
choice(choices: ['local', 'global'], name: 'releaseType', description: 'It\'s local release or global?'),
])
])
SERVICENAME = 'hydro-serving-protos'
SEARCHPATH = './requirements.txt'
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{
//remove only quotes
version = sh(script: "cat \"version\" | sed 's/\\\"/\\\\\"/g'", returnStdout: true ,label: "get version").trim()
return version
}catch(e){
return "file version not found"
}
}
def bumpVersion(String currentVersion,String newVersion, String patch, String path){
if (currentVersion =~ /\w*rc/ || newVersion =~ /\w*rc/){
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+)-?(?P<tag>\\w*rc)?(?P<addon>\\d+)?
serialize =
{major}.{minor}.{patch}-{tag}{addon}
{major}.{minor}.{patch}
[bumpversion:part:addon]
[bumpversion:part:tag]
optional_value = release
values =
rc
release
EOF""", label: "Set bumpversion configfile"
}else if (currentVersion =~ /\w*post/ || newVersion =~ /\w*post/){
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+).?(?P<tag>\\w*post)?(?P<addon>\\d+)?
serialize =
{major}.{minor}.{patch}.{tag}{addon}
{major}.{minor}.{patch}
[bumpversion:part:addon]
[bumpversion:part:tag]
optional_value = release
values =
post
release
EOF""", label: "Set bumpversion configfile"
}else if (currentVersion =~ /\w*dev/ || newVersion =~ /\w*dev/){
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+).?(?P<tag>\\w*dev)?(?P<addon>\\d+)?
serialize =
{major}.{minor}.{patch}.{tag}{addon}
{major}.{minor}.{patch}
[bumpversion:part:addon]
[bumpversion:part:tag]
optional_value = release
values =
dev
release
EOF""", label: "Set bumpversion configfile"
}else{
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 != ''){
sh("echo $newVersion > version")
}else{
sh("bumpversion $patch $path --config-file '${WORKSPACE}/bumpversion.cfg' --allow-dirty --verbose --current-version '$currentVersion'")
}
}
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 buildPython(String command, String version){
configFileProvider([configFile(fileId: 'PYPIDeployConfiguration', targetLocation: "${env.WORKSPACE}/python-package/.pypirc", variable: 'PYPI_SETTINGS')]) {
if(command == "build"){
sh script:"""#!/bin/bash
python3 -m venv venv
source venv/bin/activate
pip install wheel~=0.34.2
pip install twine
pip install -r \"$WORKSPACE/python-package/requirements.txt\" &&
make PYTHON=python3 all && make PYTHON=python3 test
""", label: "Build python package"
}else if(command == "release"){
try{
sh script: """#!/bin/bash
python3 -m venv venv
source venv/bin/activate
pip install wheel~=0.34.2
pip install twine
pip install -r \"$WORKSPACE/python-package/requirements.txt\" &&
python -m twine upload --config-file \"${env.WORKSPACE}/python-package/.pypirc\" -r pypi \"${env.WORKSPACE}/python-package/dist/*\"""",label: "Release python package"
}catch(err){
echo "$err"
}
withCredentials([file(credentialsId: 'SonatypeSigningKey', variable: 'SONATYPE_KEY_PATH')]) {
sh script: "gpg --import ${SONATYPE_KEY_PATH}", label: "Sign package"
sh script: "mkdir -p ~/.sbt/gpg/ && chmod -R 777 ~/.sbt/gpg/"
sh script: "cp ${SONATYPE_KEY_PATH} ~/.sbt/gpg/secring.asc"
dir("scala-package"){
try {
//sh script: "sbt -DappVersion=$version 'set pgpPassphrase := Some(Array())' +publishLocal", label: "publish local"
sh script: "sbt -DappVersion=$version 'set pgpPassphrase := Some(Array())' +publishSigned", label: "publish signed"
sh script: "sbt -DappVersion=$version 'sonatypeReleaseAll'", label: "Release all"
}catch(err){
echo "$err"
}
}
}
}else{
echo "command $command not found! Use build or release"
}
}
}
//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 --allow-empty -a -m 'Bump to $yVersion'", label: "commit to git"
sh script: "git push https://$username:[email protected]/Hydrospheredata/${SERVICENAME}.git --set-upstream 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 https://$username:[email protected]/Hydrospheredata/${SERVICENAME}.git --set-upstream 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}'"
}
}
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")
AUTHOR = sh(script:"git log -1 --pretty=format:'%an'", returnStdout: true, label: "get last commit author").trim()
}
stage('Test'){
echo "Change id: ${env.CHANGE_ID}"
if (env.CHANGE_ID != null){
currentVersion = getVersion()
buildPython("build", currentVersion)
}
}
stage('Release'){
echo "Change target: ${env.CHANGE_TARGET}"
if (BRANCH_NAME == 'master' && params.release == 'true' || BRANCH_NAME == 'main' && params.release == 'true' ){ //Run only manual from master
if (params.releaseType == 'global'){
oldVersion = sh(script: "curl -Ls https://pypi.org/pypi/hydro-serving-grpc/json | jq -r .info.version", returnStdout: true, label: "get grpc version").trim()
sh script: "echo oldVersion > version", label: "change version"
} else {
oldVersion = getVersion()
}
bumpVersion(getVersion(),params.newVersion,params.patchVersion,'version')
newVersion = getVersion()
buildPython("build", newVersion)
releaseService(oldVersion, newVersion)
buildPython("release", newVersion)
}
}
//post if success
if (params.releaseType == 'local' && params.release == 'true'){
slackMessage()
}
} catch (e) {
//post if failure
currentBuild.result = 'FAILURE'
if (params.releaseType == 'local' && params.release == 'true'){
slackMessage()
}
throw e
}
}