
@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 webs = [
]

def push_path = "192.168.31.201/develop"


// def defaultBranch="release/HTJ-2.03.003-20241108"
def defaultBranch="feature/HTJ-2.11.03-20250514"

pipeline {
    agent any

    parameters {
        booleanParam(name: 'TAR', defaultValue: false, description: '打包成Tar, 勾选生成Tar, 否则不生成') //
        gitParameter branchFilter: 'origin/(.*)', defaultValue: defaultBranch, name: 'BASE', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-basic.git' // 后端
        gitParameter branchFilter: 'origin/(.*)', defaultValue: defaultBranch, name: 'CORE', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-qrcodecore.git' // 后端
        gitParameter branchFilter: 'origin/(.*)', defaultValue: defaultBranch, name: 'HTJ', type: 'PT_BRANCH_TAG', useRepository: '.*factory-tobacco-qrcode-htj.git' // 后端
        gitParameter branchFilter: 'origin/(.*)', defaultValue: defaultBranch, 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.dir, type: smallfile.base.type, isUpdate: smallfile.base.isUpdate, url: smallfile.base.url])
                    repos.add([dir: smallfile.core.dir, type: smallfile.core.type, isUpdate: smallfile.core.isUpdate, url: smallfile.core.url])
                    repos.add([dir: smallfile.htj.dir, type: smallfile.htj.type, isUpdate: smallfile.htj.isUpdate, url: smallfile.htj.url])
                    repos.add([dir: smallfile.web.dir, type: smallfile.web.type, isUpdate: smallfile.web.isUpdate, url: smallfile.web.url])

                    if(smallfile.pushPath) push_path=smallfile.pushPath


                    if(smallfile.base.module) {
                        for (module in smallfile.base.module) {
                            modules.add([
                                dir: smallfile.base.dir,
                                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.dir,
                                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.dir,
                                name: module.name,
                                version: module.version,
                                isUpdate: module.isUpdate,
                                isEncryption: module.isEncryption,
                                path: module.path
                            ])
                        }
                    }
                    if(smallfile.web) {
                            webs.add([
                                dir: smallfile.web.dir,
                                name: smallfile.web.name,
                                version: smallfile.web.version,
                                isUpdate: smallfile.web.isUpdate
                            ])
                    }

                }
            }
        }
        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.npmmirror.com && 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 clean 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 clean package install"
                            if(module.name == 'qrcode-modules-data-sharing'){
                                sh "docker build -f Docker/mysqldump/Dockerfile -t  ${push_path}/${module.name}:${module.version} --build-arg FILE_NAME=${module.dir}/${module.path}/target/*.jar ${workspace}"
                            } else if(module.name == 'qrcode-modules-product-relation-pcb') {
                                sh "docker build -f Docker/relation/Dockerfile -t  ${push_path}/${module.name}:${module.version} --build-arg FILE_NAME=${module.dir}/${module.path}/target/*.jar ${workspace}"
                            } else {
                                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 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"
                        }
                        if (module.isUpdate && params.TAR) {
                            sh "docker tag ${push_path}/${module.name}:${module.version} ${module.name}:${module.version}"
                            sh "cd Docker/images && docker save -o ${module.name}-${module.version}.tar ${module.name}:${module.version}"
                        }
                    }
                }
                
            }
            post {
               success {
                   archiveArtifacts artifacts: 'Docker/images/**.tar', followSymlinks: false
                   echo '构建成功'
               }
            }
        }
        stage('步骤五: 前端构建'){
            steps {
                script {
                    for (web in webs) {
                        if(web.isUpdate){
                            sh "docker build -f Docker/nginx/Dockerfile -t  ${push_path}/${web.name}:${web.version} --build-arg FILE_NAME=${web.dir}/dist/ ${workspace}"
                            sh "echo Qrcode@2022 | docker login https://192.168.31.201 --username admin --password-stdin"
                            sh "docker push ${push_path}/${web.name}:${web.version}"
                            sh "docker images -f 'dangling=true' -q | xargs -r docker rmi"
                        }
                    }
                }
            }
        }


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