
@Library("cicdhtj@master") _
def repos = [
//     [dir: 'BASE', type: 'mvn', isUpdate: true, url: 'http://114.116.202.8:18808/factory-tobacco/factory-tobacco-basic.git'],
//     [dir: 'CORE', type: 'mvn', isUpdate: true, url: 'http://114.116.202.8:18808/factory-tobacco/factory-tobacco-qrcodecore.git'],
//     [dir: 'HTJ', type: 'mvn', isUpdate: true, url: 'http://114.116.202.8:18808/factory-tobacco/factory-tobacco-qrcode-htj.git'],
//     [dir: 'WEB', type: 'node', isUpdate: true, url: 'http://114.116.202.8:18808/factory-tobacco/factory-tobacco-qrcode-htj-vue.git']
]
def modules = [

]
def push_path = 192.168.31.200/develop

pipeline {
    agent any

    parameters {
        gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BASE', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-basic.git' // 后端
        gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'CORE', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-qrcodecore.git' // 后端
        gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'HTJ', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-qrcode-htj.git' // 后端
        gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'WEB', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-qrcode-htj-vue.git' // 后端
    }

    tools {
        jdk 'jdk8'
        nodejs 'nodejs16'
        maven 'maven3.9'
    }

    stages {
        stage('步骤一: 参数获取') {
            steps {
                script {
                    def yamlFile = readFile('project.yaml')
                    def smallfile = log.readYaml(yamlFile)

                    repos.add([dir: smallfile.base.name, type: smallfile.base.type, isUpdate: smallfile.base.isUpdate, url: smallfile.base.url])
                    repos.add([dir: smallfile.core.name, type: smallfile.core.type, isUpdate: smallfile.core.isUpdate, url: smallfile.core.url])
                    repos.add([dir: smallfile.htj.name, type: smallfile.htj.type, isUpdate: smallfile.htj.isUpdate, url: smallfile.htj.url])
                    repos.add([dir: smallfile.web.name, type: smallfile.web.type, isUpdate: smallfile.web.isUpdate, url: smallfile.web.url])

                    if(smallfile.base.isUpdate && smallfile.base.module) {
                        for (module in smallfile.base.module) {
                            modules.add([
                                dir: smallfile.base.name,
                                name: module.name,
                                version: module.version,
                                isUpdate: module.isUpdate,
                                isEncryption: module.isEncryption,
                                path: module.path
                            ])
                        }

                    }
                    if(smallfile.core.isUpdate && smallfile.core.module) {
                        for (module in smallfile.core.module) {
                            modules.add([
                                dir: smallfile.core.name,
                                name: module.name,
                                version: module.version,
                                isUpdate: module.isUpdate,
                                isEncryption: module.isEncryption,
                                path: module.path
                            ])

                        }
                    }
                    if(smallfile.htj.isUpdate && smallfile.htj.module) {
                        for (module in smallfile.htj.module) {
                            modules.add([
                                dir: smallfile.htj.name,
                                name: module.name,
                                version: module.version,
                                isUpdate: module.isUpdate,
                                isEncryption: module.isEncryption,
                                path: module.path
                            ])


                        }
                    }
                    if(smallfile.web.module) {
//                             modules.add([
//                                 dir: smallfile.web.name,
//                                 name: module.name,
//                                 version: moudle.version,
//                                 isUpdate: moudle.isUpdate,
//                                 isEncryption: moudle.isEncryption,
//                                 path: moudle.path
//                             ])
                    }

                }
            }
        }
        stage('步骤一: 代码获取') {
            steps {
                script {
                    for (repo in repos) {
                        dir(repo.dir) {
                            git branch: "${params[repo.dir]}", credentialsId: '384eb689-7944-435b-bf7c-4d9308c8b62c', url: repo.url
                        }
                    }   
                }
            }
        }

        stage('步骤二: 项目构建'){
            steps {
                script {
                    for (repo in repos) {
                        dir(repo.dir) {
                            if ( repo.isUpdate && repo.type == 'mvn') sh "mvn -Dmaven.test.failure.ignore=true -s /var/lib/jenkins/settings-itimQR.xml clean package install"
                            else if ( repo.isUpdate && repo.type == 'node') sh "npm install --registry=https://registry.npm.taobao.org && npm run build:prod-htj"

                        }
                    }
                }
            }
        }
        stage('步骤三: 条件筛选'){
            steps {
                script {
                    sh "export DOCKER_HOST=192.168.31.250"
                    for (module in modules) {
                        if(module.isUpdate && module.isEncryption){
                            sh "cd ${module.dir}/${module.path} && mvn classfinal:classFinal"
                            sh "docker build -f Docker/encryption/Dockerfile -t  ${push_path}/${module.name}:${module.version} --build-arg FILE_NAME=${module.dir}/${module.path}/target/*.jar ${workspace}"
                            sh "docker push ${push_path}/${module.name}:${module.version}"

                        }
                        if (module.isUpdate && !module.isEncryption) {
                            sh "docker build -f Docker/noencryption/Dockerfile -t  ${push_path}/${module.name}:${module.version} --build-arg FILE_NAME=${module.dir}/${module.path}/target/*.jar ${workspace}"
                            sh "docker push ${push_path}/${module.name}:${module.version}"

                        }
                    }
                }
                
            }
            post {
                success {
                    echo '构建成功'
                }
            }
        }

        stage('步骤四: 打包镜像'){
            steps {
                script {


                    sh "echo '加密并拷贝文件'"
                }
                // for (module in modules){
//                     dir(module.dir) {
//                         sh "cd ${module.path}/target/ && ls -la "
// //                         if (module.isEncryption){
// //                             sh "cd ${module.path} && mvn classfinal:classFinal"
// //                             sh "cd ${module.path}/target/ && ls -la "
// //                         }

//                     }
                // }
            }
        }
    }
}
