4.2.1、创建分支
配置要求:只要是push操作就会自动触发Jenkins的构建操作
4.2.2、安装插件
Generic Webhook Trigger
4.2.3、jenkins配置
simple-java-maven-app-master.zip
srcUrl
http://39.105.58.61:9999/root/demo-maven-service.git
branchName
master
test01
buildType
mvn
buildShell
clean package
http://39.105.58.61:9999/root/jenkinslib.git
凭据认证
*/master
Jenkinsfile
4.2.4、勾选配置触发器
http://JENKINS_URL/generic-webhook-trigger/invoke
runOpts
gitlab-demo-maven-service
4.2.5、配置gitlab
4.2.5.1、报错
http://47.93.249.164:8888/generic-webhook-trigger/invoke?token=gitlab-demo-maven-service&runOpts=GitlabPush
Url is blocked: Requests to the local network are not allowed
4.2.5.2、报错解决方式
Admin area => Settings => Network
勾选下面的选项
4.2.5.3、在次配置添加jenkins
http://47.93.249.164:8888/generic-webhook-trigger/invoke?token=gitlab-demo-maven-service&runOpts=GitlabPush
4.2.5.4、测试触发
4.2.5.5、测试验证jenkins是否已经自动触发
已自动触发构建操作
4.2.5.6、分析提交的日志
保留该报文有助于后续获取参数。如4.3获取的user_username参数
{
"object_kind":"push",
"event_name":"push",
"before":"ef9d7365666d007320a03493af057886b8cd1542",
"after":"0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
"ref":"refs/heads/master",
"checkout_sha":"0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
"message":null,
"user_id":1,
"user_name":"Administrator",
"user_username":"root",
"user_email":"",
"user_avatar":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?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":"0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
"message":"Delete deliver.sh",
"title":"Delete deliver.sh",
"timestamp":"2021-02-18T22:11:13+08:00",
"url":"http://39.105.58.61:9999/root/demo-maven-service/-/commit/0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
"author":{
"name":"Administrator",
"email":"admin@example.com"
},
"added":[
],
"modified":[
],
"removed":[
"jenkins/scripts/deliver.sh"
]
},
{
"id":"5803768fca4b4b302b3c4be5bc43e7ff6c7f78ca",
"message":"Delete Jenkinsfile",
"title":"Delete Jenkinsfile",
"timestamp":"2021-02-18T22:11:05+08:00",
"url":"http://39.105.58.61:9999/root/demo-maven-service/-/commit/5803768fca4b4b302b3c4be5bc43e7ff6c7f78ca",
"author":{
"name":"Administrator",
"email":"admin@example.com"
},
"added":[
],
"modified":[
],
"removed":[
"jenkins/Jenkinsfile"
]
},
{
"id":"ef9d7365666d007320a03493af057886b8cd1542",
"message":"Update Jenkinsfile",
"title":"Update Jenkinsfile",
"timestamp":"2021-02-18T21:50:09+08:00",
"url":"http://39.105.58.61:9999/root/demo-maven-service/-/commit/ef9d7365666d007320a03493af057886b8cd1542",
"author":{
"name":"Administrator",
"email":"admin@example.com"
},
"added":[
],
"modified":[
],
"removed":[
]
}
],
"total_commits_count":3,
"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.2.5.7、共享库
src/org/devops/build.groovy
package org.devops
//构建类型
//传入参数构建类型,构建命令
def Build(buildType,buildShell){
//页面传入的buildType匹配mvn、ant、gradle、npm
//m2、ant、Gradle、NPM为Global Tool Configuration配置变量名称
def buildTools=["mvn":"m2","ant":"ant","gradle":"Gradle","npm":"NPM"]
println("当前选择的构建类型为 ${buildType}")
buildHome = tool buildTools[buildType]
sh "${buildHome}/bin/${buildType} ${buildShell}"
}
4.3.5.7、jenkinsfile
#!groovy
//引用Library库
@Library('jenkinslib@master') _
//引用构建共享库
def build = new org.devops.build()
//引入发布共享库
def deploy = new org.devops.deploy()
//引入参数
String srcUrl = "${env.srcUrl}" //引用参数
String branchName = "${env.branchName}" //引用参数
String buildType = "${env.buildType}" //引用参数
String buildShell = "${env.buildShell}" //引用参数
pipeline {
agent {
node {
label "master" //构建主机
}
}
stages {
//下载代码
stage("CheckOut") {
steps {
script {
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: "${srcUrl}"]]])
}
}
}
stage("Build") {
steps {
script {
//引入构建共享库内函数
build.Build(buildType,buildShell)
//引用发布共享库内函数
}
}
}
}
}