Publish Ruby Gem #3
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish Ruby Gem | |
on: | |
release: | |
types: [published] | |
push: | |
tags: | |
- "v*" | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Ruby | |
uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: "3.1" | |
bundler-cache: true | |
- name: Extract version from tag | |
id: version | |
run: | | |
if [[ $GITHUB_REF == refs/tags/* ]]; then | |
VERSION=${GITHUB_REF#refs/tags/v} | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
echo "Publishing version: $VERSION" | |
else | |
echo "Not a tag push, skipping" | |
exit 1 | |
fi | |
- name: Verify version matches gemspec | |
run: | | |
GEMSPEC_VERSION=$(ruby -e "spec = eval(File.read(Dir.glob('*.gemspec').first)); puts spec.version") | |
TAG_VERSION="${{ steps.version.outputs.version }}" | |
if [ "$GEMSPEC_VERSION" != "$TAG_VERSION" ]; then | |
echo "Version mismatch: gemspec=$GEMSPEC_VERSION, tag=$TAG_VERSION" | |
echo "Your Python script should have already updated the gemspec version" | |
exit 1 | |
fi | |
echo "Version verified: $GEMSPEC_VERSION" | |
- name: Run tests (if they exist) | |
run: | | |
if [ -f "spec/spec_helper.rb" ] || [ -f "test/test_helper.rb" ]; then | |
bundle exec rake test || bundle exec rspec || echo "Tests not configured, skipping" | |
else | |
echo "No tests found, skipping" | |
fi | |
- name: Build gem | |
run: | | |
gem build *.gemspec | |
echo "Built gem files:" | |
ls -la *.gem | |
- name: Configure RubyGems credentials | |
run: | | |
mkdir -p ~/.gem | |
echo ":rubygems_api_key: ${{ secrets.RUBYGEMS_API_KEY }}" > ~/.gem/credentials | |
chmod 600 ~/.gem/credentials | |
- name: Publish to RubyGems | |
run: | | |
GEM_FILE=$(ls *.gem | head -1) | |
echo "Publishing $GEM_FILE to RubyGems..." | |
# Get gem name and version for debugging | |
GEM_NAME=$(gem specification "$GEM_FILE" name) | |
GEM_VERSION=$(gem specification "$GEM_FILE" version) | |
echo "Gem name: $GEM_NAME" | |
echo "Gem version: $GEM_VERSION" | |
# Check if gem already exists | |
if gem list "$GEM_NAME" --remote | grep -q "$GEM_NAME"; then | |
echo "Gem $GEM_NAME already exists on RubyGems.org" | |
# Check if this specific version exists | |
if gem list "$GEM_NAME" --remote --exact | grep -q "$GEM_VERSION"; then | |
echo "ERROR: Version $GEM_VERSION of $GEM_NAME already exists!" | |
echo "You need to increment the version number." | |
exit 1 | |
fi | |
fi | |
# Try to push with verbose output | |
gem push "$GEM_FILE" --verbose | |
echo "✓ Successfully published to RubyGems.org" | |
- name: Clean up credentials | |
if: always() | |
run: rm -f ~/.gem/credentials |