pipeline基本框架思維


基本框架

 

 

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                println "Build" 
            }
        }
        stage('Test') { 
            steps {
                println "Test" 
            }
        }
        stage('Deploy') { 
            steps {
                println "Deploy" 
            }
        }
    }
}

 

 

 

//定義和引入日期函數 
def riqi=''
import java.text.SimpleDateFormat

//寫日期函數便於調用
script{
    def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
    def date = new Date()
    riqi=dateFormat.format(date)
    }

pipeline {

    //環境
    environment {
    
    // 郵件通知
    emailUser = "huzy@southgnss.com"
    mimeType = "text/html"

    
    //節點JAVA_HOME
    JAVA_HOME = "C:\\Program Files\\Java\\jdk1.8.0_211"
    
    riqi = "${riqi}"
    
    
}

    //節點機器的標簽或者名字
    agent {
    label 'win_172.16.55.40'

} 

    //全局超時的等待  單位小時
    options {
        timeout(time: 1, unit: 'HOURS') 
    }
    
    
    stages {
        //拉取代碼
        stage('1-Code Pull') { 
            steps {
                echo "1-Code Pull" 
                //從svn拉代碼
                checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: 'b16957d4-8661-491c-9d88-00d297ada94c', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'http://172.16.55.19/test/AlphaTestDemo']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
  }
        }
        
        //單元測試
        stage('2-Unit Test') { 
            steps {
                echo "2-Unit Test" 
                
                //每次構建先清理垃圾
                bat 'gradlew clean'
                //再運行測試
                bat 'gradlew test'
                
            }
        }
        
        
        //代碼構建
        stage('3-Build Package') { 
            steps {
                echo "3-Build Package" 
                
                //構建apk的命令
                bat  '''
                gradlew assemble
                '''
                
                //正則匹配存檔的文件
                archiveArtifacts artifacts: '**/*.apk,**/index.html', caseSensitive: false, followSymlinks: false
                
            }
        }
    
        //發送構建郵件
        stage('4-發送構建郵件') { 
            steps {
                echo "4-發送構建郵件"
                
                                                                           emailext attachLog: true, attachmentsPattern: '**/*.apk,**/index.html', body: '''<!DOCTYPE html>
                                                                            <html>
                                                                            <head>
                                                                            <meta charset="UTF-8">
                                                                            <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次構建日志</title>
                                                                            </head>
                                                                            
                                                                            <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
                                                                                offset="0">
                                                                                <table width="95%" cellpadding="0" cellspacing="0"
                                                                                    style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                                                                                    <tr>
                                                                                        <td>(本郵件是程序自動下發的,請勿回復!)</td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td><h2>
                                                                                                <font color="#0000FF">構建結果 - ${BUILD_STATUS}</font>
                                                                                            </h2></td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td><br />
                                                                                        <b><font color="#0B610B">構建信息</font></b>
                                                                                        <hr size="2" width="100%" align="center" /></td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td>
                                                                                            <ul>
                                                                                                <li>項目名稱&nbsp;:&nbsp;${PROJECT_NAME}</li>
                                                                                                <li>構建編號&nbsp;:&nbsp;第${BUILD_NUMBER}次構建</li>
                                                                                                <li>SVN&nbsp;版本:&nbsp;${SVN_REVISION}</li>
                                                                                                <li>觸發原因:&nbsp;${CAUSE}</li>
                                                                                                <li>構建日志:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                                                                                                <li>構建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
                                                                                                <li>工作目錄&nbsp;:&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                                                                                                <li>項目&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                                                                                            </ul>
                                                                                        </td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td><b><font color="#0B610B">Changes Since Last
                                                                                                    Successful Build:</font></b>
                                                                                        <hr size="2" width="100%" align="center" /></td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td>
                                                                                            <ul>
                                                                                                <li>歷史變更記錄 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                                                                                            </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"}
                                                                                        </td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td><b>Failed Test Results</b>
                                                                                        <hr size="2" width="100%" align="center" /></td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td><pre
                                                                                                style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
                                                                                            <br /></td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td><b><font color="#0B610B">構建日志 (最后 100行):</font></b>
                                                                                        <hr size="2" width="100%" align="center" /></td>
                                                                                    </tr>
                                                                                    <!-- <tr>
                                                                                        <td>Test Logs (if test has ran): <a
                                                                                            href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a>
                                                                                            <br />
                                                                                        <br />
                                                                                        </td>
                                                                                    </tr> -->
                                                                                    <tr>
                                                                                        <td><textarea cols="80" rows="30" readonly="readonly"
                                                                                                style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
                                                                                        </td>
                                                                                    </tr>
                                                                                </table>
                                                                            </body>
                                                                            </html>''', mimeType: "${mimeType}", recipientProviders: [culprits(), developers(), requestor(), brokenBuildSuspects(), brokenTestsSuspects(), upstreamDevelopers()], replyTo: '''${emailUser}''', subject: '阿爾法項目構建:${BUILD_STATUS} - ${PROJECT_NAME} - Build # ${BUILD_NUMBER} !', to: """${emailUser}"""
                                                                                           
 
            }
        }
    
    
        //詢問是否上傳到svn
        stage('5-是否上傳到公共svn?') { 
            steps {
                // 超時等待10分鍾
                timeout(activity: true, time: 1) {
                
                

                input "5-繼續上傳到svn" 
                echo "你選擇了是,並繼續上傳到公共SVN中"
                
                //傳svn的svn的腳步
                
                
                bat '''
                CD C:\\jenkins_data\\workspace\\file\\MyPipeline@2\\app\\build\\reports\\tests\\testReleaseUnitTest & mkdir uploadUnitTest
                copy index.html C:\\jenkins_data\\workspace\\file\\MyPipeline@2\\app\\build\\reports\\tests\\testReleaseUnitTest\\uploadUnitTest\\index-%riqi%.html
                '''
                echo "單元測試報告:index-${riqi}.html"
                
                bat '''
                CD C:\\jenkins_data\\workspace\\file\\MyPipeline@2\\app\\build\\outputs\\apk\\release & mkdir uploadApp
                copy app-release.apk C:\\jenkins_data\\workspace\\file\\MyPipeline@2\\app\\build\\outputs\\apk\\release\\uploadApp\\release-%riqi%.apk
                '''
            
                echo "APK文件:release-${riqi}.apk"
                
                
                bat '''
                svn import C:\\jenkins_data\\workspace\\file\\MyPipeline@2\\app\\build\\outputs\\apk\\release\\uploadApp http://172.16.55.19/test/adds -m "1"
                '''
                
                bat '''
                svn import C:\\jenkins_data\\workspace\\file\\MyPipeline@2\\app\\build\\reports\\tests\\testReleaseUnitTest\\uploadUnitTest http://172.16.55.19/test/adds -m "1"
                '''
                
            }
            }
        }
        

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM