Jenkinsfile 1.73 KB
pipeline {
    agent any
    
    tools {
        maven 'maven-3.8'
    }
    
    stages {
        stage('Checkout') {
            steps {
                git branch: 'main',
                    url: 'https://your-gitlab.com/pengchao/pengchao-calculator.git'
            }
        }
        
        stage('Build & Test') {
            steps {
                sh 'mvn clean compile test'
            }
            
            post {
                always {
                    junit 'target/surefire-reports/*.xml'
                    publishHTML([
                        allowMissing: false,
                        alwaysLinkToLastBuild: false,
                        keepAll: true,
                        reportDir: 'target/site/jacoco',
                        reportFiles: 'index.html',
                        reportName: 'JaCoCo Coverage Report'
                    ])
                }
            }
        }
        
        stage('SonarQube Analysis') {
            steps {
                // 这里的 'sonarqube-server' 对应你在 Jenkins 系统配置中设置的 SonarQube 服务器名称
                withSonarQubeEnv('sonarqube-server') {
                    sh 'mvn sonar:sonar'
                }
            }
        }
        
        stage('Quality Gate Check') {
            steps {
                timeout(time: 10, unit: 'MINUTES') {
                    waitForQualityGate abortPipeline: true
                }
            }
        }
    }
    
    post {
        always {
            echo "Pengchao's的计算器项目构建完成 - 构建号: ${BUILD_NUMBER}"
        }
        success {
            echo "✅ 代码质量检查通过!"
        }
        failure {
            echo "❌ 构建失败,请检查日志"
        }
    }
}