当前位置:   article > 正文

Jenkins + GitBlit自动构建_jenkins gitblit

jenkins gitblit

        多人协同过程中,避免不了多人提交,但没及时构建代码,导致代码被复盖。。。。

        然后有了现在的自动构建【GitBlit配置groovy进行关联即可 ,或GitHub使用WebHook实现】

笔者反复上面的操作,还是无法实现Git Push后触发钩子进行job构建】遇到此问题,直接拉到底部看解决方法!!!

        一、 安装Jenkins【Git Plugin】 + GitBlit ,且可以正常运行【遇到问题找度娘,哈哈】

二、配置GitBlit - groovy文件

GitBlit目录下【gitblit-1.9.3\data\groovy】,复制一份jenkins.groovy,并重新命名为 myjenkins.groovy【等会儿GitBlit-接收-post-receive 脚本会使用到

编辑myjenkins.groovy的jenkins主页面

找到【def jenkinsUrl】,修改为jenkins访问地址即可

修改完成后,重启GitBlit

修改完成后,重启GitBlit

修改完成后,重启GitBlit

三、添加接收的Jenkins

版本库为本地进行commit&push的仓库【实例使用的版本库就为Jenkins】

点击版本库-编辑,弹出一个List,点击【接收】,post-receive 脚本【把刚才复制并命令的myjenkins选择并保存】

到这里GitBlit已经没什么事了,可以建Jenkins项目,尝试本地commit&push文件,看看jenkins是否正确构建

 四、建jenkins项目【这里之前的文章也提及到了,所以这里贴图处理】

本地commit&push到GitBlit,Jenkins就会自动构建

 

 

笔者反复上面的操作,还是无法实现Git Push后触发钩子进行job构建。。。。

问题WebHook触发不了并提示权限有误,如下截图:

按提示链接最终找到解决方法:

 https://plugins.jenkins.io/git/#plugin-content-push-notification-from-repository

 解决方法:

1.在Jenkins添加一个Git Access token并保存,把token复制出来备注:

 2.再次编辑GitBlit的【myjenkins.groovy】文件,加上刚才生成token信息 并且保存,重启GitBlit

以下为完整修改后的代码内容:

  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import com.gitblit.GitBlit
  17. import com.gitblit.Keys
  18. import com.gitblit.models.RepositoryModel
  19. import com.gitblit.models.UserModel
  20. import com.gitblit.utils.JGitUtils
  21. import org.eclipse.jgit.lib.Repository
  22. import org.eclipse.jgit.revwalk.RevCommit
  23. import org.eclipse.jgit.transport.ReceiveCommand
  24. import org.eclipse.jgit.transport.ReceiveCommand.Result
  25. import org.slf4j.Logger
  26. /**
  27. * Sample Gitblit Post-Receive Hook: jenkins
  28. *
  29. * The Post-Receive hook is executed AFTER the pushed commits have been applied
  30. * to the Git repository. This is the appropriate point to trigger an
  31. * integration build or to send a notification.
  32. *
  33. * This script is only executed when pushing to *Gitblit*, not to other Git
  34. * tooling you may be using.
  35. *
  36. * If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
  37. * or web.xml then it will be executed by any repository when it receives a
  38. * push. If you choose to share your script then you may have to consider
  39. * tailoring control-flow based on repository access restrictions.
  40. *
  41. * Scripts may also be specified per-repository in the repository settings page.
  42. * Shared scripts will be excluded from this list of available scripts.
  43. *
  44. * This script is dynamically reloaded and it is executed within it's own
  45. * exception handler so it will not crash another script nor crash Gitblit.
  46. *
  47. * Bound Variables:
  48. * gitblit Gitblit Server com.gitblit.GitBlit
  49. * repository Gitblit Repository com.gitblit.models.RepositoryModel
  50. * receivePack JGit Receive Pack org.eclipse.jgit.transport.ReceivePack
  51. * user Gitblit User com.gitblit.models.UserModel
  52. * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
  53. * url Base url for Gitblit String
  54. * logger Logs messages to Gitblit org.slf4j.Logger
  55. * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger
  56. *
  57. * Accessing Gitblit Custom Fields:
  58. * def myCustomField = repository.customFields.myCustomField
  59. *
  60. */
  61. // Indicate we have started the script
  62. logger.info("jenkins hook triggered by ${user.username} for ${repository.name}")
  63. // This script requires Jenkins Git plugin 1.1.14 or later
  64. // http://kohsuke.org/2011/12/01/polling-must-die-triggering-jenkins-builds-from-a-git-hook/
  65. // define your jenkins url here or set groovy.jenkinsServer in
  66. // gitblit.properties or web.xml
  67. // def jenkinsUrl = gitblit.getString('groovy.jenkinsServer', 'http://yourserver/jenkins')
  68. def jenkinsUrl = gitblit.getString('groovy.jenkinsServer', 'http://192.168.24.74:9999')
  69. // define the repository base url
  70. def jenkinsGitbaseurl = gitblit.getString('groovy.jenkinsGitbaseurl', "${url}/r")
  71. // define the trigger url
  72. def triggerUrl = jenkinsUrl + "/git/notifyCommit?url=" + jenkinsGitbaseurl + "/${repository.name}" + "&token=f57bc27f16d3e8e0a8783a1caef7d888"
  73. // trigger the build
  74. new URL(triggerUrl).getContent()

重启后,可以进行测试一下;本地Git Push一下,滴滴滴。。。构建成功

        

其他工程构建后触发
Build after other projects are build

        某些环境的构建,相互之间彼此依赖,所以,有些环境必须先安装。对于大型项目而言,会有多个工程需要构建,此时,就可以编排工程的构建顺序,我们可以安排环境工程先构建。

【GitBlit_pull】为前置工程

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/492123
推荐阅读
相关标签
  

闽ICP备14008679号