Linux 上实现 Jenkins 跨平台兼容的落地方案
一 架构选型与总体思路
二 环境与节点准备
三 流水线设计与跨平台实现
pipeline {
agent none
parameters {
string(name: 'BRANCH', defaultValue: 'main', description: 'Git branch')
choice(name: 'BUILD_PLATFORM', choices: ['linux', 'windows', 'macos'], description: 'Target platform')
}
tools {
maven 'Maven-3.8.6' // 在 Global Tool Configuration 中预配置
jdk 'JDK-11'
}
stages {
stage('Checkout') {
agent any
steps { git url: 'https://github.com/your/repo.git', branch: params.BRANCH }
}
stage('Build Linux') {
when { expression { params.BUILD_PLATFORM == 'linux' } }
agent { label 'linux' }
steps {
sh '''
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j"$(nproc)"
'''
}
}
stage('Build Windows') {
when { expression { params.BUILD_PLATFORM == 'windows' } }
agent { label 'windows' }
steps {
bat '''
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -A x64 ..
msbuild ALL_BUILD.vcxproj /p:Configuration=Release
'''
}
}
stage('Build macOS') {
when { expression { params.BUILD_PLATFORM == 'macos' } }
agent { label 'macos' }
steps {
sh '''
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j"$(sysctl -n hw.logicalcpu)"
'''
}
}
}
post {
always {
junit '**/test-reports/*.xml'
archiveArtifacts artifacts: '**/build/**/*.{zip,rar,tar.gz,exe,dll,app}', allowEmptyArchive: true
}
}
}
四 触发与产物分发
五 常见问题与最佳实践