diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..b540661 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,30 @@ +name: Release Gem + +on: + push: + branches: + - main + tags: + - 'v*' + paths: + - 'lib/stretchy/version.rb' + + +jobs: + # spec: + # uses: ./.github/workflows/spec.yml + + build: + runs-on: ubuntu-latest + # needs: spec + + steps: + - uses: actions/checkout@v4 + + - name: Publish gem + if: contains(github.ref, 'refs/tags/v') + uses: cadwallion/publish-rubygems-action@master + env: + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}} + RELEASE_COMMAND: rake release diff --git a/.github/workflows/spec.yml b/.github/workflows/spec.yml index fb8e38f..756fb32 100644 --- a/.github/workflows/spec.yml +++ b/.github/workflows/spec.yml @@ -1,6 +1,7 @@ name: Ruby CI on: + workflow_call: pull_request: jobs: @@ -13,8 +14,6 @@ jobs: os: ['ubuntu'] ruby: ['3.1', '2.7'] elasticsearch: ['7.x-SNAPSHOT', '8.12.2'] - # opensearch: ['2.12.0'] - steps: - uses: actions/checkout@v4 @@ -41,17 +40,6 @@ jobs: opensearch: runs-on: ${{matrix.os}}-latest - # services: - # opensearch: - # image: opensearchproject/opensearch:2 - # ports: - # - 9200:9200 - # env: - # discovery.type: single-node - # OPENSEARCH_JAVA_OPTS: "-Xms512m -Xmx512m" - # DISABLE_INSTALL_DEMO_CONFIG: true - # DISABLE_SECURITY_PLUGIN: true - # bootstrap.memory_lock: true strategy: matrix: diff --git a/Rakefile b/Rakefile index cd510a0..3675362 100644 --- a/Rakefile +++ b/Rakefile @@ -2,3 +2,95 @@ require "bundler/gem_tasks" task default: %i[] + +require 'octokit' +require 'versionomy' +require 'rainbow' + +def determine_current_version + # Load current version + load 'lib/stretchy/version.rb' + current_version = Versionomy.parse(Stretchy::VERSION) +end + +def determine_new_version(version) + # Load current version + current_version = determine_current_version + + # Determine new version + case version.to_sym + when :major + current_version.bump(:major) + when :minor + current_version.bump(:minor) + when :patch + current_version.bump(:tiny) + else + version =~ /\Av\d+\.\d+\.\d+\z/ ? Versionomy.parse(version) : current_version + end +end + +def create_release_branch(new_version, base_branch) + system("git stash save 'Changes before creating release branch'") + system("git fetch origin #{base_branch}") + branch_name = "release/v#{new_version}" + system("git checkout -b #{branch_name} #{base_branch}") + branch_name +end + +def update_version_file(new_version) + # Update lib/stretchy/version.rb + File.open('lib/stretchy/version.rb', 'w') do |file| + file.puts "module Stretchy\n VERSION = '#{new_version}'\nend" + end +end + +def commit_and_push_changes(new_version, branch_name) + system("git add lib/stretchy/version.rb") + system("git commit -m 'Bump version to v#{new_version}'") + system("git tag v#{new_version}") + system("git push origin #{branch_name} --tags") +end + +def create_pull_request(new_version, base_branch, branch_name) + # Create a pull request + client = Octokit::Client.new(access_token: ENV['GH_TOKEN']) + client.create_pull_request('theablefew/stretchy', base_branch, branch_name, "Release v#{new_version}") +end + +namespace :publish do + desc "Create a release" + task :release, [:version, :base_branch] do |t, args| + args.with_defaults(version: :patch, base_branch: 'main') + version = args[:version] + base_branch = args[:base_branch] + + old_version = determine_current_version + new_version = determine_new_version(version) + puts Rainbow("Bumping version from #{old_version} to #{new_version}").green + branch_name = create_release_branch(new_version, base_branch) + begin + update_version_file(new_version) + commit_and_push_changes(new_version, branch_name) + create_pull_request(new_version, base_branch, branch_name) + rescue => e + puts "Error: #{e.message}" + puts "Rolling back changes" + system("git tag -d v#{new_version}") + system("git checkout #{base_branch}") + system("git branch -D #{branch_name}") + end + end + + task :major do + Rake::Task['publish:release'].invoke('major') + end + + task :minor do + Rake::Task['publish:release'].invoke('minor') + end + + task :patch do + Rake::Task['publish:release'].invoke('patch') + end +end diff --git a/lib/stretchy/version.rb b/lib/stretchy/version.rb index 99b82e4..c0bb1a1 100644 --- a/lib/stretchy/version.rb +++ b/lib/stretchy/version.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - module Stretchy - VERSION = "0.3.0" + VERSION = '0.3.0' end diff --git a/stretchy-model.gemspec b/stretchy-model.gemspec index fd26457..f0c0e4f 100644 --- a/stretchy-model.gemspec +++ b/stretchy-model.gemspec @@ -45,6 +45,8 @@ Gem::Specification.new do |spec| spec.add_development_dependency "simplecov", "~> 0.21.2" spec.add_development_dependency "yard", "~> 0.9.36" spec.add_development_dependency "opensearch-ruby", "~> 3.0" + spec.add_development_dependency "octokit", "~> 4.20" + spec.add_development_dependency "versionomy", "~> 0.5.0" # For more information and examples about making a new gem, check out our # guide at: https://bundler.io/guides/creating_gem.html end