Skip to content

Commit

Permalink
Merge pull request #29 from theablefew/feature/publish_gem_workflow
Browse files Browse the repository at this point in the history
Update gem publishing workflow
  • Loading branch information
esmarkowski authored Mar 7, 2024
2 parents 8542e3b + ac36eb2 commit 65e6f78
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 16 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 1 addition & 13 deletions .github/workflows/spec.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Ruby CI

on:
workflow_call:
pull_request:

jobs:
Expand All @@ -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
Expand All @@ -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:
Expand Down
92 changes: 92 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions lib/stretchy/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

module Stretchy
VERSION = "0.3.0"
VERSION = '0.3.0'
end
2 changes: 2 additions & 0 deletions stretchy-model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 65e6f78

Please sign in to comment.