pipeline { //pipeline 顶层
    //any 在任何可用的agent上执行Pipeline或stage
    agent any
    //environment 自定义的环境变量
    environment {
        PROGRAM_ID = "integrated-calendar-center"     					//项目名称
        APPLICATION_NAME = "calendar-center"     						//项目应用名-镜像名称
        PROJECT_JAR_PATH = "integrated-business/integrated-calendar-center"					//项目打包后所在相对路径
        WORKSPACE = "/root/jenkins"    							//jenkins home 宿主机路径
        PROJECT_ROOT_PATH = "integrated-calendar-center"						//项目根路径名称，jenkins制作pipeline时，自定义的
        WORKER_ROOT_HOME = "/mnt/integrated"								//工作目录根目录-容器内的路径 
        WORKER_HOME = "$WORKER_ROOT_HOME/$APPLICATION_NAME"   				//项目运行所在路径-容器内的路径 
        DEPLOY_FILE_PATH = "integrated-doc/docker/$PROGRAM_ID"				//部署文件所在路径
        MAVEN_PACKAGE_PATH = "$WORKSPACE/workspace/$PROJECT_ROOT_PATH"		//maven打包根路径
        IMAGE_REPOSITORY_PREFIX="labisenlin.persagy.com"					//私有镜像仓库的前缀域名
        DOCKER_IMAGE = "$IMAGE_REPOSITORY_PREFIX/$IMAGE_REPOSITORY_PROJECT/$APPLICATION_NAME:$VERSION"	//镜像全路径
        BACKUP_PATH = "$JENKINS_HOME/program-backup/$PROJECT_ROOT_PATH/$PROGRAM_ID/$VERSION"	//备份路径
        
        SERVER_PORT = "8815"													// yml部署文件中服务端口号
        EUREKA_CLIENT_DEFAULT_ZONE = "http://frame:123456@192.168.64.18:9931/eureka/"	// yml部署文件中eureka注册地址
        EUREKA_INSTANCE_IP_ADDRESS = "192.168.64.17"							// yml部署文件中eureka登录自身IP地址
        SERVER_LOGS_LOCAL_PATH = "/mnt/integrated/logs/$APPLICATION_NAME"		// yml部署文件中,服务日志，宿主机存放位置
        
        REMOTE_DEPLOY = "true"									// false本机部署，true-指定部署服务器
        REMOTE_PORT = "22"										// 远程服务器ssh登录端口
        REMOTE_USER_IP = "root@192.168.64.17"					// 远程服务器ssh登录用户及IP
        REMOTE_DEPLOY_PATH = "/mnt/integrated/docker"			// 远程服务器部署所需的文件存放位置
    }
    //不允许并行执行Pipeline,可用于防止同时访问共享资源等
    options {
        disableConcurrentBuilds()
    }
    stages {
        //部署流程一：从SVN或者Git上拉取项目程序代码
        stage('git checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/$GIT_BRANCH']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-integrated-server-deploy', url: 'http://39.106.8.246:3003/persagy/integrated-platform.git']]])
            }
        }
        //部署流程二：Java程序进行maven打包
        stage('maven build') {
            steps {
                sh 'mvn clean install -Dmaven.test.skip=true -f $MAVEN_PACKAGE_PATH/pom.xml'
            }
        }
        //部署流程三：根据项目dockerfile创建项目的docker镜像
        stage('image buid') {
            steps {
                sh 'chmod -R +x ./$DEPLOY_FILE_PATH/'
                sh 'rm -rf ./$DEPLOY_FILE_PATH/*.jar && cp ./$PROJECT_JAR_PATH/target/*.jar ./$DEPLOY_FILE_PATH'
                sh 'docker build -f ./$DEPLOY_FILE_PATH/Dockerfile --build-arg WORKER_HOME=$WORKER_HOME -t $DOCKER_IMAGE ./$DEPLOY_FILE_PATH'
            }
        }
        //部署流程四：项目推送到本地私有仓库
        stage('repository push') {
            steps {
                sh 'docker login $IMAGE_REPOSITORY_PREFIX -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
                sh 'docker push $DOCKER_IMAGE'
                sh 'docker logout $IMAGE_REPOSITORY_PREFIX'
            }
        }
        //部署流程五：项目版本本地备份
        stage('backup') {
            steps {
                sh 'rm -rf $BACKUP_PATH/'
                sh 'mkdir -p $BACKUP_PATH/'
                sh 'cp ./$PROJECT_JAR_PATH/target/*.jar $BACKUP_PATH/'
            }
        }
        //创建环境变量文件，分发文件
        stage('deploy') {
            steps {
            	sh '''
					cat >$MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH/.env <<-EOF 
					SERVER_PORT=$SERVER_PORT
					DOCKER_IMAGE=$DOCKER_IMAGE
					WORKER_ROOT_HOME=$WORKER_ROOT_HOME
					APPLICATION_NAME=$APPLICATION_NAME
					SERVER_LOGS_LOCAL_PATH=$SERVER_LOGS_LOCAL_PATH
					EUREKA_CLIENT_DEFAULT_ZONE=$EUREKA_CLIENT_DEFAULT_ZONE
					EUREKA_INSTANCE_IP_ADDRESS=$EUREKA_INSTANCE_IP_ADDRESS
					EOF
				'''
            	script {
                    if (env.REMOTE_DEPLOY == 'true') {
                    	sh 'scp -q -r -P $REMOTE_PORT $MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH/ $REMOTE_USER_IP:$REMOTE_DEPLOY_PATH'
                        sh 'ssh -tt -p $REMOTE_PORT $REMOTE_USER_IP "$REMOTE_DEPLOY_PATH/$PROGRAM_ID/deploy.sh $APPLICATION_NAME $REMOTE_DEPLOY_PATH/$PROGRAM_ID && exit"'
                    } else {
                    	sh '$MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH/deploy.sh $APPLICATION_NAME $MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH'
                    }
                }
            }
        }
    }
}