
@Library("cicdhtj@master") _
def repos = [
//     [dir: 'BASE', type: 'mvn', isUpdate: true, url: '.git'],
//     [dir: 'CORE', type: 'mvn', isUpdate: true, url: '.git'],
//     [dir: 'HTJ', type: 'mvn', isUpdate: true, url: '.git'],
//     [dir: 'WEB', type: 'node', isUpdate: true, url: '.git']
]
def modules = [
]
def web = [
]

def push_path = "192.168.31.201/develop"

pipeline {
    agent any

    parameters {
        gitParameter branchFilter: 'origin/release/HTJ(.*)', defaultValue: 'master', name: 'BASE', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-basic.git' // 后端
        gitParameter branchFilter: 'origin/release/HTJ(.*)', defaultValue: 'master', name: 'CORE', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-qrcodecore.git' // 后端
        gitParameter branchFilter: 'origin/release/HTJ(.*)', defaultValue: 'master', name: 'HTJ', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-qrcode-htj.git' // 后端
        gitParameter branchFilter: 'origin/release/HTJ(.*)', 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.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.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.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) {
                            web.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: "release/HTJ${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 package install"
                            else if ( repo.isUpdate && repo.type == 'node') sh "npm install --registry=https://registry.npm.taobao.org && npm run build:prod-htj"
                            else echo "xxx"
                        }
                    }
                }
            }
        }
        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 -Dmaven.test.failure.ignore=true -s /var/lib/jenkins/settings-itimQR.xml package install"
                            sh "cd ${module.dir}/${module.path} && mvn -Dmaven.test.failure.ignore=true -s /var/lib/jenkins/settings-itimQR.xml 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/*-encrypted.jar ${workspace}"
                            sh "echo Qrcode@2022 | docker login https://192.168.31.201 --username admin --password-stdin"
                            sh "docker push ${push_path}/${module.name}:${module.version}"
                            sh "docker images -f 'dangling=true' -q | xargs -r docker rmi"

                        }
                        if (module.isUpdate && !module.isEncryption) {
                            sh "cd ${module.dir}/${module.path} && mvn -Dmaven.test.failure.ignore=true -s /var/lib/jenkins/settings-itimQR.xml package install"
                            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 "echo Qrcode@2022 | docker login https://192.168.31.201 --username admin --password-stdin"
                            sh "docker push ${push_path}/${module.name}:${module.version}"
                            sh "docker images -f 'dangling=true' -q | xargs -r docker rmi"
                        }
                    }
                }
                
            }
            post {
                success {
                    echo '构建成功'
                }
            }
        }

        stage('步骤五: 服务部署'){
          steps {
             withCredentials([file(credentialsId: '00789dc5-a86e-427d-9a57-f5d02a52cb6e', variable: 'KUBECONFIG_FILE')]) {
                 script {
                    echo "xxxx"
//                     for( repo in repos){
//                         if(repo.isUpdate && repo.dir == "BASE") sh "KUBECONFIG=\$KUBECONFIG_FILE helm upgrade base base-charts/ -n base -f project.yaml"
//                         if(repo.isUpdate && repo.dir == "CORE") sh "KUBECONFIG=\$KUBECONFIG_FILE helm upgrade core core-charts/ -n core -f project.yaml"
//                         if(repo.isUpdate && repo.dir == "HTJ") sh "KUBECONFIG=\$KUBECONFIG_FILE helm upgrade htj htj-charts/ -n htj -f project.yaml"
//                     }
                 }
             }
          }
          post {
              success {
                  echo '部署实施'
              }
          }
        }
    }
}
