This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathJenkinsfile
More file actions
516 lines (467 loc) · 15.4 KB
/
Jenkinsfile
File metadata and controls
516 lines (467 loc) · 15.4 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Utility function for getting the real branch name even in a pull request
def branchName() {
if (env.CHANGE_BRANCH) {
return env.CHANGE_BRANCH
}
else {
return env.BRANCH_NAME
}
}
// Utility function for checking out the repo since we want all the branches and tags
def checkoutRepo() {
// Clean the workspace
cleanWs()
// Handles both PRs and branches
script {
BRANCH_NAME_REAL = branchName();
}
// Checkout the code from github
checkout([ $class: 'GitSCM',
branches: [[
name: 'refs/heads/' + BRANCH_NAME_REAL
]],
userRemoteConfigs: [[
credentialsId: 'Github_User_And_Token',
url: 'https://github.com/LORD-MicroStrain/MSCL.git'
]],
extensions: [[
$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
],[
$class: 'LocalBranch',
localBranch: BRANCH_NAME_REAL,
]],
submoduleCfg: []
])
// Set the branch name
env.BRANCH_NAME = branchName()
}
// Run the tests
def runTests(Map config) {
def buildType = config.buildType
def isLinux = config.isLinux
def testsLabel = "Running tests (${buildType})"
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: false) {
if (isLinux) {
def crosscompile32 = ""
// Compile using 32-bit terminal operations
if (env.LINUX_CROSSCOMPILE_32) {
crosscompile32 = "linux32"
}
sh(label: testsLabel, script: """
../.devcontainer/docker-run-container.sh --os ${BUILD_OS} --arch ${BUILD_ARCH} ' \
cd ${BUILD_DIR}; \
${crosscompile32} ctest \
--test-dir "./${buildType}" \
--build-config "${buildType}" \
--output-on-failure \
--parallel \$(nproc);
'
""")
}
else {
powershell(label: testsLabel, script: """
ctest `
--test-dir . `
--build-config "${config.buildType}" `
--output-on-failure `
--parallel \$env:NUMBER_OF_PROCESSORS
""")
}
}
}
// Calling CPack manually allows for packaging both Debug and Release packages
def packageTargets(Map config) {
def packageLabel = 'Packaging'
// Install both release and debug on single config generators
if (config.isLinux) {
// The build directory order doesn't actually matter in this case
// Debug can be Release and vise versa
def debugBuildDir = env.BUILD_TYPES.split(';')[0].trim()
def releaseBuildDir = env.BUILD_TYPES.split(';')[1].trim()
sh(label: packageLabel, script: """
../.devcontainer/docker-run-container.sh --os ${BUILD_OS} --arch ${BUILD_ARCH} ' \
cd ${BUILD_DIR}; \
MICROSTRAIN_BUILD_DIR_DEBUG="${debugBuildDir}" \
MICROSTRAIN_BUILD_DIR_RELEASE="${releaseBuildDir}" \
cpack \
--config "${releaseBuildDir}/microstrain-package-all.cmake" \
-C "${env.BUILD_TYPES}"
'
""")
}
else {
powershell(label: packageLabel, script: """
cpack `
--config "CPackConfig.cmake" `
-C "${env.BUILD_TYPES}"
""")
}
}
// Build each target depending on the configuration
def buildTargets(Map config) {
def buildType = config.buildType.capitalize()
def isLinux = config.isLinux
def releaseBuildType = 'Release'
// Always build the Cpp
def targets = ['MSCL-Cpp']
if (config.libraryType.capitalize() == 'Static') {
// There is no difference in building tests between static and shared so only build it once
targets.addAll(['MSCL-Tests'])
if (buildType == releaseBuildType) {
targets.addAll(['MSCL-Python3'])
}
if (!isLinux) {
targets.addAll(['MSCL-CSharp'])
// Only need to build docs on one architecture
if (buildType == releaseBuildType && env.BUILD_ARCH && env.BUILD_ARCH == "x64") {
targets.addAll(['MSCL-Docs'])
}
}
// There is no difference in building examples between static and shared so only build it once
// Make sure it is at the end of the list so that all libraries are built first
targets.addAll(['MSCL-Examples'])
}
targets.each { target ->
def buildLabel = "Build ${target} (${buildType})"
if (isLinux) {
def crosscompile32 = ""
// Compile using 32-bit terminal operations
if (env.LINUX_CROSSCOMPILE_32) {
crosscompile32 = "linux32"
}
sh(label: buildLabel, script: """
../.devcontainer/docker-run-container.sh --os ${BUILD_OS} --arch ${BUILD_ARCH} ' \
cd ${BUILD_DIR}; \
${crosscompile32} cmake \
--build ./${buildType} \
--parallel \$(nproc) \
--target ${target};
'
""")
}
else {
powershell(label: buildLabel, script: """
cmake `
--build . `
--config ${buildType} `
--parallel \$env:NUMBER_OF_PROCESSORS `
--target ${target}
""")
}
}
}
// Configure the project based on the configuration
def configureProject(Map config) {
def libraryType = config.libraryType.capitalize() // Static or Shared
def buildType = config.buildType.capitalize() // Debug or Release
def isStatic = libraryType == 'Static'
def buildAllPython = env.BRANCH_NAME && env.BRANCH_NAME == 'master' // Build all supported versions of Python
def isWindows = config.isWindows
def isLinux = config.isLinux
def buildDocs = isStatic && isWindows ? 'ON' : 'OFF'
def args = [
'-DMSCL_BUILD_PACKAGE:BOOL=ON',
'-DMSCL_WITH_SSL:BOOL=ON',
'-DMSCL_WITH_WEBSOCKETS:BOOL=ON',
]
if (env.TOOLCHAIN_FILE) {
args.add("-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE:STRING=${env.TOOLCHAIN_FILE}")
}
// Architecture flag (Windows only)
if (isWindows && env.BUILD_ARCH) {
args.add("-A ${env.BUILD_ARCH}")
// Only need to build docs on one architecture
if (buildDocs == 'ON') {
buildDocs = env.BUILD_ARCH == "x64" ? 'ON' : 'OFF'
}
}
// Build type for single-config generators (Linux/Make)
else {
args.add("-DCMAKE_BUILD_TYPE:STRING=${buildType}")
}
// Determine boolean values for each component based on platform and build type
def buildCSharp = isStatic && isWindows ? 'ON' : 'OFF'
def buildCppExamples = isStatic ? 'ON' : 'OFF'
def buildPython3 = isStatic ? 'ON' : 'OFF'
def buildTests = isStatic ? 'ON' : 'OFF'
def buildShared = !isStatic ? 'ON' : 'OFF'
def buildCSharpExamples = buildCSharp
def buildPythonExamples = buildPython3
// Add all of the configuration options
args.addAll([
"-DBUILD_SHARED_LIBS:BOOL=${buildShared}",
"-DMSCL_BUILD_CSHARP:BOOL=${buildCSharp}",
"-DMSCL_BUILD_CSHARP_EXAMPLES:BOOL=${buildCSharpExamples}",
"-DMSCL_BUILD_DOCUMENTATION:BOOL=${buildDocs}",
"-DMSCL_BUILD_CPP_EXAMPLES:BOOL=${buildCppExamples}",
"-DMSCL_BUILD_PYTHON3:BOOL=${buildPython3}",
"-DMSCL_BUILD_PYTHON_EXAMPLES:BOOL=${buildPythonExamples}",
"-DMSCL_BUILD_TESTS:BOOL=${buildTests}"
])
// Build all supported versions of Python3
// The project already defaults to the latest supported version
if (buildPython3 && buildAllPython) {
args.add('-DMSCL_PYTHON3_REQUESTED_VERSION:STRING=""')
}
// Configure the project
def configLabel = "Configuring ${libraryType} library project"
if (isLinux) {
configLabel += " (${buildType})"
}
def cmakeArgs = args.join(' ')
if (isLinux) {
def crosscompile32 = ""
// Compile using 32-bit terminal operations
if (env.LINUX_CROSSCOMPILE_32) {
crosscompile32 = "linux32"
}
sh(label: configLabel, script: """
../.devcontainer/docker-run-container.sh --os ${BUILD_OS} --arch ${BUILD_ARCH} ' \
cd ${BUILD_DIR}; \
${crosscompile32} cmake .. -B ${buildType} ${cmakeArgs}
'
""")
}
else {
powershell(label: configLabel, script: """
cmake .. ${cmakeArgs}
""")
}
}
// Build and package the project
def buildAndPackageProject() {
// Checkout the project
checkoutRepo()
def isLinux = isUnix()
def isWindows = !isLinux
if (isLinux) {
sh(label: "Prepare Docker Image", script: """
./.devcontainer/docker-build-image.sh --os ${BUILD_OS} --arch ${BUILD_ARCH}
""")
}
// Build and package the project in the build directory
dir(env.BUILD_DIR) {
// Build static and shared variants
env.LIBRARY_TYPES.split(',').each { libraryType ->
libraryType = libraryType.trim()
def runConfiguration = true
// Build Release and Debug variants
env.BUILD_TYPES.split(';').each { buildType ->
buildType = buildType.trim()
if (runConfiguration) {
// Configure the project
configureProject(
isLinux: isLinux,
isWindows: isWindows,
libraryType: libraryType,
buildType: buildType
)
// Always run the configuration step on Linux
// Windows only needs it to run once
runConfiguration = isLinux
}
// Build the targets
buildTargets(
isLinux: isLinux,
libraryType: libraryType,
buildType: buildType
)
}
// Package all the available artifacts for both build types (Debug/Release)
packageTargets(
isLinux: isLinux
)
}
// Archive the successful build artifacts
def fileExtension = isLinux ? 'deb' : 'zip'
archiveArtifacts artifacts: "*.${fileExtension}"
// Run all the tests
env.BUILD_TYPES.split(';').each { buildType ->
buildType = buildType.trim()
runTests(
isLinux: isLinux,
buildType: buildType
)
// Archive the test results
junit testResults: "**/*.xml", allowEmptyResults: false
}
}
}
pipeline {
agent none
environment {
BUILD_TYPES = 'Debug;Release'
LIBRARY_TYPES = 'Shared,Static'
}
options {
// Set a timeout for the whole pipeline. The timer starts when the project is queued
timeout(time: 3, unit: 'HOURS')
// Only keep this number of builds for the job
buildDiscarder(logRotator(numToKeepStr: "10"))
copyArtifactPermission('*')
}
stages {
stage('Pre-Release') {
when {
branch pattern: "(develop|master)", comparator: "REGEXP"
}
agent { label 'linux-amd64' }
options {
skipDefaultCheckout()
// TODO: Fix timeout issues on Jenkins for console activity
// timeout(time: 10, activity: true, unit: 'MINUTES')
}
steps {
checkout scm
withCredentials([string(credentialsId: 'Github_Token', variable: 'GH_TOKEN')]) {
sh """
# Pre-release check/update before building on develop
"${WORKSPACE}/scripts/prerelease.sh" --target "${env.BRANCH_NAME}"
"""
}
}
}
stage('Build') {
// Run the windows and linux builds in parallel
parallel {
stage('Windows x64') {
agent {
label 'windows10'
}
environment {
BUILD_ARCH = "x64"
BUILD_DIR = "build_windows_x64"
}
options {
skipDefaultCheckout()
// TODO: Fix timeout issues on Jenkins for console activity
// timeout(time: 20, activity: true, unit: 'MINUTES')
}
steps {
buildAndPackageProject()
}
}
stage('Windows x86') {
agent {
label 'windows10'
}
environment {
BUILD_ARCH = "Win32"
BUILD_DIR = "build_windows_x86"
}
options {
skipDefaultCheckout()
// TODO: Fix timeout issues on Jenkins for console activity
// timeout(time: 20, activity: true, unit: 'MINUTES')
}
steps {
buildAndPackageProject()
}
}
stage('Linux AMD64') {
agent {
label 'linux-amd64'
}
environment {
BUILD_OS = "ubuntu"
BUILD_ARCH = "amd64"
BUILD_DIR = "build_linux_amd64"
}
options {
skipDefaultCheckout()
// TODO: Fix timeout issues on Jenkins for console activity
// timeout(time: 20, activity: true, unit: 'MINUTES')
}
steps {
buildAndPackageProject()
}
}
stage('Linux ARM64') {
agent {
label 'linux-arm64'
}
environment {
BUILD_OS = "ubuntu"
BUILD_ARCH = "arm64v8"
BUILD_DIR = "build_linux_arm64"
}
options {
skipDefaultCheckout()
// TODO: Fix timeout issues on Jenkins for console activity
// timeout(time: 20, activity: true, unit: 'MINUTES')
}
steps {
buildAndPackageProject()
}
}
stage('Linux ARM32') {
agent {
label 'linux-arm64'
}
environment {
BUILD_OS = "ubuntu"
BUILD_ARCH = "arm32v7"
BUILD_DIR = "build_linux_arm32"
TOOLCHAIN_FILE = "arm-linux-crosscompile-toolchain.cmake"
LINUX_CROSSCOMPILE_32 = true
}
options {
skipDefaultCheckout()
// TODO: Fix timeout issues on Jenkins for console activity
// timeout(time: 20, activity: true, unit: 'MINUTES')
}
steps {
buildAndPackageProject()
}
}
}
}
}
post {
success {
script {
if (env.BRANCH_NAME && env.BRANCH_NAME == 'develop') {
node("linux-amd64") {
dir("/tmp/mscl_${env.BRANCH_NAME}_${currentBuild.number}") {
copyArtifacts(projectName: "${env.JOB_NAME}", selector: specific("${currentBuild.number}"));
withCredentials([string(credentialsId: 'Github_Token', variable: 'GH_TOKEN')]) {
sh """
# Release to github
"${WORKSPACE}/scripts/release.sh" \
--artifacts "\$(find \$(pwd) -type f)" \
--target "${env.BRANCH_NAME}" \
--release "latest" \
--docs-zip "\$(find \$(pwd) -type f -name 'MSCL-Documentation-*.zip' | sort | uniq)" \
--generate-notes
"""
}
}
}
}
else if (env.BRANCH_NAME && env.BRANCH_NAME == 'master') {
node("linux-amd64") {
dir("/tmp/mscl_${env.BRANCH_NAME}_${currentBuild.number}") {
copyArtifacts(projectName: "${env.JOB_NAME}", selector: specific("${currentBuild.number}"));
withCredentials([string(credentialsId: 'Github_Token', variable: 'GH_TOKEN')]) {
sh """
# Release to github. The release script will determine if master needs to be published
"${WORKSPACE}/scripts/release.sh" \
--artifacts "\$(find \$(pwd) -type f)" \
--target "${env.BRANCH_NAME}" \
--release "\$(cd ${WORKSPACE} && git describe --match 'v*' --abbrev=0 --tags HEAD)" \
--docs-zip "\$(find \$(pwd) -type f -name 'MSCL-Documentation-*.zip' | sort | uniq)" \
--build-dir "${WORKSPACE}/build_linux_amd64"
"""
}
}
}
}
}
}
}
}