-
Notifications
You must be signed in to change notification settings - Fork 0
/
.jenkins_function.groovy
250 lines (224 loc) · 7.62 KB
/
.jenkins_function.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
// Useful Jenkins functions
// call a job to build an app
def build_app(sba_version, jobToBuild, branch) {
stage("Build app ${jobToBuild}") {
// is the job runnable ?
def enabled = Jenkins.instance.getItemByFullName(jobToBuild).isBuildable();
def jobnamebranch = "${jobToBuild}/${branch}"
echo "build app ${jobToBuild} in branch ${branch} with sba-internal version ${sba_version} ${jobnamebranch}"
if (enabled) {
// start the job (without propagate error)
def resbuild = build job: jobnamebranch, wait: true, propagate: false, parameters: [string(name: 'SBA_VERSION', value: sba_version)]
def jobResult = resbuild.getResult()
echo "Build of ${jobnamebranch} returned result: ${jobResult}"
if (jobResult != 'SUCCESS') {
sendMessage("#CC0000", "Build failed when building ${jobToBuild} in branch ${branch} with sba-internal version ${sba_version}")
// set the current build as unstable (warning)
currentBuild.result = "UNSTABLE"
}
}
}
}
// function to get the package version in package.json file
def get_pkg_version() {
def pkg_version = powershell(returnStdout: true, script: '''
$file = 'package.json'
$search = '.+"version":\\s"(.+)"'
$retval = (Select-String -path $file -pattern $search -Allmatches | % { $_.Matches.Groups[1].Value })
write-output $retval
''')
// remove CR/LF
pkg_version = pkg_version.trim()
pkg_version = "${pkg_version}${pkg_suffix}.${env.BUILD_NUMBER}"
echo "pkg_version: ${pkg_version}"
return pkg_version
}
// function to build the package tag from the version
def get_pkg_tag(sba_version) {
def pkg_tag = sba_version.split(pkg_suffix)[0]
pkg_tag = pkg_tag.trim()
pkg_tag = "${tag_prefix}${pkg_tag}"
echo "pkg_tag: ${pkg_tag}"
return pkg_tag
}
// function to check if we are in PR or another branch
def buildOrMerge() {
def typeAction = ""
if (env.BRANCH_NAME.contains("PR-")) {
typeAction = "build"
} else {
typeAction = "merge"
}
return typeAction
}
// get the branch name and the version number from the right jenkins variable
def getBranch() {
def tmpBranch=""
// PR :
// BRANCH_NAME: PR-8208
// CHANGE_TARGET: release/11.7.0
// BRANCH
// BRANCH_NAME: develop
// BRANCH_NAME: release/11.7.0
// return: release/11.7.0
if (env.BRANCH_NAME.contains("PR-")) {
tmpBranch = env.CHANGE_TARGET
} else {
tmpBranch = env.BRANCH_NAME
}
echo "tmpBranch: ${tmpBranch}"
echo "Branch returned: ${tmpBranch}"
return tmpBranch
}
// get the branch name and the version number from the right jenkins variable
def findBranchNumber() {
def tmpBranch=""
def theBranch=""
// PR :
// BRANCH_NAME: PR-8208
// CHANGE_TARGET: release/11.7.0
// BRANCH
// BRANCH_NAME: develop
// BRANCH_NAME: release/11.7.0
// return: release%2F11.7.0
echo "Triggering job for branch ${env.BRANCH_NAME}"
tmpBranch = getBranch()
theBranch = tmpBranch.replace("/", "%2F")
echo "Branch returned: ${theBranch}"
return theBranch
}
// function to append lines to the end of a file
def appendFile(afile, what) {
def content = ""
def txt = ""
try {
if (fileExists(afile)) {
content = readFile afile
what.each {
txt += it + "\n"
}
content += txt
//echo "content: ${content}"
writeFile file: afile, text: content
}
} catch (err) {
currentBuild.result = "FAILURE"
throw err
}
}
// function to update sinequa package version in package.json file
def updatePackage(version) {
withEnv(["pkgVersion=${version}"]) {
echo "pkgVersion = ${env.pkgVersion}"
powershell ('''
$file = 'package.json'
write-host "Update: $file packages: @sinequa with pkgVersion: $env:pkgVersion"
$regex1 = '\\"\\@sinequa/core\\".*'
$regex2 = '\\"\\@sinequa/components\\".*'
$regex3 = '\\"\\@sinequa/analytics\\".*'
$s1 = '"@sinequa/core": "' + $env:pkgVersion + '",'
$s2 = '"@sinequa/components": "' + $env:pkgVersion + '",'
$s3 = '"@sinequa/analytics": "' + $env:pkgVersion + '",'
(Get-Content $file) -replace $regex1, $s1 -replace $regex2, $s2 -replace $regex3, $s3 | Set-Content $file
''')
}
}
// function to send an email to the authors of the commit
def sendMessage(color, specificMessage, logfile="") {
echo "Message is: ${specificMessage}"
if (!binding.hasVariable("AUTHOR_NAME")) {
AUTHOR_NAME = ""
}
// https://jenkins.sinequa.com/env-vars.html/
to = ""
pbranch = env.BRANCH_NAME
branch_link = pbranch
if (pbranch.startsWith("PR-")) {
branch_link += " https://github.sinequa.com/Product/ice/pull/" + pbranch.replace("PR-", "")
}
if ("${color}" == "#26cc00") {
status = "OK"
} else {
status = "Failed"
to = mailto
}
if ("${AUTHOR_NAME}" != "") {
to = "${AUTHOR_NAME}" + ", " + to
}
// println("mailTo: ${to}")
subject = "[${pbranch}] ${BUILD_TAG} ${status}"
echo "Send email ${subject} to ${to}"
header = "Commit on branch ${branch_link} from ${AUTHOR_NAME}\nJob ${BUILD_URL}/\n"
message = "${header}\n${specificMessage}"
if ("${status}" == "Failed" && logfile?.trim()) {
log = bat (script: "type ${WORKSPACE}\\${logfile}",returnStdout: true)
emailext(from: "build@sinequa.com", to: "${to}", attachLog:true, subject: "${subject}", body: "${message}\n\n${log}")
} else {
emailext(from: "build@sinequa.com", to: "${to}", attachLog:false, subject: "${subject}", body: "${message}\n\n")
}
}
// get the path of npm in the version of the branch
def getIceNode(branchName) {
println "Get nodejs from ICE for the release ${branchName}"
def cmd = "&git clone --no-checkout --depth 1 --filter=blob:none --branch ${branchName} --single-branch ${env:GIT_ROOT_URL}/Product/ice ice" + "\n"
cmd += "cd ice" + "\n"
cmd += "&git sparse-checkout init --cone" + "\n"
cmd += "&git sparse-checkout set distrib/programs/win/node/" + "\n"
cmd += "&git checkout ${branchName}" + "\n"
cmd += "cd .." + "\n"
// println cmd
try {
def ret = powershell(returnStdout: true, script: cmd)
} catch (err) {
currentBuild.result = "FAILURE"
throw err
}
}
// get the path of npm in the version of the branch
def getNPMpath(pgm) {
println "Get $pgm path"
def pgmPath = ""
// def cmd = '\$npmPath="ice\\distrib\\programs\\win\\node\\14.16.0\\' + pgm + '"' + "\n"
def cmd = '\$npmPath = Get-Childitem -path ./ice -file ' + pgm + ' -recurse -ErrorAction SilentlyContinue | select fullname | Out-String -stream | select-object -skip 3 | select-object -first 1'+ "\n"
cmd += "if ( \$null -ne \$npmPath ) {" + "\n"
cmd += " if ( Test-Path \$npmPath -PathType leaf ) {" + "\n"
cmd += " Write-Output \$npmPath" + "\n"
cmd += " } else {" + "\n"
cmd += ' Write-Output ""' + "\n"
cmd += " }" + "\n"
cmd += "} else {" + "\n"
cmd += 'Write-Output ""' + "\n"
cmd += "}" + "\n"
// println cmd
pgmPath = powershell(returnStdout: true, script: cmd)
// remove CR/LF
pgmPath = pgmPath.trim()
println "Path found: "+ pgmPath
return pgmPath
}
// get the full directory name of a file
def GetDirectoryName(pgm) {
println "Get $pgm path"
def dirPath = ""
// def cmd = '\$dPath="ice\\distrib\\programs\\win\\node\\14.16.0\\' + pgm + '"' + "\n"
def cmd = '\$dPath = Split-Path ' + pgm + ' -Resolve'+ "\n"
cmd += "if ( \$null -ne \$dPath ) {" + "\n"
cmd += " Write-Output \$dPath" + "\n"
cmd += "} else {" + "\n"
cmd += 'Write-Output ""' + "\n"
cmd += "}" + "\n"
// println cmd
dirPath = powershell(returnStdout: true, script: cmd)
// remove CR/LF
dirPath = dirPath.trim()
println "Path found: "+ dirPath
return dirPath
}
// run bat with a special PATH
def batEnv(path, cmd) {
bat """
@set PATH=${path};%PATH%
${cmd}
"""
}
return this