Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Travis CI API LT

3140de70ab04d8f9fc979447b0b4fb16?s=47 pinzolo
September 17, 2014

Travis CI API LT

Travis CI Meetup

3140de70ab04d8f9fc979447b0b4fb16?s=128

pinzolo

September 17, 2014
Tweet

Transcript

  1. Travis API

  2. Who am I? 4 ໺ా ੣ਓ 4 גࣜձࣾ ۭ 4

    Engineer 4 @pinzolo 4 pinzolo
  3. How Travis? 4 ࣗ࡞ gem ͷςετ 4 Redmine ϓϥάΠϯͷςετ

  4. Why Travis? 4 ϏϧυϚτϦΫε 4 apt ͕࢖͑Δ

  5. Travis API 4 աڈͷϏϧυͷ৘ใ͕औಘͰ͖Δ 4 δϣϒͷ࣮ߦɺΩϟϯηϧɺ࠶࣮ߦͳͲ͕ϒϥ΢βͳ͠Ͱ ΋Ͱ͖Δ

  6. Travis Gem Travis ͷ API Λୟͨ͘Ίͷ gem 4 Travis API

    Λୟ͘ travis ίϚϯυ͕ར༻Ͱ͖Δ 4 travis raw Ͱ࣮ࡍͷ JSON ΋֬ೝͰ͖Δ 4 API Λϥοϓͯ͠ίʔυ͔Β࢖༻͢ΔͨΊͷϥΠϒϥϦͰ ΋͋Δ
  7. Travis Gem Travis ͷ API Λୟͨ͘Ίͷ gem 4 Travis API

    Λୟ͘ travis ίϚϯυ͕ར༻Ͱ͖Δ 4 travis raw Ͱ࣮ࡍͷ JSON ΋֬ೝͰ͖Δ 4 API Λϥοϓͯ͠ίʔυ͔Β࢖༻͢ΔͨΊͷϥΠϒϥϦͰ ΋͋Δ ← ͜ΕΛ࢖͏
  8. αϯϓϧΞϓϦέʔγϣϯ Redmine ຊମͷςετཤྺΛऔಘ͠ɺRuby ͷόʔδϣϯʹ ΑΔύϑΥʔϚϯεൺֱΛͯ͠ΈΔ https://github.com/pinzolo/travis-chart

  9. αϯϓϧίʔυ ಛఆͷϦϙδτϦͷϏϧυཤྺɺδϣϒͷ৘ใΛ DB ʹอଘ͢ Δίʔυ

  10. class Agent def initialize Travis.access_token = ENV['TRAVIS_TOKEN'] end def save_all(slug)

    t_repo = Travis::Repository.find(slug) ActiveRecord::Base.transaction do repo = save_repo(t_repo) t_repo.builds.each do |t_build| build = save_build(repo, t_build) t_build.jobs.each do |t_job| save_job(build, t_job) end end end end def save_repo(t_repo) if repo = Repository.where(slug: t_repo.slug).first repo.update(t_repo.to_h.slice(*%w(private description github_language))) else repo = Repository.create(t_repo.to_h.slice(*%w(slug private description github_language))) end repo end def save_build(repo, t_build) build_params = t_build.to_h.slice(*%w(commit_id number pull_request_id state started_at finished_at duration)) if build = repo.builds.where(number: t_build.number).first build.update(build_params) else build = repo.builds.build(build_params) build.save end build end def save_job(build, t_job) job_params = t_job.to_h.slice(*%w(log_id number state started_at finished_at queue)) job_params.merge!(t_job.to_h['config'].slice('rvm', 'env')) job_params['duration'] = (t_job.finished_at - t_job.started_at).to_i if t_job.finished_at && t_job.started_at if job = build.jobs.where(number: t_job.number).first job.update(job_params) else job = build.jobs.build(job_params) job.save end job end end
  11. 50ߦ଍ΒͣͰՄೳ 4 Repository > Build > Job ͳͲඇৗʹ௚ײతʹ࢖͑Δ 4 දࣔ͞Ε͍ͯΔ৘ใ͸΄΅औಘՄೳ

  12. ϋϚΓͲ͜Ζ 4 access token ͸ profile ϖʔδͷ token Ͱ͸͋Γ·ͤΜ 4

    travis token Ͱऔಘ͢Δ
  13. ࢖༻ͯ͠Έͯ औಘͰ͖Δ duration ͸ɺδϣϒશମͷ࣌ؒͳͷͰɺςετͷ ͚࣌ؒͩͰͳ͘ gem ͷμ΢ϯϩʔυ΍લ४උͳͲ΋ؚ·Εͯ ͠·͏ɻ Travis ͷαʔό͸·ͩ͠΋

    Rubygems.org ΍ Github ͷωο τϫʔΫ࣍ୈͳ݁ՌʹͳΔ͜ͱ΋͋Γ·͢ɻ ͳͷͰ install, before_script, script ͱ͍ͬͨɺδϣϒͷ ֤ηΫγϣϯ͝ͱͷ duration ͕औಘͰ͖Δͱخ͍͠ɻ
  14. ͋Γ͕ͱ͏͍͟͝·ͨ͠