当前位置:   article > 正文

Jenkins通过git合并分支实现自动打包_jenkins自动合并代码

jenkins自动合并代码

实现功能:git提交代码,然后Jenkins自动打包两次,提交前的分支一次,合并后的分支一次

如:dev提交到master,使得Jenkins打两个包,即dev分支在提交合并请求时打一次,

在合并请求完成后master分支打一次

git的webhooks设置

  1. Url填入jenkins的job配置

  1. 勾选合并请求触发构建,勾选后在合并请求过程中会根据jenkins的详细控制进行触发

在git合并分支,触发webhooks时,会向Jenkins传递相应参数,如下示例:

  1. 以下为打开合并请求时携带的参数:
  2. gitlabBranch : dev
  3. gitlabSourceBranch : dev
  4. gitlabActionType : MERGE
  5. gitlabUserName : Administrator
  6. gitlabUserEmail : admin@example.com
  7. gitlabSourceRepoHomepage : http://10.9.8.185/gitlab-instance-02db178f/demo
  8. gitlabSourceRepoName : demo
  9. gitlabSourceNamespace : GitLab Instance
  10. gitlabSourceRepoURL : git@10.9.8.185:gitlab-instance-02db178f/demo.git
  11. gitlabSourceRepoSshUrl : git@10.9.8.185:gitlab-instance-02db178f/demo.git
  12. gitlabSourceRepoHttpUrl : http://10.9.8.185/gitlab-instance-02db178f/demo.git
  13. gitlabMergeRequestTitle : Update dev3
  14. gitlabMergeRequestDescription :
  15. gitlabMergeRequestId : 6
  16. gitlabMergeRequestIid : 6
  17. gitlabMergeRequestState : opened
  18. gitlabMergedByUser : root
  19. gitlabMergeRequestAssignee :
  20. gitlabMergeRequestLastCommit : 9549b3448ccf41995dba6ec723715ad361676702
  21. gitlabMergeRequestTargetProjectId : 2
  22. gitlabTargetBranch : main
  23. gitlabTargetRepoName : demo
  24. gitlabTargetNamespace : GitLab Instance
  25. gitlabTargetRepoSshUrl : git@10.9.8.185:gitlab-instance-02db178f/demo.git
  26. gitlabTargetRepoHttpUrl : http://10.9.8.185/gitlab-instance-02db178f/demo.git
  27. gitlabBefore :
  28. gitlabAfter :
  29. gitlabTriggerPhrase :
  30. 注:当完成合并请求时,gitlabMergeRequestState 的状态会改变,如下:
  31. gitlabMergeRequestState : merged

Jenkins项目的触发设置

Jenkins配置

  1. http://10.9.8.185/project/demo 为job的url,需要填入gitlab webhooks 的配置

  1. 根据gitlab的触发请求选择触发构建的条件,控制权在gitlab webhooks,

如gitlab 没有push请求则jenkins不会因为gitlab的push触发构建

但合并请求的详细控制在jenkins,分别对应:打开请求,新的commit提交,完成合并,关闭合并等

Jenkins的maven文件示例

在完成git和Jenkins项目的配置后,需要根据需求书写Jenkins pipeline的maven打包文件

