流水线总体语法
指定node部分
stages阶段
post定义
paramters参数
trigger触发器
tool 语法
input 语法
when 语法
parallel 语法
sharelibary共享库使用
1.jenkins先安装sharelibary插件
2.共享库项目有自己特定的目录组织结构
vars目录和resource目录可以不要,但是src目录是必须要存在的
常用的DSL语法
1.读取json对象
2.读取jenkins中的凭据信息
3.checkout
4.publishHTML
5.input
6.httpRequest
1.需要首先安装jenkins http request插件
7.email 发邮件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
pipeline { agent any tools { // Install the Maven version configured as "M3" and add it to the path. maven "M2" } stages { stage('GetCode'){ steps { // Get some code from a GitHub repository git credentialsId: 'yxgitlab', url: 'https://1920.111:8090/tf-as/h5-new.git' // To run Maven on a Windows agent, use // bat "mvn -Dmaven.test.failure.ignore=true clean package" } post { // If Maven was able to run the tests, even if some of the test // failed, record the test results and archive the jar file. success { print("getCode success") } } } stage("Build"){ steps { script { sh "cd /var/lib/jenkins/workspace/sdc-h5" sh "npm config set strict-ssl false" sh "npm install --registry=https://registry.npm.taobao.org" sh "npm install node-sass --unsafe-perm --save-dev --registry=https://registry.npm.taobao.org" sh "npm run build:sit" } } } stage("Deploy"){ steps { script { sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/sdc-h5/dist/ dest=/home/admin/runtime/nginx/html/'" } } post { // If Maven was able to run the tests, even if some of the test // failed, record the test results and archive the jar file. success { print("Deploy Success") emailext ( subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", body: """构建成功""", to: "cfgitlab_an@163.com,724627@qq.com", from: "cfgiab_in@163.com" ) } failure { emailext 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> 本邮件由系统自动发出,无需回复!<br/> 各位同事,大家好,以下为${PROJECT_NAME }项目构建信息</br> <td><font color="#CC0000">构建结果 - ${BUILD_STATUS}</font></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>触发原因: ${CAUSE}</li> <li>构建状态: ${BUILD_STATUS}</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> <h4><font color="#0B610B">失败用例</font></h4> <hr size="2" width="100%" /> $FAILED_TESTS<br/> <h4><font color="#0B610B">最近提交(#$SVN_REVISION)</font></h4> <hr size="2" width="100%" /> <ul> ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"} </ul> 详细提交: <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a><br/> </td> </tr> </table> </body> </html>''', subject: '构建通知:${BUILD_STATUS} - ${PROJECT_NAME} - Build # ${BUILD_NUMBER} !', to: '12727@qq.com', from: "cfgi_admin@163.com" } } } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
stage("Deploy"){ steps { script { sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/sdc-h5/dist/ dest=/home/admin/runtime/nginx/html/'" } } post { // If Maven was able to run the tests, even if some of the test // failed, record the test results and archive the jar file. success { print("Deploy Success") } always { emailext 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> 本邮件由系统自动发出,无需回复!<br/> 各位同事,大家好,以下为${PROJECT_NAME }项目构建信息</br> <td><font color="#CC0000">构建结果 - ${BUILD_STATUS}</font></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>触发原因: ${CAUSE}</li> <li>构建状态: ${BUILD_STATUS}</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> <h4><font color="#0B610B">失败用例</font></h4> <hr size="2" width="100%" /> $FAILED_TESTS<br/> <h4><font color="#0B610B">最近提交(#$SVN_REVISION)</font></h4> <hr size="2" width="100%" /> <ul> ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"} </ul> 详细提交: <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a><br/> </td> </tr> </table> </body> </html>''', subject: '构建通知:${BUILD_STATUS}-${PROJECT_NAME}-Build # ${BUILD_NUMBER} !', to: '1247@qq.com', from: "cadmin@163.com" } failure { print("Deploy Fail") } } }
8.jenkins流水线语法自动生成