1. 通过jenkins pipeline 中 groovy  语言的一些方法,编写统一的java pipeline  打包段。


  2. stage('up') {
        steps {            
              script {
                def sub_build_path = ''                    
                for (val in BUILD_SCRIPTS.split(';|&&')){
                    if (val.contains('cd') && val.split(' ').size() >= 2){        
                        for ( sub_val in val.split(' ')){
                            if (sub_val != '' && ! sub_val.contains('cd') ){                            
                                sub_build_path +=sub_val
                                if (! sub_build_path.endsWith('/')){
                                  sub_build_path +='/'
                                }
                            }
                        }
                    }
                }            
                target = sh returnStdout: true,script: "find $sub_build_path -type d -name target"            
                if (target != '' ){
                   sub_build_path +='target/'
                }else{
                   // for grandle
                   sub_build_path +='build/libs/'    
                }
                rvfind = sh returnStdout: true,script: "find $sub_build_path -name '*.war'"
                if (rvfind == ''){    
                     outfile = "${sourcefile}.jar"                                    
                     rvfind = sh returnStdout: true, script: "ls -l $sub_build_path| awk '{print \$NF}' | egrep '*.jar\$'  | awk -v ORS=' ' '{print \$0}' "                             
                }
                rvfind = rvfind.trim()  
                sh "if test ! -d ${sourcefile};then mkdir ${sourcefile};else rm -rf ./${sourcefile}/*;fi;"
                if ( outfile.contains('war')){                
                    sh """
                        unzip -q  -d ${sourcefile} ${rvfind}                        
                       """
                }else{
                    dir(sourcefile){
                        for (ofile in rvfind.split(' ')){    
                           if (ofile != ''){    
                             pathfile = '../' + sub_build_path + ofile                       
                             sh """
                              /bin/cp -f $pathfile .;
                             """
                          }
                        }
                    }                
                }                                              
                sh """
                    zip -qry $zipfile ${sourcefile}
                """               
              }
              stash includes: "$zipfile", name: "$zipfile"
        }
    }
  3.     stage('service oper'){
          //agent {lable 'node_label'}
          steps{
            unstash "$zipfile"
            sh """ 
                sudo su -c "PATH=$PATH;${stop_script} && echo 'STOP Proceess success' ||echo 'STOP Process failure '"
            """
            script{
                    if (! unzip_path.endsWith('/')){
                            unzip_path += '/'
                    }
                    if (unzip_path.contains('ROOT')) {
                      sh """
                       sudo su -c "cd ${unzip_path};rm -rf ./*;unzip -oq ${workspace}/$zipfile;mv ${sourcefile}/* .;rm -rf ./${sourcefile}"                 
                       """
                    }else if (outfile.endsWith('jar')){                   
                        for (ofile in rvfind.split(' ')){    
                           sh "if test -f ${unzip_path}$ofile;then rm -f ${unzip_path}$ofile;fi"
                        }
                        sh """
                           sudo su -c "cd ${unzip_path};unzip -oq ${workspace}/$zipfile;mv ${sourcefile}/* .;rm -rf ./${sourcefile}"                 
                           """
                    }else{                    
                        sh """
                        sudo su -c "cd ${unzip_path};rm -rf ./$sourcefile;unzip -oq ${workspace}/$zipfile"                    
                        """
                    }   
            }
             sh """
                sudo su -c "PATH=$PATH;${start_script}"            
             """
          }
        }