8.Jenkins进阶之流水线pipeline基础使用实践(1)
目录一览:
0x01 基础实践
(1) Maven 构建之 Pipeline Script
(2) Maven 构建之 Pipeline Script from SCM
(3) Jenkins pipeline 之 邮件(Email)发信管理
WeiyiGeek Blog - 为了能到远方,脚下的每一步都不能少。
原文地址: https://mp.weixin.qq.com/s/RES8eg0ljzdrQ_ls_hHWjw
Tips : 本文章来源 Blog 站点或者 WeiyiGeek
公众账号 (技术交流、友链交换请邮我哟
),
- 微信公众号-WeiyiGeek # 精华文章发布地址(及时发布)
- 首页-https://weiyigeek.top # 国内有时访问较慢(及时更新)
- 博客-https://blog.weiyigeek.top # 国内访问快但(更新不及时)
实现效果:
0x01 基础实践
(1) Maven 构建之 Pipeline Script
描述:此处重新不在累述新建流水线任务(maven-pipeline-helloword
)而是直接进行配置测试等关键项;
流程:代码拉取 -> 代码检测 -> 代码构建 -> 代码部署 -> 消息通知
Step 1. Dashboard -> maven-pipeline-helloword -> 流水线项目配置 (名称|丢弃旧的构建|参数化构建过程(Git/名称))
# Git 参数
名称: git_tags
描述: Project_Release
参数类型: 标签
默认值: origin/master
排序方式: DESCENDING SMART
----------------------------
# 选项 参数
名称: deploy_option
选项:
deploy
rollback
redeploy
描述: 部署&回退&重部署
Step 2.流水线 Pipeline script 编写
pipeline {
agent any
stages {
stage ("代码获取") {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${git_tags}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'b4c8b4e9-2777-44a1-a1ed-e9dc21d37f4f', url: 'git@gitlab.weiyigeek.top:ci-cd/java-maven.git']]])
}
} stage ("代码质检") {
steps {
sh label: 'sonar', returnStatus: true, script: '''/usr/local/bin/sonar-scanner -Dsonar.projectName=${JOB_NAME} -Dsonar.projectKey=Hello-World -Dsonar.sources=.'''
}
} stage ("项目构建") {
steps {
// 此处实际上不用执行cd命令
sh label: 'build', script: 'cd /var/lib/jenkins/workspace/maven-pipeline-helloword/ && /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true '
}
} stage ("项目部署") {
steps {
// 脚本为前面一章的部署脚本(两种方式)
sh label: 'deploy', script: '/tmp/script/maven-jenkins-ci-script.sh'
// sh "/tmp/script/maven-jenkins-ci-script.sh"
}
}
} // 消息通知: POST阶段当所有任务执行后触发
post {
// 企业微信
always {
qyWechatNotification aboutSend: true, failNotify: true, failSend: true, mentionedId: 'ALL', mentionedMobile: '', startBuild: true, successSend: true, unstableSend: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c222f3fc-f645-440a-ad24-0ce8d9626fa0'
} // 钉钉
success {
//当此Pipeline成功时打印消息(注意此处的robot为您在Jenkins全局配置中设置钉钉的id值)
echo 'success'
dingtalk (
robot: 'weiyigeek-1000',
type: 'LINK',
title: 'Jenkins 持续集成 - 任务名称 ${JOB_NAME}',
text: [
'项目构建成功',
'Pipeline 流水线测试'
],
messageUrl: 'http://www.weiyigeek.top',
picUrl: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2319834554,1372531032&fm=26&gp=0.jpg',
atAll: true
)
}
}
}
Step 3.Pipeline maven-pipeline-helloword 进行选项参数构建 (Build with Parameters)-> 部署 v1.8 版本
Step 4.查看部署结果与信息通知
(2) Maven 构建之 Pipeline Script from SCM
描述: 我也可以将上面流水线的脚本放在我们的代码项目之中,在流水线拉取项目时候便会自动按照项目中的Jenkinsfile
文件内容进行执行对于操作
Step 1.修改项目首页文件以及在项目根添加Jenkinsfile文件(内容将取消第一阶段的代码拉取),例如:
# (1) E:\EclipseProject\hello-world\src\main\webapp\index.jsp
Maven - Hello World - v1.10 - Pipeline script for SCM
# (2) 项目信息
/e/EclipseProject/hello-world (master)
$ ls
Jenkinsfile pom.xml src/ target/ # (3) Jenkinsfile :注意内容将取消第一阶段的代码拉取
pipeline {
agent any
stages { stage ("代码质检") {
steps {
sh label: 'sonar', returnStatus: true, script: '''/usr/local/bin/sonar-scanner -Dsonar.projectName=${JOB_NAME} -Dsonar.projectKey=Hello-World -Dsonar.sources=.'''
}
} stage ("项目构建") {
steps {
sh label: 'build', script: 'cd /var/lib/jenkins/workspace/maven-pipeline-helloword/ && /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true '
}
} stage ("项目部署") {
steps {
// 脚本为前面一章的部署脚本
sh label: 'deploy', script: '/tmp/script/maven-jenkins-ci-script.sh'
}
}
} // 消息通知: POST阶段当所有任务执行后触发
post {
// 企业微信
always {
qyWechatNotification aboutSend: true, failNotify: true, failSend: true, mentionedId: 'ALL', mentionedMobile: '', startBuild: true, successSend: true, unstableSend: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c222f3fc-f645-440a-ad24-0ce8d9626fa0'
} // 钉钉
success {
//当此Pipeline成功时打印消息 (注意此处的robot为您在Jenkins全局配置中设置钉钉的id值)
echo 'success'
dingtalk (
robot: 'weiyigeek-1000',
type: 'LINK',
title: 'Jenkins 持续集成 - 任务名称 ${JOB_NAME}',
text: [
'项目构建成功',
'Pipeline 流水线测试'
],
messageUrl: 'http://www.weiyigeek.top',
picUrl: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2319834554,1372531032&fm=26&gp=0.jpg',
atAll: true
)
}
}
}
Step 2.上传修改的项目到Gitlab内部私有仓库中
$ git add .
$ git commit -m "v1.10 pipeline script for SCM"
# [master 3f9b6e8] v1.10 pipeline
# 2 files changed, 26 insertions(+), 1 deletion(-)
# create mode 100644 Jenkinsfile
$ git push
# To http://gitlab.weiyigeek.top/ci-cd/java-maven.git
# 0f50b10..3f9b6e8 master -> master
$ git tag -a "v1.10" -m "v1.10 Pipelinescript for SCM "
$ git push origin v1.10
# To http://gitlab.weiyigeek.top/ci-cd/java-maven.git
# * [new tag] v1.10 -> v1.10
Step 3.任务项目流水线设置 -> 选择 Pipeline script from SCM -> git -> 输入 Repository URL 和 Credentials -> 指定分支
Branches to build
(以及Jenkinsfile 拉取的文件名实现自动构建集成)
Step 4.项目构建参数输入 -> v1.10 | deploy -> 进行构建 -> 查看流水线
Step 5.查看部署结果
http://10.10.107.202:30089/
结果正常;
Maven - Hello World - v1.10 - Pipeline script for SCM 访问时间: Tue Jan 26 2021 14:54:35 GMT+0800 (中国标准时间) Server : Apache Tomcat/8.5.61 | 10.244.1.217 Client : 10.244.0.0 | 10.244.0.0 Document_Root : /usr/local/tomcat/webapps/ROOT/ URL : 10.10.107.202/index.jsp
(3) Jenkins pipeline 之 邮件(Email)发信管理
描述: 如果利用 Freestyle 的原生Job我们可以很好的进行Job邮件发信,而在与 Jenkins 流水线中需要Extended E-mail Notification
的方式进行实现(此处只是简单说明建议采用钉钉或者企业微信的方式更加及时方便);
下面提供两种格式进行参考:
(1) Scripted Pipeline
node ("master"){
parameters {string(name:'Jenkins',defaultValue:'Hello',description:'How should I greet the world')}
echo "${params.nane} 你好!"
// gitlab 流水线通知
gitlabCommitStatus {
stage('第1步拉代码'){
echo "拉代码"
git credentialsId: '03fd8295-c536-4871-9794-1c37394676e0', url: 'git@gitlab.corp.XXX.com:wangxu/ops.git'
}
stage('第2步编译'){
echo "编译"
// sh "source /etc/profile && /usr/local/maven/bin/mvn clean compile"
}
stage('第3步打包'){
echo "打包,有一个mail模块是系统级别的,需要sudo"
// sh "sudo /usr/local/maven/bin/mvn package"
// echo "完成后 修改一下权限,否则下一次麻烦"
// sh "sudo chown -R jenkins: ."
// sh "find -name '*SNAPSHOT.jar' "
}
stage('第四步单元测试'){
echo "单元测试"
//sh "sudo /usr/local/maven/bin/mvn test"
}
stage("放到下载服务器上"){
echo "发送文件"
// sh "sudo cp ./account-email/target/account-email-1.0.0-SNAPSHOT.jar /home/admin/webserver/html/download && sudo chown -R admin: /home/admin/webserver/html/download"
}
}
stage("发送邮件"){
def git_url = 'git@gitlab.corp.XXX.com:wangxu/ops.git'
def branch = 'dev'
def username = 'Jenkins'
echo "Hello Mr.${username}"
echo "GIT路径: ${git_url}" echo "发送邮件"
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>
<td>(本邮件由程序自动下发,请勿回复!)</td>
</tr>
<tr>
<td>
<h2><font color="#FF0000">构建结果 - ${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><a href="${PROJECT_URL}">${PROJECT_URL}</a>
<td>
<ul>
<li>项目名称:${PROJECT_NAME}</li>
<li>GIT路径:<a href="git@gitlab.corp.XXX.com:wangxu/ops.git">git@gitlab.corp.XXX.com:wangxu/ops.git</a></li>
<li>GIT分支:master</li>
<li>构建编号:${BUILD_NUMBER}</li>
<li>触发原因:${CAUSE}</li>
<li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
</ul>
</td>
</tr>
<tr>
<td>
<b><font color="#0B610B">变更信息:</font></b>
<hr size="2" width="100%" align="center" />
</td>
</tr>
<tr>
<td>
<ul>
<li>上次构建成功后变化 : ${CHANGES_SINCE_LAST_SUCCESS}</a></li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>上次构建不稳定后变化 : ${CHANGES_SINCE_LAST_UNSTABLE}</a></li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>变更集:${JELLY_SCRIPT,template="html"}</a></li>
</ul>
</td>
</tr>
<!--
<tr>
<td>
<b><font color="#0B610B">Failed Test Results</font></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,escapeHtml=true}</textarea>
</td>
</tr>-->
<hr size="2" width="100%" align="center" /> </table> </body>
</html>
""", subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: 'master@weiyigeek.top'
}
}
(2) Declarative Pipeline
1 pipeline{
2 agent{label 'master'}
3 environment {
4 git_url = 'git@gitlab.corp.XXX.com:wangxu/ops.git'
5 git_key = '03fd8295-c536-4871-9794-1c37394676e0'
6 git_branch = 'master'
7 }
8
9 stages {
10 stage('下载代码') {
11 steps {
12 git branch: "${git_branch}",credentialsId: "${git_key}", url: "${env.git_url}"
13 }
14 }
15 }
16
17 post {
18 //always部分 pipeline运行结果为任何状态都运行
19 always{
20 script{
21 emailext body:
22 ''' <!DOCTYPE html>
23 <html>
24 <head>
25 <meta charset="UTF-8">
26 <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
27 </head>
28 <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
29 <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
30 <tr>
31 <td>(本邮件由程序自动下发,请勿回复!)</td>
32 </tr>
33 <tr>
34 <td>
35 <h2><font color="#FF0000">构建结果 - ${BUILD_STATUS}</font></h2>
36 </td>
37 </tr>
38 <tr>
39 <td><br />
40 <b><font color="#0B610B">构建信息</font></b>
41 <hr size="2" width="100%" align="center" />
42 </td>
43 </tr>
44 <tr><a href="${PROJECT_URL}">${PROJECT_URL}</a>
45 <td>
46 <ul>
47 <li>项目名称:${PROJECT_NAME}</li>
48 <li>GIT路径:<a href="git@gitlab.corp.XXX.com:wangxu/ops.git">git@gitlab.corp.XXX.com:wangxu/ops.git</a></li>
49 <li>GIT分支:master</li>
50 <li>构建编号:${BUILD_NUMBER}</li>
51 <li>触发原因:${CAUSE}</li>
52 <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
53 </ul>
54 </td>
55 </tr>
56 <tr>
57 <td>
58 <b><font color="#0B610B">变更信息:</font></b>
59 <hr size="2" width="100%" align="center" />
60 </td>
61 </tr>
62 <tr>
63 <td>
64 <ul>
65 <li>上次构建成功后变化 : ${CHANGES_SINCE_LAST_SUCCESS}</a></li>
66 </ul>
67 </td>
68 </tr>
69 <tr>
70 <td>
71 <ul>
72 <li>上次构建不稳定后变化 : ${CHANGES_SINCE_LAST_UNSTABLE}</a></li>
73 </ul>
74 </td>
75 </tr>
76 <tr>
77 <td>
78 <ul>
79 <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
80 </ul>
81 </td>
82 </tr>
83 <tr>
84 <td>
85 <ul>
86 <li>变更集:${JELLY_SCRIPT,template="html"}</a></li>
87 </ul>
88 </td>
89 </tr>
90 <!--
91 <tr>
92 <td>
93 <b><font color="#0B610B">Failed Test Results</font></b>
94 <hr size="2" width="100%" align="center" />
95 </td>
96 </tr>
97 <tr>
98 <td>
99 <pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
100 <br />
101 </td>
102 </tr>
103
104 <tr>
105 <td>
106 <b><font color="#0B610B">构建日志 (最后 100行):</font></b>
107 <hr size="2" width="100%" align="center" />
108 </td>
109 </tr>-->
110 <!-- <tr>
111 <td>Test Logs (if test has ran): <a
112 href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a>
113 <br />
114 <br />
115 </td>
116 </tr> -->
117 <!--
118 <tr>
119 <td>
120 <textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=100,escapeHtml=true}</textarea>
121 </td>
122 </tr>-->
123 <hr size="2" width="100%" align="center" />
124
125 </table>
126
127
128 </body>
129 </html>
130 ''',
131 subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: 'master@weiyigeek.top'
132 }
133 }
134
135 success {
136 //当此Pipeline成功时打印消息
137 echo 'success'
138 }
139 failure {
140 //当此Pipeline失败时打印消息
141 echo 'failure'
142 }
143 unstable {
144 //当此Pipeline 为不稳定时打印消息
145 echo 'unstable'
146 }
147 aborted {
148 //当此Pipeline 终止时打印消息
149 echo 'aborted'
150 }
151 changed {
152 //当pipeline的状态与上一次build状态不同时打印消息
153 echo 'changed'
154 }
155 }
156 }
WeiyiGeek Blog - 为了能到远方,脚下的每一步都不能少。
Tips : 本文章来源 Blog 站点或者 WeiyiGeek
公众账号 (友链交换请邮我哟
):
微信公众号-WeiyiGeek # 精华文章发布地址(及时发布)
https://weiyigeek.top 国内有时访问较慢
https://weiyigeek.gitee.io
Tips: 更多学习笔记文章请关注
WeiyiGeek
公众账号
【微信公众号关注(点击)】
【邮箱联系: Master#weiyigeek.top】
8.Jenkins进阶之流水线pipeline基础使用实践(1)的更多相关文章
- 9.Jenkins进阶之流水线pipeline基础使用实践(2)
目录一览: 0x01 基础实践 0x02 进阶实践 (1) Sonarqube 代码质量检测之 Pipeline Script from SCM (2) Gitlab 自动触发构建之 Pipeline ...
- 6.Jenkins进阶之流水线pipeline语法入门学习(1)
目录一览: 0x00 前言简述 Pipeline 介绍 Pipeline 基础知识 Pipeline 扩展共享库 BlueOcean 介绍 0x01 Pipeline Syntax (0) Groov ...
- 7.Jenkins进阶之流水线pipeline语法入门学习(2)
目录一览: (2) Declarative Pipeline Syntax 2.1) Sections - 章节 2.2) Directives - 指令 2.3) Sequential Stages ...
- jenkins中的流水线( pipeline)的理解(未完)
目录 一.理论概述 Jenkins流水线的发展历程 什么是Jenkins流水线 一.理论概述 pipeline是流水线的英文释义,文档中统一称为流水线 Jenkins流水线的发展历程 在Jenki ...
- jenkins中的流水线( pipeline)的理解(未完)
目录 一.理论概述 Jenkins流水线的发展历程 什么是Jenkins流水线 一.理论概述 pipeline是流水线的英文释义,文档中统一称为流水线 Jenkins流水线的发展历程 在Jenki ...
- jenkins的流水线pipeline+项目实验php
声明:实验环境使用Jenkins的应用与搭建的环境 新建一个流水线 pipeline脚本语法架构 node('slave节点名'){ def 变量 #def可以进行变量声明 stage('阶段名A') ...
- Jenkins流水线(pipeline)实战之:从部署到体验
关于Jenkins流水线(pipeline) Jenkins 流水线 (pipeline) 是一套插件,让Jenkins可以实现持续交付管道的落地和实施. 关于blueocean Blue Ocean ...
- GitLab集成Jenkins、Harborn构建pipeline流水线任务
一.计划 在jenkins中构建流水线任务时,从GitLab当中拉取代码,通过maven打包,然后构建dokcer镜像,并将镜像推送至harbor当中.Jenkins中含开发.测试.生产视图,开发人员 ...
- Pipeline流水线设计的最佳实践
谈到到DevOps,持续交付流水线是绕不开的一个话题,相对于其他实践,通过流水线来实现快速高质量的交付价值是相对能快速见效的,特别对于开发测试人员,能够获得实实在在的收益.很多文章介绍流水线,不管是j ...
随机推荐
- 阿里云镜像站DNS——Chrome配置方法
镜像下载.域名解析.时间同步请点击 阿里巴巴开源镜像站 DNS 简介 域名系统(服务)协议(DNS)是一种分布式网络目录服务,主要用于域名与 IP 地址的相互转换,以及控制因特网的电子邮件的发送. 阿 ...
- Python之VSCode
在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带的编辑器.由于本人用惯了宇宙第一IDE(Visual Studio),所以当Visual Studio C ...
- 4月17日 python学习总结 反射、object内置方法、元类
一.反射 下述四个函数是专门用来操作类与对象属性的,如何操作? 通过字符串来操作类与对象的属性,这种操作称为反射 class People: country="China" def ...
- OpenCV开发笔记(七十四):OpenCV3.4.1+ffmpeg3.4.8交叉编译移植到海思平台Hi35xx平台
前言 移植opencv到海思平台,opencv支持对视频进行解码,需要对应的ffmpeg支持. Ffmpeg的移植 Ffmpeg的移植请参考之前的文章:<FFmpeg开发笔记(十): ...
- git tag、gitignore和git撤销提交
前言 最近在git的使用过程中遇到了一些新的问题,所以写下来方便自己回忆. git tag 打标签 git tag -a v1.00 -m "注释" git tag 打标签命令 - ...
- vue渐进式?
小到可以只使用核心功能,比如单文件组件作为一部分嵌入:大到使用整个工程,vue init webpack my-project来构建项目:VUE的核心库及其生态系统也可以满足你的各式需求(core+v ...
- Java如何跳出当前的多重嵌套循环?
在Java中,要想跳出多重循环,可以在外面的循环语句前定义一个标号,然后在里层循环体的代码中使用带有标号的break 语句,即可跳出外层循环.例如, outer: for(int i=0;i<1 ...
- SpringBoot和SpringCloud的区别?
SpringBoot专注于快速方便的开发单个个体微服务. SpringCloud是关注全局的微服务协调整理治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来, 为各个微服务之间提供 ...
- idea-spring-boot打包jar/var
下面的插件配置的里面需要加上具体的main类 <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- kafka客户端打印日志
kafka 0.10.0 java客户端使用slf4j作为日志门面,需要我们加入具体的日志实现依赖才能打印日志,日志框架:http://www.cnblogs.com/set-cookie/p/883 ...