请在容器内部安装mvn以及对应的jdk

1.启动命令

docker run -u root -d --name=gch-jenkins-v2.0 -p8082:8080 \
-v /home/dockerData/jenkinsHome_V2/:/var/jenkins_home \
-v /home/dockerData/nodejs/:/opt/soft/nodejs-v16.15 \
-v $(which docker):/usr/bin/docker \
-v /var/run/docker.sock:/var/run/docker.sock \
gch-docker-jenkins:v2.0

2.注意安装前请更新依赖

3.JDK配置

/etc/profile

export JAVA_HOME=/usr/local/jdk1.8.0_202
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH

4.jenkins部署pipeline配置

cp 可用scp实现多服务器编译文件复制

node {
   def mvnHome
   stage("请输入部署的环境"){
    def inputResp = input id: "input_${BUILD_NUMBER}", message: '''请输入部署的环境【dev-test-pro】''', parameters: [string(defaultValue: 'dev', description: '''确认部署环境,输入指令后点击Proceed【继续】,终止请点击Abort【终止】''', name: 'action')]
    
    if(inputResp == "dev")
    {
        // throw new Exception("The input is incorrect, we abort this job..")
        println "开始部署到开发环境......"
    }
    else if(inputResp == "pro")
    {
        // throw new Exception("The input is incorrect, we abort this job..")
        println "开始部署到正式环境......"
    }
    else if(inputResp == "test")
    {
        // throw new Exception("The input is incorrect, we abort this job..")
        println "开始部署到测试环境......"
    }else{
        println "开始部署到$inputResp环境......"
    }
     environment = "$inputResp"   
    }
    stage('Preparation') { // for display purposes
    	// 这个是docker内部jenkins 的绝对路径
        
    	// 目标服务器的用户名
		sshUserName = "username"
		// 目标服务器的密码
		sshPassWord = "password"
		// 目标服务器的IP
		sshIp = "ip"
		// 目标服务器的端口
		sshPort = "port"
		// 当前jenkins创建的item名称
		itemName = 'itemName'
		// 当前github拉下来的项目存放位置
		workSpaceName = 'workSpacePath'
        // 这里配置git路径 可以通过流程语法中 git 选项填写获得
        // 下面配置请根据实际情况,可以不用修改
        git credentialsId: '38d7041b-2246-4473-94e7-c7cef7be3251', url: 'gitUrl'
		// 容器里
		mvnHome = '/opt/soft/apache-maven-3.9.2'
		// 容器外的根位置
		jenkinsHomeMain = '/home/dockerData/jenkinsHome'
	    // 容器里的根位置
	    jenkinsHome = '/var/jenkins_home'
		// java 运行所在绝对路径,指代:jenkinsHomeMain/javaPath
		javaPath = 'jdk1.8.0_202/bin/java'
		// 记录第一个java程序运行标识
		pid = ""
		// 记录jar包名称
		jarNamePre = ""
		// 移动后的jar包名称
		jarNameMv = ""
		// 用于存储获取jar包的数组
		jarNames = null
    }
    stage('Build') {
        // 整体build
        echo "pid $pid"
        sh "$mvnHome/bin/mvn clean install -Dmaven.test.skip=true"
        echo "====================maven构建成功============================="  
    }
    stage('获取当前jar包名称,变换目的为了一系统多环境'){
        jarNamePre = sh(script:"find $jenkinsHome/workspace/$itemName/$workSpaceName/target  -maxdepth 1 -name *.jar", returnStdout:true).trim()
        jarNames = jarNamePre.split("/");
        echo "$jarNames"
        jarNamePre = jarNames[jarNames.size() - 1];
        echo "$jarNamePre"
        jarNameMv = environment + "-" + jarNamePre;
        echo "===================变换后的jar包:$jarNameMv=============="
    }
    stage('获取执行程序的pid'){
        pid = sh(script:"/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp ps -ef \\| grep $jarNameMv \\| grep -v grep \\|awk \\'{print \'\$2\'}\\'", returnStdout:true).trim()
    }
    stage('Results') {
        // 远程执行当前程序的脚本文件
		echo "===================开始拷贝文件========================="
		echo "==删除当前item目录[rm -rf $jenkinsHome/$environment/$itemName]=="
		sh "rm -rf $jenkinsHome/$environment/$itemName"
		
		echo "===================创建当前item目录====================="
		sh "mkdir -p $jenkinsHome/$environment/$itemName"
		
		echo "===================创建当前日志文件====================="
		sh "touch $jenkinsHome/$environment/$itemName/$itemName\\.log"
		
		echo "===================复制当前运行jar包===================="
		sh "cp $jenkinsHome/workspace/$itemName/$workSpaceName/target/$jarNamePre $jenkinsHome/$environment/$itemName/$jarNameMv"
		
		echo "===================授予执行权限========================="
		sh "chmod 777 $jenkinsHome/$environment/$itemName/*"
        // 停止程序
        if(pid.length() > 0){
            echo "===================停止正在执行的程序==================="
            sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp ps -ef \\| grep $jarNameMv \\| grep -v grep \\|awk \\'{print \'\$2\'}\\' \\| xargs kill -9;echo '杀掉程序'"
        }
        echo "===================运行当前程序==================="
		sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp nohup $jenkinsHomeMain/$javaPath -jar $jenkinsHomeMain/$environment/$itemName/$jarNameMv --spring.profiles.active=$environment \\> $jenkinsHomeMain/$environment/$itemName/$itemName\\.log &"
        echo "====================运行成功============================="  
    }
}

