|
| 1 | +WORKSPACE_DIR = File.expand_path(File.dirname(__FILE__) + '/..') |
| 2 | + |
| 3 | +def in_dir(dir) |
| 4 | + current = Dir.pwd |
| 5 | + begin |
| 6 | + Dir.chdir(dir) |
| 7 | + yield |
| 8 | + ensure |
| 9 | + Dir.chdir(current) |
| 10 | + end |
| 11 | +end |
| 12 | + |
| 13 | +def stage(stage_name, description, options = {}) |
| 14 | + if ENV['STAGE'].nil? || ENV['STAGE'] == stage_name || options[:always_run] |
| 15 | + puts "🚀 Release Stage: #{stage_name} - #{description}" |
| 16 | + begin |
| 17 | + yield |
| 18 | + rescue Exception => e |
| 19 | + puts '💣 Error completing stage.' |
| 20 | + puts "Fix the error and re-run release process passing: STAGE=#{stage_name}#{ ENV['PREVIOUS_PRODUCT_VERSION'] ? " PREVIOUS_PRODUCT_VERSION=#{ENV['PREVIOUS_PRODUCT_VERSION']}" : ''}#{ ENV['PREVIOUS_PRODUCT_VERSION'] ? " PRODUCT_VERSION=#{ENV['PRODUCT_VERSION']}" : ''}" |
| 21 | + raise e |
| 22 | + end |
| 23 | + ENV['STAGE'] = nil unless options[:always_run] |
| 24 | + elsif !ENV['STAGE'].nil? |
| 25 | + puts "Skipping Stage: #{stage_name} - #{description}" |
| 26 | + end |
| 27 | +end |
| 28 | + |
| 29 | +desc 'Perform a release' |
| 30 | +task 'perform_release' do |
| 31 | + |
| 32 | + in_dir(WORKSPACE_DIR) do |
| 33 | + stage('ExtractVersion', 'Extract the last version from CHANGELOG.md and derive next version unless specified', :always_run => true) do |
| 34 | + changelog = IO.read('CHANGELOG.md') |
| 35 | + ENV['PREVIOUS_PRODUCT_VERSION'] ||= changelog[/^### \[v(\d+\.\d+)\]/, 1] |
| 36 | + |
| 37 | + next_version = ENV['PRODUCT_VERSION'] |
| 38 | + unless next_version |
| 39 | + version_parts = ENV['PREVIOUS_PRODUCT_VERSION'].split('.') |
| 40 | + next_version = "#{version_parts[0]}.#{sprintf('%02d', version_parts[1].to_i + 1)}" |
| 41 | + ENV['PRODUCT_VERSION'] = next_version |
| 42 | + end |
| 43 | + |
| 44 | + # Also initialize release date if required |
| 45 | + ENV['RELEASE_DATE'] ||= Time.now.strftime('%Y-%m-%d') |
| 46 | + end |
| 47 | + |
| 48 | + stage('ZapWhite', 'Ensure that zapwhite produces no changes') do |
| 49 | + sh 'bundle exec zapwhite' |
| 50 | + end |
| 51 | + |
| 52 | + stage('GitClean', 'Ensure there is nothing to commit and the working tree is clean') do |
| 53 | + status_output = `git status -s 2>&1`.strip |
| 54 | + raise 'Uncommitted changes in git repository. Please commit them prior to release.' if 0 != status_output.size |
| 55 | + end |
| 56 | + |
| 57 | + stage('Build', 'Build the project to ensure that the tests pass') do |
| 58 | + sh "bundle exec buildr clean package PRODUCT_VERSION=#{ENV['PRODUCT_VERSION']}" |
| 59 | + end |
| 60 | + |
| 61 | + stage('PatchChangelog', 'Patch the changelog to update from previous release') do |
| 62 | + changelog = IO.read('CHANGELOG.md') |
| 63 | + changelog = changelog.gsub("### Unreleased\n", <<HEADER) |
| 64 | +### [v#{ENV['PRODUCT_VERSION']}](https://github.com/react4j/react4j-widget/tree/v#{ENV['PRODUCT_VERSION']}) (#{ENV['RELEASE_DATE']}) |
| 65 | +[Full Changelog](https://github.com/react4j/react4j-widget/compare/v#{ENV['PREVIOUS_PRODUCT_VERSION']}...v#{ENV['PRODUCT_VERSION']}) |
| 66 | +HEADER |
| 67 | + IO.write('CHANGELOG.md', changelog) |
| 68 | + |
| 69 | + sh 'git reset 2>&1 1> /dev/null' |
| 70 | + sh 'git add CHANGELOG.md' |
| 71 | + sh 'git commit -m "Update CHANGELOG.md in preparation for release"' |
| 72 | + end |
| 73 | + |
| 74 | + stage('TagProject', 'Tag the project') do |
| 75 | + sh "git tag v#{ENV['PRODUCT_VERSION']}" |
| 76 | + end |
| 77 | + |
| 78 | + stage('PatchChangelogPostRelease', 'Patch the changelog post release to prepare for next development iteration') do |
| 79 | + changelog = IO.read('CHANGELOG.md') |
| 80 | + changelog = changelog.gsub("# Change Log\n", <<HEADER) |
| 81 | +# Change Log |
| 82 | +
|
| 83 | +### Unreleased |
| 84 | +HEADER |
| 85 | + IO.write('CHANGELOG.md', changelog) |
| 86 | + |
| 87 | + `bundle exec zapwhite` |
| 88 | + sh 'git add CHANGELOG.md' |
| 89 | + sh 'git commit -m "Update CHANGELOG.md in preparation for next development iteration"' |
| 90 | + end |
| 91 | + |
| 92 | + stage('PushChanges', 'Push changes to git repository') do |
| 93 | + sh 'git push' |
| 94 | + sh 'git push --tags' |
| 95 | + end |
| 96 | + |
| 97 | + stage('GithubRelease', 'Create a Release on GitHub') do |
| 98 | + changelog = IO.read('CHANGELOG.md') |
| 99 | + start = changelog.index("### [v#{ENV['PRODUCT_VERSION']}]") |
| 100 | + raise "Unable to locate version #{ENV['PRODUCT_VERSION']} in change log" if -1 == start |
| 101 | + start = changelog.index("\n", start) |
| 102 | + start = changelog.index("\n", start + 1) |
| 103 | + |
| 104 | + end_index = changelog.index('### [v', start) |
| 105 | + |
| 106 | + changes = changelog[start, end_index - start] |
| 107 | + |
| 108 | + changes = changes.strip |
| 109 | + |
| 110 | + tag = "v#{ENV['PRODUCT_VERSION']}" |
| 111 | + |
| 112 | + require 'octokit' |
| 113 | + |
| 114 | + client = Octokit::Client.new(:netrc => true, :auto_paginate => true) |
| 115 | + client.login |
| 116 | + client.create_release('react4j/react4j-widget', tag, :name => tag, :body => changes, :draft => false, :prerelease => true) |
| 117 | + |
| 118 | + candidates = client.list_milestones('react4j/react4j-widget').select {|m| m[:title].to_s == tag} |
| 119 | + unless candidates.empty? |
| 120 | + milestone = candidates[0] |
| 121 | + unless milestone[:state] == 'closed' |
| 122 | + client.update_milestone('react4j/react4j-widget', milestone[:number], :state => 'closed') |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + if ENV['STAGE'] |
| 129 | + raise "Invalid STAGE specified '#{ENV['STAGE']}' that did not match any stage" |
| 130 | + end |
| 131 | +end |
0 commit comments