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>项目名称 : ${PROJECT_NAME}</li> <li>构建编号 : 第${BUILD_NUMBER}次构建</li> <li>SVN 版本: ${SVN_REVISION}</li> <li>触发原因: ${CAUSE}</li> <li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li> <li>构建 Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li> <li>工作目录 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li> <li>项目 Url : <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=" %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" ''' } } } } }