4.2.1、创建分支

配置要求:只要是push操作就会自动触发Jenkins的构建操作

4.2、配置gitlab自动提交流水线 - 图1

4.2.2、安装插件

  1. Generic Webhook Trigger

4.2、配置gitlab自动提交流水线 - 图2

4.2.3、jenkins配置

4.2、配置gitlab自动提交流水线 - 图3simple-java-maven-app-master.zip

  1. srcUrl
  2. http://39.105.58.61:9999/root/demo-maven-service.git

4.2、配置gitlab自动提交流水线 - 图4

  1. branchName
  2. master
  3. test01

4.2、配置gitlab自动提交流水线 - 图5

  1. buildType
  2. mvn

4.2、配置gitlab自动提交流水线 - 图6

  1. buildShell
  2. clean package

4.2、配置gitlab自动提交流水线 - 图7

  1. http://39.105.58.61:9999/root/jenkinslib.git
  2. 凭据认证
  3. */master
  4. Jenkinsfile

4.2.4、勾选配置触发器

4.2、配置gitlab自动提交流水线 - 图8

4.2、配置gitlab自动提交流水线 - 图9

4.2、配置gitlab自动提交流水线 - 图10

  1. http://JENKINS_URL/generic-webhook-trigger/invoke
  2. runOpts
  3. gitlab-demo-maven-service

4.2.5、配置gitlab

4.2.5.1、报错

  1. http://47.93.249.164:8888/generic-webhook-trigger/invoke?token=gitlab-demo-maven-service&runOpts=GitlabPush

4.2、配置gitlab自动提交流水线 - 图11

  1. Url is blocked: Requests to the local network are not allowed

4.2.5.2、报错解决方式

  1. Admin area => Settings => Network
  2. 勾选下面的选项

4.2、配置gitlab自动提交流水线 - 图12

4.2.5.3、在次配置添加jenkins

  1. http://47.93.249.164:8888/generic-webhook-trigger/invoke?token=gitlab-demo-maven-service&runOpts=GitlabPush

4.2、配置gitlab自动提交流水线 - 图13

4.2.5.4、测试触发

4.2、配置gitlab自动提交流水线 - 图14

4.2.5.5、测试验证jenkins是否已经自动触发

  1. 已自动触发构建操作

4.2、配置gitlab自动提交流水线 - 图15

4.2.5.6、分析提交的日志

4.2、配置gitlab自动提交流水线 - 图16