注:以下示例中的jacoco、junit、sonar需要额外插件

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('parameters'){
  5. steps{
  6. sh '''
  7. echo "gitlab webhooks触发构建携带的参数"
  8. echo "
  9. gitlabBranch : ${gitlabBranch}
  10. gitlabSourceBranch : ${gitlabSourceBranch}
  11. gitlabActionType : ${gitlabActionType}
  12. gitlabUserName : ${gitlabUserName}
  13. gitlabUserEmail : ${gitlabUserEmail}
  14. gitlabSourceRepoHomepage : ${gitlabSourceRepoHomepage}
  15. gitlabSourceRepoName : ${gitlabSourceRepoName}
  16. gitlabSourceNamespace : ${gitlabSourceNamespace}
  17. gitlabSourceRepoURL : ${gitlabSourceRepoURL}
  18. gitlabSourceRepoSshUrl : ${gitlabSourceRepoSshUrl}
  19. gitlabSourceRepoHttpUrl : ${gitlabSourceRepoHttpUrl}
  20. gitlabMergeRequestTitle : ${gitlabMergeRequestTitle}
  21. gitlabMergeRequestDescription : ${gitlabMergeRequestDescription}
  22. gitlabMergeRequestId : ${gitlabMergeRequestId}
  23. gitlabMergeRequestIid : ${gitlabMergeRequestIid}
  24. gitlabMergeRequestState : ${gitlabMergeRequestState}
  25. gitlabMergedByUser : ${gitlabMergedByUser}
  26. gitlabMergeRequestAssignee : ${gitlabMergeRequestAssignee}
  27. gitlabMergeRequestLastCommit : ${gitlabMergeRequestLastCommit}
  28. gitlabMergeRequestTargetProjectId : ${gitlabMergeRequestTargetProjectId}
  29. gitlabTargetBranch : ${gitlabTargetBranch}
  30. gitlabTargetRepoName : ${gitlabTargetRepoName}
  31. gitlabTargetNamespace : ${gitlabTargetNamespace}
  32. gitlabTargetRepoSshUrl : ${gitlabTargetRepoSshUrl}
  33. gitlabTargetRepoHttpUrl : ${gitlabTargetRepoHttpUrl}
  34. gitlabBefore : ${gitlabBefore}
  35. gitlabAfter : ${gitlabAfter}
  36. gitlabTriggerPhrase : ${gitlabTriggerPhrase}"
  37. '''
  38. }
  39. }
  40. stage('checkout') {
  41. steps {
  42. script{
  43. if( "${env.gitlabMergeRequestState}" == 'opened' ){
  44. echo "打开合并请求,构建合并分支"
  45. env.BRANCH="${env.gitlabBranch}"
  46. }
  47. if( "${env.gitlabMergeRequestState}" == 'merged' ){
  48. echo "完成合并请求,切换构建分支为主分支"
  49. env.BRANCH="${env.gitlabTargetBranch}"
  50. }
  51. if( "${env.gitlabMergeRequestState}" == '' || "${env.gitlabMergeRequestState}" == 'null' ){
  52. echo "没有合并请求,构建默认分支"
  53. env.BRANCH="${BRANCH}"
  54. }
  55. echo "动态分支 :${env.BRANCH} 拉取指定分支代码"
  56. }
  57. echo "env.BRANCH 为定义的参数变量,根据变量拉取指定分支"
  58. checkout([$class: 'GitSCM', branches: [[name: "${env.BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: '5e3b04f0-93aa-4607-a149-b0ca73de9cfe', url: 'http://10.9.8.185/gitlab-instance-02db178f/demo.git']]])
  59. }
  60. }
  61. stage('BUILD FILE') {
  62. steps {
  63. sh 'echo "build 构建日期文件写入"'
  64. sh '''
  65. L_RELEASE_NOTE=Notes.txt
  66. L_COMMIT=`git log -1 --pretty=format:%H`
  67. echo "Release Date: ` date \'+%Y-%m-%d %H:%M:%S\'`" >> $L_RELEASE_NOTE
  68. echo "Commit: ${L_COMMIT}" >> ${L_RELEASE_NOTE}
  69. echo "Build Number: "${BUILD_NUMBER} >> ${L_RELEASE_NOTE}
  70. echo "Web Address: "${BUILD_URL} >> ${L_RELEASE_NOTE}
  71. NOTE_FILE=BUILD
  72. echo "build: ${VERSION}-b${BUILD_NUMBER}(${BRANCH})" >> $NOTE_FILE
  73. echo "date: `date \'+%Y-%m-%d %H:%M:%S\'`" >> $NOTE_FILE '''
  74. }
  75. }
  76. stage('build') {
  77. steps {
  78. echo "build 安装包"
  79. sh 'mvn versions:set -DnewVersion=${VERSION}'
  80. sh 'mvn clean package -Dmaven.test.failure.ignore=true -Dbuild.number=${BUILD_NUMBER}'
  81. //sh 'mvn clean org.jacoco:jacoco-maven-plugin:0.8.8:prepare-agent package -Dmaven.test.failure.ignore=true -Dbuild.number=${BUILD_NUMBER}'
  82. }
  83. }
  84. stage('Junit Test') {
  85. steps {
  86. echo "构建后使用junit插件 添加报告"
  87. junit 'target/surefire-reports/*.xml'
  88. }
  89. }
  90. stage('SonarQube') {
  91. steps {
  92. script{
  93. scannerHome = tool 'SonarQubeScanner'
  94. }
  95. withSonarQubeEnv('SonarQube7.8') {
  96. // This expands the evironment variables SONAR_CONFIG_NAME, SONAR_HOST_URL, SONAR_AUTH_TOKEN that can be used by any script.
  97. sh "${scannerHome}/bin/sonar-scanner " +
  98. "-Dsonar.projectKey=demo " +
  99. "-Dsonar.host.url=http://10.97.8.185:6080 " +
  100. "-Dsonar.java.binaries=target/classes " +
  101. "-Dsonar.sources=. " +
  102. "-Dsonar.sourceEncoding=UTF-8 " +
  103. "-Dsonar.nodejs.executable=/opt/node/node-v16.17.0-linux-x64/bin/node " +
  104. "-Dsonar.login=4ac58e617129ce88038d9741f5ba6a786cda3ed6"
  105. }
  106. }
  107. }
  108. stage('Code Coverage') {
  109. steps {
  110. echo "构建后使用jacoco插件 添加报告"
  111. sh 'ls target'
  112. //sh "mvn org.jacoco:jacoco-maven-plugin:0.8.8:report"
  113. jacoco()
  114. }
  115. }
  116. stage('artifacts') {
  117. steps {
  118. echo "归档文件,指定目录下build文件"
  119. //archiveArtifacts artifacts: 'target/*.jar', followSymlinks: false
  120. }
  121. }
  122. }
  123. post {
  124. always {
  125. echo " always 构建后触发"
  126. }
  127. success{
  128. echo " success 构建成功 "
  129. }
  130. failure{
  131. echo " failure 构建失败 "
  132. }
  133. aborted{
  134. echo " aborted 终止构建 "
  135. }
  136. }
  137. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/825025
推荐阅读
相关标签
  

闽ICP备14008679号