KACE华润信托流水线

import hudson.model.*;


def compileWeb(String path){
    sh """
    cd $path || exit
    rm -rf package-lock.json yarn.lock
    yarn install
    yarn run build
    """

}
def flywayMigrate(String databaseType, String env, String path, String action) {
    if(action=="all"){
        sh """
            ansible-playbook playbooks/clean.yml \
                -e database="$databaseType" \
                -e deploy_env="$env" \
                -e workspace="$path" 
            ansible-playbook playbooks/migrate.yml \
                -e database="$databaseType" \
                -e deploy_env="$env" \
                -e workspace="$path" 
        """
    }else{
        sh """
            ansible-playbook playbooks/migrate.yml \
                -e database="$databaseType" \
                -e deploy_env="$env" \
                -e workspace="$path" 
        """
    }
}


def rsyncFile(String src, String des, type="standalone"){
    // delete file exist.
    sh """
        [ -d $src -a -d $des ] &&  rsync -av --delete  ${src}/  $des || echo "Input parameters do not meet the requirements"
        [ $type = msa ] && \\cp -r ${src}/config.json.msa ${des}/config.json || echo "No need to copy file"
    """
}

pipeline{

    agent{

        node{
            label 'Node10.202.63.17'
        }
    }

    parameters{
        choice choices: ['Hotfix_2.1.0'], description: '选择代码分支]', name: 'gitlab_branch'
        choice choices: ['oracle', 'mysql', 'dm'], description: '指定想要升级的数据库类型', name: 'selected_database'
        booleanParam (defaultValue: true, description: '编译KACE后端', name: 'build_application')
        booleanParam (defaultValue: true, description: '编译KACE前端', name: 'build_web')
        booleanParam (defaultValue: false, description: '升级数据库', name: 'update_database')
        choice choices: ['all', 'update'], description: '是否全量更新数据库:(all为全量, upgrade为增量)', name: 'update_strategy'
        booleanParam (defaultValue: true, description: '部署KACE后端', name: 'deploy_application')
        booleanParam (defaultValue: true, description: '部署KACE前端', name: 'deploy_web')
        booleanParam (defaultValue: false, description: '发布KACE到MAVEN制品库[SNAPSHOT]', name: 'publish_kace')
    }

    environment{
        microservice_web="/opt/web/ace-web-micro"
        deploy_env="develop"
        ansible_src="${env.WORKSPACE}/deploy/ansible"
        boot_app="/opt/huarun/bootapp"
    }

    stages{

        stage('拉取代码'){

            steps{
                git  branch: '$gitlab_branch', credentialsId: 'git-user', url: 'http://xxx/jr/kace/koca-cf.git'
            }
        }

        stage('打印变量'){

            steps{

                script{
                    pom = readMavenPom file: './ace/parent/pom.xml'
                    // 设置appVersion为整个项目版本号
                    packageVersion = "${pom.properties.revision}"
                    git_commit_id = sh (
                    script: 'git rev-parse --short HEAD',
                    returnStdout: true
                    ).trim()
                    println("="*80);
                    println("当前工作目录为: ${env.WORKSPACE}")
                    println("当前代码分支: ${gitlab_branch}")
                    println("选择数据库类型: ${selected_database}")
                    println("应用版本: ${packageVersion}")
                    println("Git提交ID: ${git_commit_id}")
                    println("部署环境: ${deploy_env}")
                    println("="*80);
                }
            }
        }

        stage('编译KACE'){

            parallel{

                stage('编译KACE后端'){

                    when{

                        expression{
                            return params.build_application
                        }
                    }

                    steps{

                        dir('ace'){
                            sh 'cd parent && mvn clean install -Dmaven.test.skip=true'
                            sh 'cd modules && mvn clean install -Dmaven.test.skip=true'
                            //                            sh 'cd bootapps/koca-center&& mvn clean package -Dmaven.test.skip=true'
                        }
                    }
                }

                stage('编译KACE前端'){

                    when{

                        expression{
                            return params.build_web
                        }
                    }

                    steps{

                        script{
                            compileWeb("web/framework")
                        }
                    }
                }
            }
        }

        stage('升级数据库'){

            when{

                expression{
                    return params.update_database
                }
            }

            steps{

                dir("${ansible_src}"){

                    script{
                        flywayMigrate("$selected_database","$deploy_env","${env.WORKSPACE}", "$update_strategy")
                    }
                }
            }
        }

        stage('部署KACE'){

            parallel{

                stage('部署KACE后端'){

                    when{

                        expression{
                            return params.deploy_application
                        }
                    }

                    stages{

                        stage('部署KACE'){

                            steps{

                                dir("${boot_app}"){

                                    script{
                                        sh """
                                        mvn clean package -Dmaven.test.skip=true
                                        cd msa-bootapp/target
                                        rm -rf ${boot_app}/msa-bootapp/src/lib/*
                                        cp msa-bootapp-APP-*-full.jar ${boot_app}/msa-bootapp/src/lib
                                        cd ${boot_app}/msa-bootapp/src/bin
                                        ./shutdown.sh
                                        JENKINS_NODE_COOKIE=dontKillMe && ${boot_app}/msa-bootapp/src/bin/startup.sh
                                        sleep 10
                                        """
                                    }
                                }
                            }
                        }
                    }
                }

                stage('部署KACE前端'){

                    when{

                        expression{
                            return params.deploy_web
                        }
                    }

                    steps{

                        script{
                            rsyncFile("web/framework/dist", "${microservice_web}/dist", "msa")
                        }
                    }
                }
            }
        }

        stage('发布KACE到制品库'){

            when{

                expression{
                    return params.publish_kace
                }
            }

            steps{

                dir('ace'){

                    configFileProvider([configFile(fileId: '2de38b15-b7d3-496a-bcfc-2f7a956b6796', variable: 'MAVEN_SZ')]){
                        sh 'cd parent && mvn -s $MAVEN_SZ  clean deploy -Dmaven.test.skip=true'
                        sh 'cd modules && mvn -s $MAVEN_SZ clean deploy -Dmaven.test.skip=true'
                    }
                }
            }
        }
    }
}