保留该报文有助于后续获取参数。如4.3获取的user_username参数

  1. {
  2. "object_kind":"push",
  3. "event_name":"push",
  4. "before":"ef9d7365666d007320a03493af057886b8cd1542",
  5. "after":"0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
  6. "ref":"refs/heads/master",
  7. "checkout_sha":"0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
  8. "message":null,
  9. "user_id":1,
  10. "user_name":"Administrator",
  11. "user_username":"root",
  12. "user_email":"",
  13. "user_avatar":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  14. "project_id":2,
  15. "project":{
  16. "id":2,
  17. "name":"demo-maven-service",
  18. "description":"",
  19. "web_url":"http://39.105.58.61:9999/root/demo-maven-service",
  20. "avatar_url":null,
  21. "git_ssh_url":"git@39.105.58.61:root/demo-maven-service.git",
  22. "git_http_url":"http://39.105.58.61:9999/root/demo-maven-service.git",
  23. "namespace":"Administrator",
  24. "visibility_level":0,
  25. "path_with_namespace":"root/demo-maven-service",
  26. "default_branch":"master",
  27. "ci_config_path":null,
  28. "homepage":"http://39.105.58.61:9999/root/demo-maven-service",
  29. "url":"git@39.105.58.61:root/demo-maven-service.git",
  30. "ssh_url":"git@39.105.58.61:root/demo-maven-service.git",
  31. "http_url":"http://39.105.58.61:9999/root/demo-maven-service.git"
  32. },
  33. "commits":[
  34. {
  35. "id":"0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
  36. "message":"Delete deliver.sh",
  37. "title":"Delete deliver.sh",
  38. "timestamp":"2021-02-18T22:11:13+08:00",
  39. "url":"http://39.105.58.61:9999/root/demo-maven-service/-/commit/0d3c7eae64d87eb92d7132dd4233da7baf670ca7",
  40. "author":{
  41. "name":"Administrator",
  42. "email":"admin@example.com"
  43. },
  44. "added":[
  45. ],
  46. "modified":[
  47. ],
  48. "removed":[
  49. "jenkins/scripts/deliver.sh"
  50. ]
  51. },
  52. {
  53. "id":"5803768fca4b4b302b3c4be5bc43e7ff6c7f78ca",
  54. "message":"Delete Jenkinsfile",
  55. "title":"Delete Jenkinsfile",
  56. "timestamp":"2021-02-18T22:11:05+08:00",
  57. "url":"http://39.105.58.61:9999/root/demo-maven-service/-/commit/5803768fca4b4b302b3c4be5bc43e7ff6c7f78ca",
  58. "author":{
  59. "name":"Administrator",
  60. "email":"admin@example.com"
  61. },
  62. "added":[
  63. ],
  64. "modified":[
  65. ],
  66. "removed":[
  67. "jenkins/Jenkinsfile"
  68. ]
  69. },
  70. {
  71. "id":"ef9d7365666d007320a03493af057886b8cd1542",
  72. "message":"Update Jenkinsfile",
  73. "title":"Update Jenkinsfile",
  74. "timestamp":"2021-02-18T21:50:09+08:00",
  75. "url":"http://39.105.58.61:9999/root/demo-maven-service/-/commit/ef9d7365666d007320a03493af057886b8cd1542",
  76. "author":{
  77. "name":"Administrator",
  78. "email":"admin@example.com"
  79. },
  80. "added":[
  81. ],
  82. "modified":[
  83. ],
  84. "removed":[
  85. ]
  86. }
  87. ],
  88. "total_commits_count":3,
  89. "push_options":{
  90. },
  91. "repository":{
  92. "name":"demo-maven-service",
  93. "url":"git@39.105.58.61:root/demo-maven-service.git",
  94. "description":"",
  95. "homepage":"http://39.105.58.61:9999/root/demo-maven-service",
  96. "git_http_url":"http://39.105.58.61:9999/root/demo-maven-service.git",
  97. "git_ssh_url":"git@39.105.58.61:root/demo-maven-service.git",
  98. "visibility_level":0
  99. }
  100. }

4.2.5.7、共享库

  1. src/org/devops/build.groovy
  2. package org.devops
  3. //构建类型
  4. //传入参数构建类型,构建命令
  5. def Build(buildType,buildShell){
  6. //页面传入的buildType匹配mvn、ant、gradle、npm
  7. //m2、ant、Gradle、NPM为Global Tool Configuration配置变量名称
  8. def buildTools=["mvn":"m2","ant":"ant","gradle":"Gradle","npm":"NPM"]
  9. println("当前选择的构建类型为 ${buildType}")
  10. buildHome = tool buildTools[buildType]
  11. sh "${buildHome}/bin/${buildType} ${buildShell}"
  12. }

4.3.5.7、jenkinsfile

  1. #!groovy
  2. //引用Library库
  3. @Library('jenkinslib@master') _
  4. //引用构建共享库
  5. def build = new org.devops.build()
  6. //引入发布共享库
  7. def deploy = new org.devops.deploy()
  8. //引入参数
  9. String srcUrl = "${env.srcUrl}" //引用参数
  10. String branchName = "${env.branchName}" //引用参数
  11. String buildType = "${env.buildType}" //引用参数
  12. String buildShell = "${env.buildShell}" //引用参数
  13. pipeline {
  14. agent {
  15. node {
  16. label "master" //构建主机
  17. }
  18. }
  19. stages {
  20. //下载代码
  21. stage("CheckOut") {
  22. steps {
  23. script {
  24. checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: "${srcUrl}"]]])
  25. }
  26. }
  27. }
  28. stage("Build") {
  29. steps {
  30. script {
  31. //引入构建共享库内函数
  32. build.Build(buildType,buildShell)
  33. //引用发布共享库内函数
  34. }
  35. }
  36. }
  37. }
  38. }