You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
slms/docs/Jenkinsfile.full

295 lines
12 KiB

// ========================================
// SLMS 完整版 Jenkinsfile (四端打包 + 双分支推送)
// 备份文件 - 待 pom.xml 配置完成后使用
// ========================================
pipeline {
agent any
environment {
JAVA_HOME = 'E:\\2025-2026\\GitAIOps\\jdk'
ANDROID_HOME = 'D:\\development\\Android'
SONAR_HOST_URL = 'http://localhost:9000'
SONAR_PROJECT_KEY = 'slms:smart-library-management-system'
}
tools {
maven 'maven396'
jdk 'jdk21'
}
stages {
stage('1. 拉取代码') {
steps {
echo '========== 从 Gitea 拉取代码 =========='
checkout scm
echo '✓ 代码拉取成功'
}
}
stage('2. 编译项目') {
steps {
echo '========== 编译 SLMS 项目 =========='
bat '''
set JAVA_HOME=%JAVA_HOME%
mvn clean compile -DskipTests
'''
echo '✓ 项目编译成功'
}
}
stage('3. 运行测试') {
steps {
echo '========== 运行单元测试 =========='
bat '''
set JAVA_HOME=%JAVA_HOME%
mvn test
'''
echo '✓ 测试执行完成'
}
post {
always {
junit '**/target/surefire-reports/*.xml'
}
}
}
stage('4. SonarQube 质检') {
steps {
echo '========== 执行 SonarQube 代码质量检测 =========='
withSonarQubeEnv('SonarQube') {
bat '''
set JAVA_HOME=%JAVA_HOME%
mvn sonar:sonar
'''
}
echo '✓ SonarQube 分析完成'
}
}
stage('5. 质量阈检查') {
steps {
echo '========== 等待 SonarQube 质量阈结果 =========='
timeout(time: 5, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
echo '✓ 质量阈检查通过'
}
}
stage('6. 四端打包') {
when {
expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' }
}
parallel {
stage('6.1 CLI 打包 (JAR)') {
steps {
echo '========== 打包 CLI 应用 (JAR) =========='
bat '''
set JAVA_HOME=%JAVA_HOME%
mvn clean package -DskipTests -P cli
'''
echo '✓ CLI JAR 打包成功'
}
}
stage('6.2 GUI 打包 (EXE)') {
steps {
echo '========== 打包 GUI 应用 (EXE) =========='
bat '''
set JAVA_HOME=%JAVA_HOME%
mvn clean package -DskipTests -P gui
REM 使用 jpackage 打包为 Windows EXE
jpackage --input target --name SLMS-GUI --main-jar slms-gui.jar --type exe --win-console
'''
echo '✓ GUI EXE 打包成功'
}
}
stage('6.3 Web 打包 (WAR)') {
steps {
echo '========== 打包 Web 应用 (WAR) =========='
bat '''
set JAVA_HOME=%JAVA_HOME%
mvn clean package -DskipTests -P web
'''
echo '✓ Web WAR 打包成功'
}
}
stage('6.4 Android 打包 (APK)') {
steps {
echo '========== 打包 Android 应用 (APK) =========='
bat '''
set JAVA_HOME=%JAVA_HOME%
set ANDROID_HOME=%ANDROID_HOME%
cd android
gradlew assembleDebug
'''
echo '✓ Android APK 打包成功'
}
}
}
}
stage('7. 归档制品') {
when {
expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' }
}
steps {
echo '========== 归档构建制品 =========='
script {
archiveArtifacts artifacts: '''
**/target/*.jar,
**/target/*.war,
**/target/*.exe,
**/target/*.msi,
**/android/build/outputs/apk/**/*.apk
''',
fingerprint: true,
allowEmptyArchive: true
}
echo '✓ 制品归档成功'
}
}
stage('8. 推送代码到 feature-ldl') {
when {
expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' }
}
steps {
echo '========== 推送源代码到头歌 feature-ldl 分支 =========='
script {
try {
withCredentials([usernamePassword(
credentialsId: 'tougo-credentials',
usernameVariable: 'TOUGO_USER',
passwordVariable: 'TOUGO_PASS'
)]) {
bat """
git config user.name "Jenkins CI"
git config user.email "jenkins@slms.local"
git remote add tougo https://bdgit.educoder.net/pu6zrsfoy/CHZU_CS231_SEB_lab.git || git remote set-url tougo https://bdgit.educoder.net/pu6zrsfoy/CHZU_CS231_SEB_lab.git
git push https://%TOUGO_USER%:%TOUGO_PASS%@bdgit.educoder.net/pu6zrsfoy/CHZU_CS231_SEB_lab.git HEAD:feature-ldl --force
"""
}
echo '✓ 源代码推送到 feature-ldl 成功'
} catch (Exception e) {
echo '⚠ 推送代码到 feature-ldl 失败'
echo "错误信息: ${e.message}"
currentBuild.result = 'UNSTABLE'
}
}
}
}
stage('9. 推送制品到 release') {
when {
expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' }
}
steps {
echo '========== 推送构建制品到头歌 release 分支 =========='
script {
try {
withCredentials([usernamePassword(
credentialsId: 'tougo-credentials',
usernameVariable: 'TOUGO_USER',
passwordVariable: 'TOUGO_PASS'
)]) {
bat """
REM 创建 artifacts 目录
if not exist artifacts mkdir artifacts
REM 复制所有制品到 artifacts 目录
xcopy /Y /S target\\*.jar artifacts\\ 2>nul
xcopy /Y /S target\\*.war artifacts\\ 2>nul
xcopy /Y /S target\\*.exe artifacts\\ 2>nul
xcopy /Y /S target\\*.msi artifacts\\ 2>nul
xcopy /Y /S android\\build\\outputs\\apk\\debug\\*.apk artifacts\\ 2>nul
REM 添加制品到 Git
git add artifacts/
git commit -m "chore: 发布构建制品 Build #%BUILD_NUMBER%" || echo "No changes to commit"
REM 推送到 release 分支
git push https://%TOUGO_USER%:%TOUGO_PASS%@bdgit.educoder.net/pu6zrsfoy/CHZU_CS231_SEB_lab.git HEAD:release --force
"""
}
echo '✓ 制品推送到 release 分支成功'
} catch (Exception e) {
echo '⚠ 推送制品到 release 失败'
echo "错误信息: ${e.message}"
currentBuild.result = 'UNSTABLE'
}
}
}
}
}
post {
success {
echo '========== 流水线执行成功 =========='
emailext (
subject: "✅ SLMS 构建成功 - Build #${BUILD_NUMBER}",
body: """
<h2>SLMS 项目构建成功</h2>
<p><strong>构建编号:</strong> ${BUILD_NUMBER}</p>
<p><strong>构建时间:</strong> ${BUILD_TIMESTAMP}</p>
<p><strong>Git 提交:</strong> ${GIT_COMMIT}</p>
<hr>
<h3>构建结果</h3>
<ul>
<li>✅ 代码编译成功</li>
<li>✅ 单元测试通过</li>
<li>✅ SonarQube 质检通过</li>
<li>✅ CLI 应用打包成功 (JAR)</li>
<li>✅ GUI 应用打包成功 (EXE)</li>
<li>✅ Web 应用打包成功 (WAR)</li>
<li>✅ Android 应用打包成功 (APK)</li>
<li>✅ 源代码已推送到 feature-ldl 分支</li>
<li>✅ 制品已推送到 release 分支</li>
</ul>
<hr>
<p><a href="${BUILD_URL}">查看构建详情</a></p>
<p><a href="${BUILD_URL}artifact/">下载构建制品</a></p>
<p><a href="http://localhost:9000/dashboard?id=slms:smart-library-management-system">查看 SonarQube 报告</a></p>
""",
to: '${DEFAULT_RECIPIENTS}',
mimeType: 'text/html'
)
}
failure {
echo '========== 流水线执行失败 =========='
emailext (
subject: "❌ SLMS 构建失败 - Build #${BUILD_NUMBER}",
body: """
<h2>SLMS 项目构建失败</h2>
<p><strong>构建编号:</strong> ${BUILD_NUMBER}</p>
<p><strong>构建时间:</strong> ${BUILD_TIMESTAMP}</p>
<p><strong>Git 提交:</strong> ${GIT_COMMIT}</p>
<hr>
<h3>失败原因</h3>
<p style="color: red;">请检查以下可能的问题:</p>
<ul>
<li>代码编译错误</li>
<li>单元测试失败</li>
<li>SonarQube 质检不通过</li>
<li>打包过程出错</li>
</ul>
<hr>
<p><a href="${BUILD_URL}console">查看构建日志</a></p>
<p><a href="http://localhost:9000/dashboard?id=slms:smart-library-management-system">查看 SonarQube 报告</a></p>
""",
to: '${DEFAULT_RECIPIENTS}',
mimeType: 'text/html'
)
}
always {
echo '========== 清理工作空间 =========='
cleanWs()
}
}
}