pipeline {
    agent any
//    options {
//        ansiColor('xterm')
//    }
//    properties([parameters([gitParameter(branch: '', branchFilter: '.*', defaultValue: '*/master', name: 'buildBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition')])])
    tools {
        maven: 'MavenInner'
    }
    parameters {
//        string(name: 'BRANCH_NAME', defaultValue: 'main', description: '请输入要构建的分支名称')
        gitParameter(branch: '', branchFilter: '.*', defaultValue: '*/master', name: 'buildBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition')
    }
    environment {
        GIT_CREDENTIALS = credentials('your-gitlab-credentials-id')
        GIT_URL='ssh://git@git.keyfil.com:9922/qifu-gateway/keycloak-justauth.git'
    }
    stages {
        stage('Checkout') {
            steps {
                echo "正在从 GitLab 拉取 ${params.BRANCH_NAME} 分支的代码..."
//                git credentialsId: "${GIT_CREDENTIALS}", url: 'https://gitlab.com/your-repo.git', branch: "${params.BRANCH_NAME}"
                git branch: "${params.buildBranch}", credentialsId: '1', url: "${GIT_URL}"
            }
        }
        stage('Maven Build') {
            steps {
                echo "开始执行 Maven 构建..."
                sh 'mvn clean test jacoco:report -Daliyun=*'
            }
        }
        stage('Generate Coverage Report') {
            steps {
                echo "使用 Git Forensics 统计代码变更..."
                discoverGitReferenceBuild referenceJob: 'Jacoco测试', targetBranch: '$referenceJob'
                recordCoverage qualityGates: [[criticality: 'NOTE', metric: 'MODULE'], [baseline: 'MODIFIED_FILES', criticality: 'NOTE', metric: 'FILE'], [baseline: 'MODIFIED_LINES', criticality: 'NOTE', metric: 'LINE']], tools: [[parser: 'JACOCO']]
            }
        }
    }
    post {
        success {
            echo "构建成功！"
        }
        failure {
            echo "构建失败，请检查日志。"
        }
    }
}