Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
name: Publish gem

# master 上 gemspec 版本號尚未發佈到 rubygems.org 時,自動以 Trusted Publishing (OIDC) 發佈。
# 版本已存在則跳過;不使用任何儲存的 RubyGems API key。
on:
push:
branches:
- master
workflow_dispatch:

permissions:
contents: read

concurrency:
group: publish-gem-${{ github.repository }}
cancel-in-progress: false

env:
# 與 test.yml 矩陣中最新的 Ruby 3.4.11 / Rails 8.0 組合一致
RUBY_VERSION: '3.4.11'
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_8.0.gemfile
GEM_NAME: warehouse_items
GEMSPEC: warehouse_items.gemspec

jobs:
check:
name: Check version on rubygems.org
runs-on: ubuntu-latest
outputs:
should_publish: ${{ steps.check.outputs.should_publish }}
gem_name: ${{ steps.spec.outputs.name }}
gem_version: ${{ steps.spec.outputs.version }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: ruby/setup-ruby@b1da4809b005e0178fe08a1d57317bffc00b7b4c # v1.332.0
with:
ruby-version: ${{ env.RUBY_VERSION }}

- name: Read gem name and version from gemspec
id: spec
run: |
ruby -e 'spec = Gem::Specification.load(ENV.fetch("GEMSPEC")) or abort("cannot load #{ENV["GEMSPEC"]}"); abort("gemspec name #{spec.name} != #{ENV["GEM_NAME"]}") unless spec.name == ENV["GEM_NAME"]; puts "name=#{spec.name}"; puts "version=#{spec.version}"' >> "$GITHUB_OUTPUT"
cat "$GITHUB_OUTPUT"

- name: Check whether this version already exists on rubygems.org
id: check
env:
GEM_NAME: ${{ steps.spec.outputs.name }}
GEM_VERSION: ${{ steps.spec.outputs.version }}
run: |
set -euo pipefail
status=$(curl -sS -o versions.json -w '%{http_code}' "https://rubygems.org/api/v1/versions/${GEM_NAME}.json")
if [ "$status" = "404" ]; then
echo "Gem ${GEM_NAME} not found on rubygems.org; will publish."
echo "should_publish=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$status" != "200" ]; then
echo "::error::rubygems.org API returned HTTP ${status}"
exit 1
fi
if jq -e --arg v "$GEM_VERSION" 'any(.[]; .number == $v)' versions.json > /dev/null; then
echo "${GEM_NAME} ${GEM_VERSION} already on rubygems.org; skip publish."
echo "should_publish=false" >> "$GITHUB_OUTPUT"
else
echo "${GEM_NAME} ${GEM_VERSION} not on rubygems.org yet; will publish."
echo "should_publish=true" >> "$GITHUB_OUTPUT"
fi

test:
name: Test (Ruby 3.4 / Rails 8.0)
needs: check
if: needs.check.outputs.should_publish == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: ruby/setup-ruby@b1da4809b005e0178fe08a1d57317bffc00b7b4c # v1.332.0
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true

- run: bundle exec rake t

publish:
name: Publish ${{ needs.check.outputs.gem_name }} ${{ needs.check.outputs.gem_version }}
needs: [check, test]
if: needs.check.outputs.should_publish == 'true' && github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
environment: gem-publish
permissions:
id-token: write # Trusted Publishing (OIDC)
contents: write # rake release 推送 vX.Y.Z tag
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: ruby/setup-ruby@b1da4809b005e0178fe08a1d57317bffc00b7b4c # v1.332.0
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true

# setup-ruby 的 bundler-cache 會把 BUNDLE_PATH 等設定寫進 .bundle/config,
# 若該檔被 git 追蹤,rake release 的 guard_clean 會判定工作目錄不乾淨。
# 目前此 repo 沒有追蹤任何 .bundle/config;這一步對「有追蹤才標記」做容錯處理,
# 日後若加入 gemfiles/.bundle/config 或 .bundle/config 也不會讓發佈失敗。
- name: Ignore CI-only bundler config change
run: |
git status --porcelain
git ls-files -z '*.bundle/config' | xargs -0 -r git update-index --assume-unchanged
git status --porcelain

# 透過 rubygems/configure-rubygems-credentials 取得短效 OIDC token,
# 再執行 bundle exec rake release(build、建立並推送 vX.Y.Z tag、push gem)
- uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Tests

