modified version of jenkins debian glue (https://github.com/mika/jenkins-debian-glue) for devuan
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.
 
 
 
 
 

87 lines
2.0 KiB

  1. #!/usr/bin/env ruby
  2. require 'httparty'
  3. require 'json'
  4. require 'net/http'
  5. require 'sinatra'
  6. set :port, 8042
  7. ## config
  8. CONFIG = YAML.load_file('config.yml')
  9. TOKEN = CONFIG["token"] || "unconfigured"
  10. CAUSE = CONFIG["cause"] || "git_commit_triggered"
  11. SERVER = CONFIG["server"] || "https://jenkins/"
  12. ## helper functions
  13. def trigger_job(job, branch)
  14. uri = URI("#{SERVER}job/#{job}/buildWithParameters")
  15. req = Net::HTTP::Post.new(uri.path)
  16. req.set_form_data('token' => TOKEN,
  17. 'cause' => CAUSE,
  18. 'branch' => branch)
  19. res = Net::HTTP.start(uri.host, uri.port,
  20. :use_ssl => uri.scheme == 'https',
  21. :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
  22. http.request(req)
  23. end
  24. case res
  25. when Net::HTTPSuccess, Net::HTTPRedirection
  26. p "OK"
  27. else
  28. res.value
  29. end
  30. end
  31. def job_names
  32. json = HTTParty.get("#{SERVER}/api/json")
  33. json["jobs"].map {|job| job["name"]}
  34. end
  35. def job_details(job_name)
  36. HTTParty.get("#{SERVER}/job/#{job_name}/api/json")
  37. end
  38. def build_details(job_name, build_number)
  39. HTTParty.get("#{SERVER}/job/#{job_name}/#{build_number}/api/json")
  40. end
  41. ## main sinatra app
  42. # this is where GitHub actually ends up at, configured as
  43. # WebHook URL at https://github.com/$OWNER/$PROJECT/settings/hooks
  44. post '/trigger' do
  45. push = JSON.parse(params[:payload])
  46. # p "JSON data: #{push.inspect}"
  47. # p JSON.pretty_generate(push)
  48. project = push["repository"]["name"]
  49. project = project + "-source" # we use $job-source as default entry point
  50. branch = push["ref"]
  51. # make sure we do not trigger a build with a branch that was just deleted
  52. deleted = push["deleted"]
  53. if deleted
  54. puts "branch #{branch} was deleted, not triggering build"
  55. elsif job_names.include? project
  56. trigger_job(project, branch)
  57. else
  58. puts "no such job #{project}"
  59. end
  60. end
  61. # debugging helper
  62. post '/debug' do
  63. puts params[:payload]
  64. end
  65. ## END OF FILE #################################################################