You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jenkins.groovy 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // define your jenkins access token here or set groovy.jenkinsToken in
  69. // gitblit.properties or web.xml (https://github.com/jenkinsci/git-plugin/#push-notification-from-repository)
  70. def jenkinsToken = gitblit.getString('groovy.jenkinsToken', 'yourtoken')
  71. // define the repository base url
  72. def jenkinsGitbaseurl = gitblit.getString('groovy.jenkinsGitbaseurl', "${url}/r")
  73. // define the trigger url
  74. def triggerUrl = jenkinsUrl + "/git/notifyCommit?url=" + jenkinsGitbaseurl + "/${repository.name}" + "&token=" + jenkinsToken
  75. // trigger the build
  76. new URL(triggerUrl).getContent()