附:
流程图
邮箱
构建push报文
{
"object_kind":"push",
"event_name":"push",
"before":"d4022a2e922dcbef2e6a7b9a45469084109af9a0",
"after":"7c6afe6956cd586fbc43509fa8858bc96bbf98f6",
"ref":"refs/heads/master",
"checkout_sha":"7c6afe6956cd586fbc43509fa8858bc96bbf98f6",
"message":null,
"user_id":1,
"user_name":"Administrator",
"user_username":"root",
"user_email":"liuhaoran@newland.com.cn",
"user_avatar":"https://www.gravatar.com/avatar/a76eabfb331c9452745754dfec08f402?s=80&d=identicon",
"project_id":2,
"project":{
"id":2,
"name":"demo-maven-service",
"description":"",
"web_url":"http://39.105.58.61:9999/root/demo-maven-service",
"avatar_url":null,
"git_ssh_url":"git@39.105.58.61:root/demo-maven-service.git",
"git_http_url":"http://39.105.58.61:9999/root/demo-maven-service.git",
"namespace":"Administrator",
"visibility_level":0,
"path_with_namespace":"root/demo-maven-service",
"default_branch":"master",
"ci_config_path":null,
"homepage":"http://39.105.58.61:9999/root/demo-maven-service",
"url":"git@39.105.58.61:root/demo-maven-service.git",
"ssh_url":"git@39.105.58.61:root/demo-maven-service.git",
"http_url":"http://39.105.58.61:9999/root/demo-maven-service.git"
},
"commits":[
{
"id":"7c6afe6956cd586fbc43509fa8858bc96bbf98f6",
"message":"Update pom.xml",
"title":"Update pom.xml",
"timestamp":"2021-02-24T14:23:24+08:00",
"url":"http://39.105.58.61:9999/root/demo-maven-service/-/commit/7c6afe6956cd586fbc43509fa8858bc96bbf98f6",
"author":{
"name":"Administrator",
"email":"liuhaoran@newland.com.cn"
},
"added":[
],
"modified":[
],
"removed":[
]
}
],
"total_commits_count":1,
"push_options":{
},
"repository":{
"name":"demo-maven-service",
"url":"git@39.105.58.61:root/demo-maven-service.git",
"description":"",
"homepage":"http://39.105.58.61:9999/root/demo-maven-service",
"git_http_url":"http://39.105.58.61:9999/root/demo-maven-service.git",
"git_ssh_url":"git@39.105.58.61:root/demo-maven-service.git",
"visibility_level":0
}
}
4.6.1、规划
1、Jenkins
项目名称:gitlab-demo-maven
2、gitlab
共享库名称:jenkinslib
jenkinsfile:jenkinslib/Jenkinsfile
构建共享库文件:jenkinslib/src/org/devops/build.groovy
邮件共享库名称:jenkinslib/src/org/devops/toemail.groovy
代码库名称:demo-maven-service
3、安装插件
Generic Webhook Trigger
Email Extension Template
Email Extension Plugin
4、新建gitlab凭证
5、gitlab新建开发用户,并具有看到上述两个项目的权限(先用root用户做实验)
6、开发人员提交低吗后Jenkins上会有构建人员描述,并给构建人员发送构建的结果通知
要求:push请求会自动触发构建操作。但是新建分支操作除外,构建完成后会给对应的开发人员发送邮件通知是否成功。
4.6.2、gitlab代码库
4.6.2.1、gitlab代码库
demo-maven-service-master.tar.gz
4.6.2.2、gitlab共享库
4.6.3、Jenkins全局工具配置
4.6.3.1、配置jDK
4.6.3.2、配置Git
4.6.3.3、配置Maven
4.6.4、Jenkins系统配置
Jenkins Location
全局属性
Global Pipeline Libraries
Extended E-mail Notification
smtp.qq.com
465
446693337@qq.com
授权码
勾选ssl
UTF-8
HTML(text/html)
4.6.5、Jenkins项目配置
4.6.5.1、Jenkins参数配置
参数化构建过程:为了给共享库传参
srcUrl
http://39.105.58.61:9999/root/demo-maven-service.git
branchName
master
test01
buildType
mvn
buildShell
clean package
4.6.5.2、Jenkins的Generic Webhook Trigger配置
4.6.5.2.1、Post content parameters 配置
地址记录
Is triggered by HTTP requests to http://JENKINS_URL/generic-webhook-trigger/invoke
获取分支名
branch
$.ref
获取提交代码人员的用户名
userName
$.user_username
获取git的仓库地址
ssh_url
$.project.ssh_url
过滤到创建分支的push请求(用来后面匹配过滤检验新建的分支不触发自动构建)
before
$.before
after
$.after
object_kind
$.object_kind
邮箱配置
user_email
$.user_email
4.6.52.2、Request parameters配置
runOpts
4.6.5.2.3、Token配置
使用项目名称作为token
gitlab-demo-maven
4.6.4.2.4、勾选此处配置
4.6.5.2.5、Optional filter配置
过滤新建分支触发
^push\s(?!0{40}).{40}\s(?!0{40}).{40}$
以push开头,且不等于40个0会被放开校验(也就是说新建分支的push请求会被校验住,其他的push请求会正常自动化触发构建)
$object_kind $after $before
参考链接
https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd/gitlab/gitlab-push-ignore-when-no-commits.feature
4.6.5.3、Jenkins的流水线配置
http://39.105.58.61:9999/root/jenkinslib.git
4.6.6、gitlab自动push配置
4.6.6.1、配置自动化push
http://47.93.249.164:8888/generic-webhook-trigger/invoke?token=gitlab-demo-maven&runOpts=GitlabPush
4.6.6.2、添加报错
Url is blocked: Requests to the local network are not allowed
解决方式
4.6.6.3、再次配置
4.6.6.4、测试
测试验证Jenkins是否已经自动触发构建
已经触发自动化构建,如果新建分支的话push报文中的before字段为40个0,会自动屏蔽。
4.6.7、gitlab配置邮箱
4.6.7.1、gitlab配置邮箱
添加该用户得邮箱,为了后期构建通知
第一张添加邮箱的图片为管理员添加的,第二张添加邮件的图片是用户自己登陆自己的账号完了自行进行添加的
4.6.7.2、测试邮箱是否发送
修改代码提交