1
0

pagure_ci_jenkins.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Jenkins with Pagure-ci
  2. ======================
  3. Jenkins is a Continuous Integration service that can be configured to be
  4. integrated with pagure.
  5. This document describe the steps needed to make it work.
  6. How does it work?
  7. -----------------
  8. The principal is:
  9. * pagure will trigger a build on jenkins when a pull-request is created,
  10. updated or when someone explicitly asks pagure to do so or when a new commit
  11. is pushed (if pagure-ci is configured to trigger on commit).
  12. * pagure will send a few information to jenkins when triggering a build:
  13. ``REPO``, ``BRANCH``, ``BRANCH_TO``, ``cause``.
  14. * jenkins will do its work and, using webhook, report to pagure that it has
  15. finished its task
  16. * pagure will query jenkins to know the outcome of the task and flag the PR
  17. accordingly
  18. ``REPO`` corresponds to the url of the repository the pull-request originates
  19. from (so most often it will be a fork of the main repository).
  20. ``BRANCH`` corresponds to the branch the pull-request originates from (the
  21. branch of the fork).
  22. ``BRANCH_TO`` corresponds to the targeted branch in the main repository (the
  23. branch of the main project in which the PR is to be merged).
  24. ``cause`` is the reason the build was triggered (ie: the pull-request id or the
  25. commit hash).
  26. How to enable Pagure CI
  27. -----------------------
  28. * Visit the settings page of your project
  29. * Scroll down to the `Hooks` section and click on `Pagure CI`
  30. * Select the type of CI service you want
  31. * Enter the URL of the CI service. For example `http://jenkins.fedoraproject.org`
  32. * Enter the name of the job the CI service will trigger. For example `pagure-ci`
  33. * Tick the checkbox activating the hook. Either trigger on every commits, trigger only
  34. on pull-requests or both every commits and pull-requests.
  35. These steps will activate the hook, after reloading the page or the tab, you
  36. will be given access to two important values: the token used to trigger the
  37. build on jenkins and the URL used by jenkins to report the status of the
  38. build.
  39. Keep these two available when configuring jenkins for your project.
  40. Configure Jenkins
  41. -----------------
  42. These steps can only be made by the admins of your jenkins instance, but
  43. they only need to be made once.
  44. * Download the following plugins:
  45. * `Git Plugin <https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin>`_
  46. * `Notification Plugin <https://wiki.jenkins-ci.org/display/JENKINS/Notification+Plugin>`_
  47. Configure your project on Jenkins
  48. ---------------------------------
  49. * Go to the `Configure` page of your project
  50. * Under `Job Notification` click `Add Endpoint`
  51. * Fields in Endpoint will be :
  52. ::
  53. FORMAT: JSON
  54. PROTOCOL: HTTP
  55. EVENT: All Events
  56. URL: <The URL provided in the Pagure CI hook on pagure>
  57. TIMEOUT: 3000
  58. LOG: 1
  59. * Tick the checkbox `This build is parameterized`
  60. * Add two `String Parameters` named REPO and BRANCH
  61. * Source Code Management select Git and give the URL of the pagure project
  62. * Under Build Trigger click on Trigger build remotely and specify the token
  63. given by pagure.
  64. * Under Build -> Add build step -> Execute Shell
  65. * In the box given enter the shell steps you want for testing your project.
  66. Example Script
  67. ::
  68. # Script specific for Pull-Request build
  69. if [ -n "$REPO" -a -n "$BRANCH" ]; then
  70. git remote rm proposed || true
  71. git remote add proposed "$REPO"
  72. git fetch proposed
  73. git checkout origin/master
  74. git config --global user.email "you@example.com"
  75. git config --global user.name "Your Name"
  76. git merge --no-ff "proposed/$BRANCH" -m "Merge PR"
  77. fi
  78. # Part of the script specific to how you run the tests on your project
  79. * To use the URL to POST results you need to add CI token to Jenkins instance.
  80. This token could be found in Pagure project `Settings` -> `Hooks` -> `Pagure CI`.
  81. In Jenkins add it in `Manage Jenkins` -> `Manage Credentials` -> `Stores scoped to Jenkins`
  82. -> `Jenkins` -> `Global credentials (unrestricted)` -> `Add Credentials` as kind
  83. `Secret text` (ID will be used in script).
  84. Example function used in Jenkins pipeline script
  85. ::
  86. # 'pagure-auth' is the ID of the credentials
  87. def notifyPagurePR(repo, msg, status, phase, credentials = 'pagure-auth'){
  88. def json = JsonOutput.toJson([name: 'pagure', url: env.JOB_NAME, build: [full_url: currentBuild.absoluteUrl, status: status, number: currentBuild.number, phase: phase]])
  89. println json
  90. withCredentials([string(credentialsId: credentials, variable: "PAGURE_PUSH_SECRET")]) {
  91. /* We need to notify pagure that jenkins finished but then pagure will
  92. wait for jenkins to be done, so if we wait for pagure's answer we're
  93. basically stuck in a loop where both jenkins and pagure are waiting
  94. for each other */
  95. sh "timeout 1 curl -X POST -d \'$json\' https://pagure.io/api/0/ci/jenkins/$repo/\${PAGURE_PUSH_SECRET}/build-finished -H \"Content-Type: application/json\" | true"
  96. }
  97. }
  98. * To be able to trigger builds from Pagure CI you need to change the Global Security. Go
  99. to `Manage Jenkins` -> `Configure Global Security` and find `Authorization` section.
  100. In `Matrix-based security` add Read permission to `Anonymous Users` for Overall/Job/View.