pipeline {
    agent any

    tools {
        maven 'Maven-3.8.5'
        jdk 'JDK8'
    }

    parameters {
        choice(name: 'ENV', choices: ['prod'], description: '部署环境')
        choice(name: 'SERVICE', choices: ['gateway', 'work', 'oa', 'authorization', 'admin', 'bi', 'crm', 'job', 'examine', 'hrm'], description: '选择要部署的微服务')
    }
    environment {
        DOCKER_REGISTRY = "registry.cn-shanghai.aliyuncs.com"
        DOCKER_NAMESPACE = "advich-group"
        DOCKER_REPO = "weichi-crm-${params.SERVICE}"
        DOCKER_IMAGE = "${DOCKER_REGISTRY}/${DOCKER_NAMESPACE}/${DOCKER_REPO}:latest"
        PROJECT_SERVICE_COMMON_NAME = "core"
    }

    stages {
        stage('拉取Git代码') {
            steps {
                checkout scmGit(branches: [[name: '${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'fb3553e7-6ea6-4f34-92a0-6ba34a71c5b5', url: 'https://git.dev.advichcloud.com/wangfan/weichi-crm-server.git']])
            }
        }

        stage('公共工程打包') {
            steps {
                script {
                    echo '****************************** 公共工程打包 ******************************'
                    sh """
                        mvn -f ${PROJECT_SERVICE_COMMON_NAME} clean install -Dmaven.test.skip=true
                    """
                }
            }
        }

        stage('微服务工程打包') {
            steps {
                script {
                    echo '****************************** 微服务工程打包 ******************************'
                    sh "mvn -f ${params.SERVICE} clean install -Dmaven.test.skip=true"
                }
            }
        }

        stage('Build and Push Docker Image') {
            steps {
                script {
                    withCredentials([usernamePassword(
                        credentialsId: '6ec7fc8d-859f-43ba-8ebc-337bedec055b',
                        usernameVariable: 'REGISTRY_USERNAME',
                        passwordVariable: 'REGISTRY_PASSWORD'
                    )]) {
                        sh """
                            cd ${params.SERVICE}
                            echo "\${REGISTRY_PASSWORD}" | docker login ${DOCKER_REGISTRY} -u \${REGISTRY_USERNAME} --password-stdin
                            docker build -t ${DOCKER_IMAGE} .
                            docker push ${DOCKER_IMAGE}
                        """
                    }
                }
            }
        }

        stage('Deploy to Server') {
            when {
                expression { return params.ENV == 'prod' }
            }
            steps {
                script {
                    echo '****************************** 部署到服务器 ******************************'
                    def DOCKER_REGISTRY = "${DOCKER_REGISTRY}"
                    def DOCKER_NAMESPACE = "${DOCKER_NAMESPACE}"
                    def DOCKER_REPO = "${DOCKER_REPO}"
                    sshPublisher(publishers: [sshPublisherDesc(configName:'CRM(阿里云)', transfers: [sshTransfer(cleanRemote: false, excludes: '',
                    execCommand: """
                        docker login -u wangfan@1934160439855919 -p Initial0 ${DOCKER_REGISTRY}
                        docker pull ${DOCKER_IMAGE}
                        docker rm -f ${DOCKER_REPO} || true
                        docker run -d --name ${DOCKER_REPO} ${DOCKER_IMAGE}
                    """,
                    execTimeout: 120000, flatten: false, makeEmptyDirs: false,
                    noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '',
                    remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
                    usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose:
                    false)])
                }
            }
        }

        stage('Clean Workspace') {
            steps {
                cleanWs()
            }
        }

    }
}