on:
push:
branches: [ master, main ]
pull_request:
branches: [ master, main ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- ruby: "3.3.12"
gemfile: gemfiles/rails_7.2.gemfile
- ruby: "3.3.12"
gemfile: gemfiles/rails_8.0.gemfile
- ruby: "3.4.11"
gemfile: gemfiles/rails_7.2.gemfile
- ruby: "3.4.11"
gemfile: gemfiles/rails_8.0.gemfile
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run tests
run: bundle exec rake t
9 changes: 9 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# CLAUDE.md

## 發版規則(Ruby gem)

- 這個 gem(`warehouse_items`,gemspec:`warehouse_items.gemspec`)在 master 收到「新版本號」時,由 GitHub Actions(`.github/workflows/publish.yml`)自動發佈到 rubygems.org(Trusted Publishing,不使用 API key)。
- **每一個 PR 都必須跳版號**(改 `lib/warehouse/version.rb`),除非這個 PR 只改文件、CI 或 CLAUDE.md 且明確不需要發版。沒跳版號的 PR 合進 master 後不會發佈。
- 跳版規則(Semantic Versioning):修 bug 跳 patch(x.y.Z);新增相容功能跳 minor(x.Y.0);不相容變更跳 major(X.0.0)。
- 版號只改在 PR 裡。AI 帳號只能開 branch 與 PR,不得合併到 master;只有真人帳號可以合併。合併就等於批准發佈。
- 不得為了讓測試通過而改變程式原本的行為。
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Rake::TestTask.new(:t) do |t|
t.test_files = FileList["test/**/*_test.rb"]
end

task :default => :test
task :default => :t
12 changes: 12 additions & 0 deletions gemfiles/rails_7.2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
source "https://rubygems.org"

gemspec path: ".."

gem "activesupport", "~> 7.2.0"
gem "rake", ">= 13"
gem "minitest", "~> 5.0"
gem "shoulda-context"
gem "logger"
gem "mutex_m"
gem "bigdecimal"
55 changes: 55 additions & 0 deletions gemfiles/rails_7.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
PATH
remote: ..
specs:
warehouse_items (1.0.0)
activesupport

GEM
remote: https://rubygems.org/
specs:
activesupport (7.2.3.2)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1, < 6)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
base64 (0.3.0)
benchmark (0.5.0)
bigdecimal (4.1.3)
concurrent-ruby (1.3.8)
connection_pool (3.0.2)
drb (2.2.3)
i18n (1.15.2)
concurrent-ruby (~> 1.0)
logger (1.7.0)
minitest (5.27.0)
mutex_m (0.3.0)
rake (13.4.2)
securerandom (0.4.1)
shoulda-context (2.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
activesupport (~> 7.2.0)
bigdecimal
bundler (>= 1.16)
logger
minitest (~> 5.0)
mutex_m
rake (>= 13, >= 10.0)
shoulda-context
warehouse_items!

BUNDLED WITH
2.7.2
13 changes: 13 additions & 0 deletions gemfiles/rails_8.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true
source "https://rubygems.org"

gemspec path: ".."

# Target Rails 8.0.5.1 ActiveSupport (8.1.3.1 also exists on RubyGems)
gem "activesupport", "8.0.5.1"
gem "rake", ">= 13"
gem "minitest", "~> 5.0"
gem "shoulda-context"
gem "logger"
gem "mutex_m"
gem "bigdecimal"
57 changes: 57 additions & 0 deletions gemfiles/rails_8.0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
PATH
remote: ..
specs:
warehouse_items (1.0.0)
activesupport

GEM
remote: https://rubygems.org/
specs:
activesupport (8.0.5.1)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
uri (>= 0.13.1)
base64 (0.3.0)
benchmark (0.5.0)
bigdecimal (4.1.3)
concurrent-ruby (1.3.8)
connection_pool (3.0.2)
drb (2.2.3)
i18n (1.15.2)
concurrent-ruby (~> 1.0)
logger (1.7.0)
minitest (5.27.0)
mutex_m (0.3.0)
rake (13.4.2)
securerandom (0.4.1)
shoulda-context (2.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uri (1.1.1)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
activesupport (= 8.0.5.1)
bigdecimal
bundler (>= 1.16)
logger
minitest (~> 5.0)
mutex_m
rake (>= 13, >= 10.0)
shoulda-context
warehouse_items!

BUNDLED WITH
2.7.2
8 changes: 5 additions & 3 deletions lib/warehouse.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "warehouse/version"
require 'active_support/core_ext/object'
require 'active_support/core_ext/class'
require 'active_support'
require "active_support"
require "active_support/core_ext/object"
require "active_support/core_ext/object/blank"
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/class"
require "warehouse/item/code"
require "warehouse/item/barcode"
require "warehouse/item/random"
Expand Down
6 changes: 4 additions & 2 deletions warehouse_items.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/superlanding/warehouse_items"
spec.license = "MIT"

spec.required_ruby_version = ">= 2.7"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
Expand All @@ -23,8 +25,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "bundler", ">= 1.16"
spec.add_development_dependency "rake", ">= 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "shoulda-context"

Expand Down
Loading