github_release.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env ruby
  2. require 'fileutils'
  3. require 'pathname'
  4. require 'tmpdir'
  5. require 'octokit'
  6. BUILD_SH = Pathname(__FILE__).+('../../build.sh').expand_path
  7. VERSION = `sh '#{BUILD_SH}' get-version`.strip
  8. RELEASE = "v#{VERSION}"
  9. BUILD = BUILD_SH.parent + 'build'
  10. OBJC_ZIP = BUILD + "realm-objc-#{VERSION}.zip"
  11. SWIFT_ZIP = BUILD + "realm-swift-#{VERSION}.zip"
  12. CARTHAGE_ZIP = BUILD + 'Carthage.framework.zip'
  13. CARTHAGE_XCFRAMEWORK_ZIP = BUILD + 'Carthage.xcframework.zip'
  14. REPOSITORY = 'realm/realm-swift'
  15. puts 'Creating Carthage XCFramework package'
  16. FileUtils.rm_f CARTHAGE_XCFRAMEWORK_ZIP
  17. CARTHAGE_XCODE_VERSION = BUILD_SH.parent.+('Jenkinsfile.releasability').read()[/carthageXcodeVersion = '([0-9.]+)'/, 1]
  18. Dir.mktmpdir do |tmp|
  19. Dir.chdir(tmp) do
  20. system('unzip', SWIFT_ZIP.to_path, "realm-swift-#{VERSION}/#{CARTHAGE_XCODE_VERSION}/*.xcframework/*", :out=>"/dev/null") || exit(1)
  21. Dir.chdir("realm-swift-#{VERSION}/#{CARTHAGE_XCODE_VERSION}") do
  22. system('zip', '--symlinks', '-r', CARTHAGE_XCFRAMEWORK_ZIP.to_path, 'Realm.xcframework', 'RealmSwift.xcframework', :out=>"/dev/null") || exit(1)
  23. end
  24. end
  25. end
  26. def release_notes(version)
  27. changelog = BUILD_SH.parent.+('CHANGELOG.md').readlines
  28. current_version_index = changelog.find_index { |line| line =~ (/^#{Regexp.escape version}/) }
  29. unless current_version_index
  30. raise "Update the changelog for the last version (#{version})"
  31. end
  32. current_version_index += 2
  33. previous_version_lines = changelog[(current_version_index+1)...-1]
  34. previous_version_index = current_version_index + (previous_version_lines.find_index { |line| line =~ /^\d+\.\d+\.\d+(-(alpha|beta|rc)(\.\d+)?)?\s+/ } || changelog.count)
  35. relevant = changelog[current_version_index..previous_version_index]
  36. relevant.join.strip
  37. end
  38. RELEASE_NOTES = release_notes(VERSION)
  39. github = Octokit::Client.new
  40. github.access_token = ENV['GITHUB_ACCESS_TOKEN']
  41. puts 'Creating GitHub release'
  42. prerelease = (VERSION =~ /alpha|beta|rc/) ? true : false
  43. response = github.create_release(REPOSITORY, RELEASE, name: RELEASE, body: RELEASE_NOTES, prerelease: prerelease)
  44. release_url = response[:url]
  45. uploads = [OBJC_ZIP, SWIFT_ZIP, CARTHAGE_ZIP, CARTHAGE_XCFRAMEWORK_ZIP]
  46. uploads.each do |upload|
  47. puts "Uploading #{upload.basename} to GitHub"
  48. github.upload_asset(release_url, upload.to_path, content_type: 'application/zip')
  49. end