一个pipeline 4个功能

突发奇想让开发在测试环境实现服务的四个状态
一个pipeline的四个功能块
前提了解pipeline的语法

jenkins pipeline基础语法与示例

pipeline {
    agent any
    environment {
	   //项目名称
       PROJECT_NAME="imin"
	   //相对编译目录的jar包路径 
	   JAR_PATH = "target/${PROJECT_NAME}.jar"
	   //目标主机 jar包目录路径
	   DEST_HOST_JAR_DIR = "/home/java/${PROJECT_NAME}/"
	   //目标主机ip 或者 主机名
	   DEST_HOST="im"
	   //jar包使用的配置
	   ACTIVE_NAME = "t"
	   //ssh到目标主机的用户名
	   SSH_USER = "centos"
	   //ssh到目标主机的端口
	   SSH_PORT = "6"
	   //jar包启动监听的端口
	   SERVER_PORT="90"
    }
	stages {
		stage('拉代码'){
			when {
				environment name: 'ACT',
				value: 'update'
			}
				steps{
					git branch: "${BRANCH}", credentialsId: 'xxxxxxxxxxxx', url: 'http://git.xxxxxxxxxxxxxxxxxxxxxxxxx.git'
				}
		}
		
		stage('编译'){
			when {
				environment name: 'ACT',
				value: 'update'
			}
				steps{
					withMaven(maven: 'M3.6.3') {
						//mvn构建
						sh "mvn clean install -Dmaven.test.skip=true && ls -a"
					}
				}
		}

		stage('停止当前服务'){
			when {
				anyOf{
					environment name: 'ACT',
					value: 'update';
					
					environment name: 'ACT',
					value: 'restart';
					
					environment name: 'ACT',
					value: 'stop';
					
					environment name: 'ACT',
					value: 'rollback';
				}
			}
			steps{
			
				//停止当前服务
				sh """
					ssh -p ${SSH_PORT} -i /home/id_rsa  ${SSH_USER}@${DEST_HOST} -tt << remotessh
					(jps -mvl |grep ${PROJECT_NAME} |cut -d ' ' -f 1 |xargs kill -9 )||echo 没找到服务可以kill
					exit
					remotessh
				"""
			}
		}
		
		stage('目标主机jar包回滚'){
			when {
				environment name: 'ACT',
				value: 'rollback'
			}	    
				steps{
					//回滚jar包
					sh """
						ssh -p ${SSH_PORT} -i /home/id_rsa  ${SSH_USER}@${DEST_HOST} -tt << remotessh
						echo 备份jar包
						cd ${DEST_HOST_JAR_DIR}
						rm -rf ${PROJECT_NAME}.jar
						mv bak_${PROJECT_NAME}.jar ${PROJECT_NAME}.jar || echo 没找到备份jar包
						exit
						remotessh
					"""
				}
		}
		
		
		stage('备份并发送jar包'){
			when {
				environment name: 'ACT',
				value: 'update'
			}	    
				steps{
					//备份jar包
					sh """
						ssh -p ${SSH_PORT} -i /home/id_rsa  ${SSH_USER}@${DEST_HOST} -tt << remotessh
						echo 备份jar包
						cd ${DEST_HOST_JAR_DIR}
						rm -rf bak_${PROJECT_NAME}.jar
						mv ${PROJECT_NAME}.jar bak_${PROJECT_NAME}.jar || echo 没找到jar包无需备份
						exit
						remotessh
					"""
					//传输新的jar包
					sh "cd ${PROJECT_NAME} && scp -P${SSH_PORT} -i /home/id_rsa  ${JAR_PATH}  ${SSH_USER}@${DEST_HOST}:${DEST_HOST_JAR_DIR}"
				}
		}

		stage('启动服务'){
			when {
				anyOf{
					environment name: 'ACT',
					value: 'update';
					
					environment name: 'ACT',
					value: 'restart';
					
					environment name: 'ACT',
					value: 'rollback';
				}
			}
			steps{
				//执行部署脚本
				sh """
					ssh -p ${SSH_PORT} -i /home/id_rsa  ${SSH_USER}@${DEST_HOST} -tt << remotessh
					echo 执行脚本
					cd ${DEST_HOST_JAR_DIR}
					pwd
					ls -l
					sleep 10
					nohup java  \
						-jar ./${PROJECT_NAME}.jar \
						-Xms512 -Xmx512 -Xmn256 \
						--server.port=${SERVER_PORT} \
						--spring.profiles.active=${ACTIVE_NAME} \
						> output.log 2>&1 &
					exit
					remotessh
				"""
			}
		}
	}
}