node {
    def workspace=pwd()
    // 版本
    def tag = "0.8"
    def ali_registry = "registry.cn-beijing.aliyuncs.com"
    // 镜像仓库的地址
    // def harbor_url = "192.168.81.102:85"
    // 镜像仓库的项目,这里建议项目名称和jenkins的item项目名称、以及harbor的项目名称保持一致，否则用一下脚本会出问题
    // def harbor_project = "demo"
    def mavenPath="/usr/share/maven"
    // 拉取代码
    stage('pull code') {
        checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '0f6d6eaa8754e735262afa495fe2828d611fca17', url: 'http://39.105.23.186:3000/develop/digimeta-MultiSaas.git']]])
    }
    // 代码静态检查
    stage('Maven Package and Sonar') {
        if  ("${project_name}" ==  'digimeta-MultiSaas' ) {
            echo '打包根目录'
            sh 'mvn clean package sonar:sonar'
        } else {
            echo  "打包子目录并进行代码检查 ${project_name}"
            sh "mvn clean install"
            sh "mvn -f ${project_name} clean package -e -U -Dmaven.test.skip=true sonar:sonar"
        }

        //script {
            //引入Jenkins SonarQube-Scanner全局工具 "全局配置中以SonarQube-Scanner命名的工具"
        //    scannerHome = tool 'SonarQube-Scanner'
        //}
        //引用SonarQube环境 "系统配置中配置的SonarQube servers的name值 "
        //withSonarQubeEnv('Sonar') {
        //执行sonar-scanner命令
        //sh "${scannerHome}/bin/sonar-scanner"
        //    $mavenPath/bin/mvn sonar:sonar
        //}
    }
    // build Docker并推送镜像仓库
    stage('build project') {
        if  ("${project_name}" ==  'digimeta-MultiSaas' ) {
            echo '仅做代码检查，不打包目录'
        } else {
            echo  "构件微服务 ${project_name}，并推送到镜像仓库"
            sh """
                cd ${workspace}/${project_name}
                docker login --username=缔智元2023 ${ali_registry}
                docker build --tag ${ali_registry}/digitalman-multisaas/${project_name}:${version}
                docker push ${ali_registry}/digitalman-multisaas/${project_name}:${version}
            """

        }
        //echo "把jar上传镜像仓库"
        //def oldImageName = "${project_name}:latest"
        //def newImageName = "${harbor_url}/${harbor_project}/${project_name}:${tag}"
        // 改名称 做规范
        //sh "docker tag ${oldImageName} ${newImageName}"
        // 删除之前的 镜像
        //sh "docker rmi ${oldImageName}"
        // 推送到 dockers仓库
        //withCredentials([usernamePassword(credentialsId: '8a3d7ab1-4cd6-482c-86c9-a12aa6404d98', passwordVariable: 'harbor_password', usernameVariable: 'harbor_account')]) {
            // 登录
            //sh "docker login -u ${harbor_account} -p ${harbor_password} ${harbor_url}"
            // 上传
            //sh "docker push ${newImageName}"
            //echo "镜像推送成功"
        //}
    }
}