require 'fileutils' require 'open-uri' require 'zip' module JenkinsWorkflow class Publisher attr_accessor :url def initialize(url: nil) @url = url end # Fetch results archived for the particular job, and stick them in your # workspace directory. # # This will blindly assume your workspace directory is configured for # working with the p4 command line client. As in, you have a login ticket, # and .p4config setup properly, etc. # # The job archive is first downloaded to a temporary directory. We unpack # it and cherry-pick things we are interested in, to create a "results" # site. # def archive_results(job_url, workspace_dir) Dir.mktmpdir('publisher') do |temp_dir| download_and_unpack_archive(job_url, temp_dir) archive_dir = File.join(temp_dir, 'archive') copy_tarball(archive_dir, workspace_dir) copy_installers(archive_dir, workspace_dir) copy_documentation(archive_dir, workspace_dir) copy_test_results(archive_dir, workspace_dir) end puts 'You are good to go to submit' end def download_and_unpack_archive(job_url, temp_dir) archive_url = "#{job_url}/artifact/*zip*/archive.zip" zip_path = File.join(temp_dir, 'archive.zip') open(zip_path, 'wb') do |file| file << open(archive_url).read end Zip::File.open(zip_path) do |zip_file| zip_file.each do |f| f_path = File.join(temp_dir, f.name) FileUtils.mkdir_p(File.dirname(f_path)) zip_file.extract(f, f_path) unless File.exist?(f_path) end end File.unlink(zip_path) end def copy_tarball(temp_dir, workspace_dir) tgz_path = File.join(temp_dir, 'pkg/helix-web-services.tgz') FileUtils.copy(tgz_path, workspace_dir) end def copy_installers(temp_dir, workspace_dir) file_name = 'helix-web-services-2015.1.main~trusty.deb' dest = File.join(workspace_dir, file_name) File.unlink(dest) if File.exist?(dest) Dir.glob(File.join(temp_dir, 'helix-web-services/packaging/omnibus-helix-web-services/pkg/*~trusty*.deb')) do |p| FileUtils.move(p, dest) end file_name = 'helix-web-services-2015.1.main~precise.deb' dest = File.join(workspace_dir, file_name) File.unlink(dest) if File.exist?(dest) Dir.glob(File.join(temp_dir, 'helix-web-services/packaging/omnibus-helix-web-services/pkg/*~precise*.deb')) do |p| FileUtils.move(p, dest) end file_name = 'helix-web-services-2015.1.main.rpm' dest = File.join(workspace_dir, file_name) File.unlink(dest) if File.exist?(dest) Dir.glob(File.join(temp_dir, 'helix-web-services/packaging/omnibus-helix-web-services/pkg/*.rpm')) do |p| FileUtils.move(p, dest) end end def copy_documentation(temp_dir, workspace_dir) Dir.glob(File.join(temp_dir, 'doc-output/*')) do |path| name = File.basename(path) FileUtils.copy(path, File.join(workspace_dir, 'doc', name)) end output_dir = File.join(workspace_dir, 'doc', 'helix_web_services') FileUtils.rmtree(output_dir) FileUtils.move(File.join(temp_dir, 'helix_web_services', 'doc-output'), output_dir) output_dir = File.join(workspace_dir, 'doc', 'helix_web_services_client_ruby') FileUtils.rmtree(output_dir) FileUtils.move(File.join(temp_dir, 'helix_web_services_client', 'doc-output'), output_dir) end def copy_test_results(temp_dir, workspace_dir) Dir.glob("#{temp_dir}/**/spec-output/*.html") do |path| name = File.basename(path) dest = File.join(workspace_dir, 'test', name) unless File.exist?(File.dirname(dest)) FileUtils.mkdir_p(File.dirname(dest)) end FileUtils.cp(path, dest) end end end end