多环境部署

//成功好使的命令
node {
   // 定义mvnHome
   def mvnHome
   // 目标服务器的用户名
   def sshUserName = "[name]"
   // 目标服务器的密码
   def sshPassWord = "[password]"
   // 目标服务器的IP
   def sshIp = "[ip]"
   // 目标服务器的端口
   def sshPort = "[port]"
   // 容器外的根位置(远程部署机器根的位置)
   def jenkinsHomeMain = '/home/dockerData/jenkinsHome'
   // java 运行所在绝对路径,指代:jenkinsHomeMain/javaPath
   def javaPath = 'jdk1.8.0_202/bin/java'
   
   stage("确认部署到生产"){
    def inputResp = input id: "input_${BUILD_NUMBER}", message: '''请输入部署的环境【dev-test-pro】''', parameters: [string(defaultValue: 'dev', description: '''确认是否部署到生产,输入指令后点击Proceed,终止请点击Abort''', name: 'action')]
    if(inputResp == "dev")
    {
        // throw new Exception("The input is incorrect, we abort this job..")
        println "开始部署到开发环境......"
        
    }
    else if(inputResp == "pro")
    {
        // throw new Exception("The input is incorrect, we abort this job..")
        println "开始部署到正式环境......"
		sshUserName = "[name]"
		sshPassWord = "[password]"
		sshIp = "[ip]"
		sshPort = "[port]"
		jenkinsHomeMain = '/home/javaHome'
		javaPath= '/usr/local/src/jdk1.8.0_202/bin/java'
		git credentialsId: '38d7041b-2246-4473-94e7-c7cef7be3251', url: 'gitUrl'
    }
    else if(inputResp == "test")
    {
	    git branch: 'dev', credentialsId: '38d7041b-2246-4473-94e7-c7cef7be3251', url: 'gitUrl'
        // throw new Exception("The input is incorrect, we abort this job..")
        println "开始部署到测试环境......"
    }else{
        git branch: 'dev', credentialsId: '38d7041b-2246-4473-94e7-c7cef7be3251', url: 'gitUrl'
		println "开始部署到$inputResp环境......"
    }
     environment = "$inputResp"   
    }
    stage('Preparation') { // for display purposes
		// 当前jenkins创建的item名称
		itemName = 'gch-aas-knowledge'
		// 当前github拉下来的项目存放位置
		workSpaceName = 'gch-aas-service/gch-aas-knowledge'
        // 这里配置git路径 可以通过流程语法中 git 选项填写获得
        // 下面配置请根据实际情况,可以不用修改
        // git credentialsId: '$credentialsIdd', url: '$urll'
		// 容器里
		mvnHome = '/opt/soft/apache-maven-3.9.2'
	    // 容器里的根位置
	    jenkinsHome = '/var/jenkins_home'
		// 记录第一个java程序运行标识
		pid = ""
		// 记录jar包名称
		jarNamePre = ""
		// 移动后的jar包名称
		jarNameMv = ""
		// 用于存储获取jar包的数组
		jarNames = null
		
    }
    stage('Build') {
        // 整体build
        echo "pid $pid"
        sh "$mvnHome/bin/mvn clean install -Dmaven.test.skip=true"
        echo "====================maven构建成功============================="  
    }
    stage('获取当前jar包名称,变换目的为了一系统多环境'){
        jarNamePre = sh(script:"find $jenkinsHome/workspace/$itemName/$workSpaceName/target  -maxdepth 1 -name *.jar", returnStdout:true).trim()
        jarNames = jarNamePre.split("/");
        echo "$jarNames"
        jarNamePre = jarNames[jarNames.size() - 1];
        echo "$jarNamePre"
        jarNameMv = environment + "-" + jarNamePre;
        echo "===================变换后的jar包:$jarNameMv=============="
    }
    stage('获取执行程序的pid'){
        pid = sh(script:"/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp ps -ef \\| grep $jarNameMv \\| grep -v grep \\|awk \\'{print \'\$2\'}\\'", returnStdout:true).trim()
    }
    stage('容器内修改jar包内容'){
        if(environment == "pro"){
             // 远程执行当前程序的脚本文件
		     echo "===================开始拷贝文件========================="
		     echo "==删除当前item目录[rm -rf $jenkinsHome/$environment/$itemName]=="
		     sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp rm -rf $jenkinsHomeMain/$environment/$itemName"
		
		     echo "===================创建当前item目录====================="
		     sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp mkdir -p $jenkinsHomeMain/$environment/$itemName"
		
		     echo "===================创建当前日志文件====================="
		     sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp touch $jenkinsHomeMain/$environment/$itemName/$itemName\\.log"
        }else{
             // 远程执行当前程序的脚本文件
		     echo "===================开始拷贝文件========================="
		     echo "==删除当前item目录[rm -rf $jenkinsHome/$environment/$itemName]=="
		     sh "rm -rf $jenkinsHome/$environment/$itemName"
		
		     echo "===================创建当前item目录====================="
		     sh "mkdir -p $jenkinsHome/$environment/$itemName"
		
		     echo "===================创建当前日志文件====================="
		     sh "touch $jenkinsHome/$environment/$itemName/$itemName\\.log"
        }
       
    }
    stage('Results') {
		if(environment == "pro"){
		    echo "===================复制当前运行jar包===================="
		    sh "/usr/bin/sshpass -p '$sshPassWord' scp -o StrictHostKeyChecking=no -P$sshPort -r $jenkinsHome/workspace/$itemName/$workSpaceName/target/$jarNamePre $sshUserName@$sshIp:/$jenkinsHomeMain/$environment/$itemName/$jarNameMv"
		    echo "===================授予执行权限========================="
		    sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp chmod 777 $jenkinsHomeMain/$environment/$itemName/*"
            // 停止程序
            if(pid.length() > 0){
                echo "===================停止正在执行的程序==================="
                sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp ps -ef \\| grep $jarNameMv \\| grep -v grep \\|awk \\'{print \'\$2\'}\\' \\| xargs kill -9;echo '杀掉程序'"
            }
            echo "===================运行当前程序==================="
	    	sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp nohup $javaPath -jar $jenkinsHomeMain/$environment/$itemName/$jarNameMv --spring.profiles.active=$environment \\> $jenkinsHomeMain/$environment/$itemName/$itemName\\.log &"
            echo "====================运行成功============================="  
            echo "===============删除无用拉取的文件========================"
            sh "rm -rf $jenkinsHome/workspace/$itemName/*"
		}else{
		    echo "===================复制当前运行jar包===================="
		    sh "cp $jenkinsHome/workspace/$itemName/$workSpaceName/target/$jarNamePre $jenkinsHome/$environment/$itemName/$jarNameMv"
		
		    echo "===================授予执行权限========================="
		    sh "chmod 777 $jenkinsHome/$environment/$itemName/*"
            // 停止程序
            if(pid.length() > 0){
                echo "===================停止正在执行的程序==================="
                sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp ps -ef \\| grep $jarNameMv \\| grep -v grep \\|awk \\'{print \'\$2\'}\\' \\| xargs kill -9;echo '杀掉程序'"
            }
            echo "===================运行当前程序==================="
	    	sh "/usr/bin/sshpass -p '$sshPassWord' ssh -o StrictHostKeyChecking=no -p $sshPort $sshUserName@$sshIp nohup $jenkinsHomeMain/$javaPath -jar $jenkinsHomeMain/$environment/$itemName/$jarNameMv --spring.profiles.active=$environment \\> $jenkinsHomeMain/$environment/$itemName/$itemName\\.log &"
            echo "====================运行成功============================="  
            echo "===============删除无用拉取的文件========================"
            sh "rm -rf $jenkinsHome/workspace/$itemName/*"
		}
		    
    }
}

5.docker 制作镜像

// 制作docker镜像
docker commit -a "gch" -m "create a new docker image by gch"  3acd143824d8 gch-docker-jenkins:v1.0
// 打成so包
docker save -o gch-docker-jenkins-v1.0.tar gch-docker-jenkins:v1.0
// 加载so包成docker镜像
docker load -i gch-docker-jenkins-v1.0.tar