pipeline{
	agent any
	
	//node {
		//customWorkspace ''  //自定义工作空间
	//}
		
	parameters{
		choice(
			name: 'branchName',
			choices: ['saas-feature-dev','saas-inner-dev'], 
			description: '请选择你要发布的分支名称'
		)
		choice(
			name: 'mode', 
			choices: ['deploy','rollback'], 
			description: '请选择发布或者回滚？注意：回滚到最近成功构建的版本。'
		)
	}
	
	environment{
		CI = "true"
		JAR_NAME = "platomix-gmetry-system"
		PROJECT_NAME = "platomix-gmetry-system-api"
		PACKAGE_PROJECT_FILE = "${env.WORKSPACE}\\${PROJECT_NAME}\\target\\${JAR_NAME}.jar"
		PROJECT_FOLDER = "C:\\platomix\\projects\\${JAR_NAME}"
		RUN_PROJECT_FILE = "${PROJECT_FOLDER}\\${JAR_NAME}.jar"
		BAK_PROJECT_FILE = "${PROJECT_FOLDER}\\bak\\${JAR_NAME}-bak.jar"
		PROJECT_LOG_FOLDER = "${PROJECT_FOLDER}\\logs"
		
		MAVEN_HOME = "C:\\platomix\\component\\apache-maven-3.6.3"
		GIT_HOME = "C:\\platomix\\component\\git-2.12.0.2"
	}
	
	options{
		timestamps()   //日志时间	
		
		disableConcurrentBuilds() //禁止并行
		
        timeout(time: 30, unit: 'MINUTES')  //流水线的超时时间设置30分钟
	}
	
	//triggers{}
	
	stages {
		stage("初始准备"){
			steps {
				echo "1.项目名称: ${env.JAR_NAME}"
				echo "2.项目目录: ${env.PROJECT_FOLDER}"
				echo "3.项目备份文件: ${env.BAK_PROJECT_FILE}"
				echo "清理临时文件目录开始.."
				script{
					try{
						dir("${env.WORKSPACE}@tmp") {
					      deleteDir()
					    }
					    dir("${env.WORKSPACE}@script") {
					      deleteDir()
					    }
					    dir("${env.WORKSPACE}@script@tmp") {
					      deleteDir()
					    }
						cleanWs()  //删除${WORKSPACE}目录
					}catch(err){}
				}
			    echo "清理临时文件目录结束.."
			}
		}
		
		stage("拉取代码"){
			when {
                environment name: 'mode',value: 'deploy'
            }
			steps {
				echo "开始拉取代码，当前分支：${params.branchName}"
				git branch: "${params.branchName}", credentialsId: 'f14bc12d-2f9c-4227-92cc-9b4d41a1ebac', url: 'http://git.platomix.net/aokunsang/platomix-gmetry-system.git'
			}
		}
		
		stage("编译打包"){
			when {
                environment name: 'mode',value: 'deploy'
            }
			steps {
				echo "开始构建项目，构建BuildNumber:${env.BUILD_NUMBER}"
				bat "${env.MAVEN_HOME}\\bin\\mvn clean package -e -U -Dmaven.test.skip=true"
			}
		}
		
		stage("停止服务"){
			steps {
				script{
					try{
						bat "${env.PROJECT_FOLDER}\\stop.bat"
					}catch(err){}
				}
				echo "停止服务完成！"
			}
		}
		
		stage("JAR包备份"){
			when {
                environment name: 'mode',value: 'deploy'
            }
			
			steps {
				script{
	            	def projectFile = "${env.RUN_PROJECT_FILE}"
	            	if(fileExists(projectFile) == true){
	            		bat "move ${projectFile} ${env.BAK_PROJECT_FILE}"
	            	}
	            	bat "move ${env.PACKAGE_PROJECT_FILE} ${projectFile}"
				}
			}
		}
		
		stage("代码回滚"){
			when {
                environment name: 'mode',value: 'rollback'
            }
            steps {
            	echo '开始回滚代码...'
            	script {
            		def rollbackFile = "${env.BAK_PROJECT_FILE}"
            		if(fileExists(rollbackFile) == true){
            			bat "echo f | xcopy ${rollbackFile} ${env.RUN_PROJECT_FILE} /y"
		                echo "代码回滚已完成！"
            		}else {
            			error("代码回滚失败，暂无备份JAR包。")
            		}
            	}
            }
		}
		
		stage("启动服务"){
            steps {
            	echo "正在启动服务，请稍等..."
                script{
					try{
						bat "call ${env.PROJECT_FOLDER}\\start.bat"
						sleep(time:5, unit:"SECONDS")
						timeout(time: 10, unit: 'SECONDS'){
							bat "${env.GIT_HOME}\\usr\\bin\\tail -f ${PROJECT_LOG_FOLDER}\\info.log"
						}
					}catch(err){}
				}
                echo "启动服务完成！"
	       }
		}
	}
	
	post{
		always {
			echo "always"
		}
		success {
			script {
				try{
					bat "taskkill /f /im tail.exe"
				}catch(err){}
				currentBuild.description = "构建成功！";
			}
		}
		failure {
			script {
				currentBuild.description = "构建失败！";
			}
		}
		aborted {
			script {
				currentBuild.description = "构建取消！";
			}
		}
	}
}