Publishing a Ruby Gem to the GitHub Package Registry

08 May 2021 in TIL

When trying to publish a new gem to the GitHub package registry I received the following error:

Unable to push RubyGem to Package Registry (The expected resource was not found)

After doing some digging, it looks as though you cannot publish gems with any name you like to the package registry.

If you're using the GITHUB_SECRET token in GitHub Actions, the gem name and repo name must match exactly. If your repo is michael-test-org/my_gem then the gem must be named my_gem.

If you're using a personal access token you have a little more flexibility. You can publish gems that match any repository within your organisation, not just the repository that the workflow is running for.

If you're looking for a workflow to publish gems, here are the relevant steps:

name: Publish Gem
- "v*"
runs-on: ubuntu-18.04
- uses: actions/checkout@v2
- name: Ruby gem cache
uses: actions/cache@v2
path: ${{ github.workspace }}/vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Bundle Setup
run: |
gem install --no-document bundler
bundle config path ${{ github.workspace }}/vendor/bundle
- name: Bundle Install
run: bundle install --jobs 4 --retry 3
- name: Set Credentials
run: |
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:github: Bearer ${GITHUB_TOKEN}\n" > $HOME/.gem/credentials
- name: Publish to GitHub Packages
run: |
export OWNER=$( echo ${{ github.repository }} | cut -d "/" -f 1 )
gem build station.gemspec
gem push --KEY github --host${OWNER} *.gem
gem push *.gem