Created
August 17, 2017 10:05
-
-
Save jpkrohling/d9445dd0ee75a8de0a6354bf35e72edc to your computer and use it in GitHub Desktop.
Hawkular Services pipeline script to publish artifacts on a GitHub release
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rest-client' | |
require 'json' | |
require 'open-uri' | |
require 'digest' | |
organization = 'hawkular' | |
repository = 'hawkular-services' | |
username = ENV['GITHUB_USERNAME'] | |
password = ENV['GITHUB_PASSWORD'] | |
release_tag = ENV['MVN_RELEASE_VERSION'] | |
group_artifacts_to_upload = ['org.hawkular.services:hawkular-services-dist:zip', 'org.hawkular.services:hawkular-services-dist:tar.gz'] | |
base_local_path = "/home/jenkins/.m2/repository" | |
base_nexus_url = "https://repository.jboss.org/nexus/service/local/repositories/releases/content" | |
base_url = "https://#{username}:#{password}@api.github.com" | |
base_url_upload = "https://#{username}:#{password}@uploads.github.com" | |
begin | |
release = JSON.parse(RestClient.get("#{base_url}/repos/#{organization}/#{repository}/releases/tags/#{release_tag}").body) | |
puts "Publishing to the existing release: #{release_tag}" | |
rescue RestClient::NotFound | |
puts "Creating new release on GitHub: #{release_tag}" | |
release_response = RestClient.post "#{base_url}/repos/#{organization}/#{repository}/releases", { tag_name: release_tag }.to_json, :content_type => :json, :accept => :json | |
release = JSON.parse(release_response) | |
end | |
# assets of a release: | |
github_existing_assets = JSON.parse(RestClient.get("#{base_url}/repos/#{organization}/#{repository}/releases/#{release['id']}/assets")) | |
group_artifacts_to_upload.each do | group_artifact | | |
group, artifact, type = group_artifact.split ':' | |
base_maven_path = "#{group.gsub(/\./, '/')}/#{artifact}/#{release_tag}" | |
main_artifact = "#{artifact}-#{release_tag}.#{type}" | |
checksum = "#{main_artifact}.sha1" | |
[main_artifact, checksum].each do | asset | | |
if github_existing_assets.select { | item | item['name'] == asset }.empty? | |
artifact_contents = nil | |
local_artifact = "#{base_local_path}/#{base_maven_path}/#{asset}" | |
remote_artifact = "#{base_nexus_url}/#{base_maven_path}/#{asset}" | |
if File.exist? local_artifact | |
puts "Using local artifact #{local_artifact}" | |
File.open(local_artifact, 'r') do | file | | |
artifact_contents = file.read | |
end | |
else | |
puts "Using remote artifact #{remote_artifact}" | |
temp_artifact = Tempfile.open(asset) do | file | | |
file.write open(remote_artifact).read | |
file.rewind | |
unless asset.end_with? '.sha1' | |
checksum_actual = Digest::SHA1.file file.path | |
checksum_expected = nil | |
if File.exist? "#{local_artifact}.sha1" | |
checksum_expected = open("#{local_artifact}.sha1").read | |
else | |
checksum_expected = open("#{remote_artifact}.sha1").read | |
end | |
unless checksum_actual == checksum_expected | |
raise "The SHA1 checksum for #{asset} doesn't match the expected checksum from #{remote_artifact}.sha1. Actual checksum: #{checksum_actual}" | |
end | |
end | |
artifact_contents = file.read | |
end | |
end | |
upload_url = "#{base_url_upload}/repos/#{organization}/#{repository}/releases/#{release['id']}/assets?name=#{asset}" | |
puts "Uploading #{asset} to release #{release_tag}" | |
RestClient::Request.execute( | |
method: :post, | |
url: upload_url, | |
headers: {'Content-Type' => 'application/zip'}, | |
payload: artifact_contents | |
) | |
else | |
puts "The asset #{asset} already exists for this release. Skipping the upload. To override, manually delete the asset from GitHub and re-run this script." | |
end | |
end | |
end | |
puts "Finished creating the release and uploading the assets to GitHub" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment