Appearance
Jenkins 集成
Jenkinsfile 配置
groovy
// Jenkinsfile(声明式流水线)
pipeline {
agent {
docker {
image 'eclipse-temurin:17-jdk'
args '-v gradle-cache:/root/.gradle' // 挂载 Gradle 缓存
}
}
environment {
GRADLE_OPTS = '-Dorg.gradle.daemon=false' // CI 中禁用 Daemon
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh './gradlew build --build-cache --parallel'
}
}
stage('Test') {
steps {
sh './gradlew test --build-cache'
}
post {
always {
// 发布测试报告
junit '**/build/test-results/test/*.xml'
publishHTML([
reportDir: 'build/reports/tests/test',
reportFiles: 'index.html',
reportName: 'Test Report'
])
}
}
}
stage('Code Quality') {
steps {
sh './gradlew checkstyleMain spotbugsMain jacocoTestReport'
}
post {
always {
publishHTML([
reportDir: 'build/reports/jacoco/test/html',
reportFiles: 'index.html',
reportName: 'Coverage Report'
])
}
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
sh './gradlew publish'
}
}
}
post {
failure {
// 发送通知
mail to: 'team@example.com', subject: '构建失败', body: "详情:${env.BUILD_URL}"
}
}
}缓存加速
groovy
pipeline {
agent any
options {
// 保留最近 10 次构建
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage('Build') {
steps {
// 使用 Gradle 构建缓存服务器
sh '''
./gradlew build \
--build-cache \
--configuration-cache \
--parallel \
-Dorg.gradle.caching.debug=false
'''
}
}
}
}gradle.properties(CI 专用)
properties
# CI 环境的 gradle.properties
org.gradle.daemon=false # CI 不需要 Daemon
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.jvmargs=-Xmx4g -Dfile.encoding=UTF-8下一步
- GitHub Actions 集成 - GitHub CI 配置