DX Heroes logo
#guide
#ruby
#sdk

Release and publish a new Ruby SDK version automatically: a step-by-step guide

Length: 

8 min

Published: 

September 21, 2023

Release and publish a new Ruby SDK version automatically: a step-by-step guide

Keeping a changelog tidy, tagging a new version of an SDK, then pushing it to a package repository. These are repetitive chores, and every one of them can be automated. That gives your developers back time they would otherwise spend on bookkeeping.

This tutorial shows you how to set that up. You will go through these steps:

  1. Set up a GitHub Action to release a new SDK version.
  2. Publish a new SDK version:
    • Create a RubyGems account.
    • Create a RubyGems API key and store it securely in GitHub.
    • Set up a GitHub workflow that auto-publishes each new SDK version.

Prerequisites

You already have a working SDK and you have pushed it to a GitHub repository.

Release a new version

Owner + Developer: Set up a GitHub Action to release new SDK versions

To avoid updating the changelog and tagging versions by hand, we recommend the GitHub Action release-please. It tracks code changes since the last release and keeps a pull request open with the changelog and version bump, based on your commit messages. When you want to ship a new version, you merge that pull request.

Set up the Action like this:

Owner:

  1. In your GitHub repository, go to Settings > Actions > General and check the option "Allow GitHub Actions to create and approve pull requests".

Developer:

  1. In the root folder of your project, create a directory .github/workflows/. This is where GitHub Action configuration files belong.
  2. In this directory, create a new YAML file. Name it release-please.yml.
  3. Copy the following code into your configuration file:
# This workflow opens and updates a pull request with a new package version
# based on code changes.

# The pull request updates the version in version.rb, updates the changelog
# and creates release tags.

# https://github.com/marketplace/actions/release-please-action

on:
  push:
    branches:
      - master

permissions:
  contents: write
  pull-requests: write

name: release-please

jobs:
  release-please:
    runs-on: ubuntu-22.04
    steps:
      - uses: google-github-actions/release-please-action@v3.7.10
        with:
          release-type: ruby
          package-name: release-please-action
          version-file: "lib/your_gem_name/version.rb"
          pull-request-title-pattern: "chore(release): ${version}"
          pull-request-header: ":robot: Merge this PR to release a new version"

Change the line version-file: "lib/your_gem_name/version.rb" so it points to your actual version.rb file. If your main branch is named something other than master (for example main), change it on Line 13.

  1. If you have released the SDK before, make sure the commit with the latest release has a version tag attached (in the format "v2.1.1") and that you have pushed the tag to GitHub. release-please reads this tag to work out the next version number. Without such a tag, it suggests "v1.0.0" as the next version.

  2. Commit these changes and push them to GitHub. The workflow is now set up. The next time someone adds something releasable with a conventional commit message (a fix: or feat: prefix), release-please opens a pull request. As you add more code, it updates the pull request automatically. When you are ready to ship a new version of your SDK, merge the pull request. The version is released and the updated SDK is published to RubyGems.

release-please follows the Semantic Versioning (SemVer) specification, so it picks version numbers based on how significant your changes are. A breaking change bumps the major version, a new feature bumps the minor version, and a bug fix bumps the patch version. That makes it easy for your users to see how much each new version of your SDK changes.

For release-please to read the significance of your changes and pick the right version number, your commit messages need to follow the Conventional Commits format. Use a fixed prefix: "feat: add new feature" for a new feature, "fix: resolve bug" for a bug fix, "chore: update dependencies" for a non-code change. Stay consistent and release-please generates correct version numbers and changelogs. You can still edit the changelog by hand in the open pull request before you release.

Publish a new version

Owner: Create a RubyGems account, get an API key and store it securely in GitHub

  1. Before you publish your SDK for the first time, create a RubyGems account here.
  2. Go to Settings > API keys and enter your password when prompted. Click "New API key". Choose a name for the key, make sure the "Push rubygem" scope is selected, and create the key.
  3. Copy the API key.
  4. Open your project on GitHub. Go to Settings > Security > Secrets and Variables > Actions and click "New repository secret".
  5. Enter RUBYGEMS_API_KEY in the Name field.
  6. Enter the API key from Step 3 in the Secret field. Click "Add secret" to confirm.

Developer: Automate SDK publishing with a GitHub workflow

You can set up a GitHub workflow that publishes your SDK to RubyGems automatically once you push a release commit to the GitHub origin repository:

  1. In your project, go to the folder .github/workflows (create it if it does not exist yet) and create a new YAML file. Name it for example rubygems-publish.yml.
  2. Copy the following code into your configuration file:
# This workflow will publish a gem to rubygems.org when a release is created

name: Publish Gem

on:
  push:
    branches:
      - master

jobs:
  publish:
    if: contains(github.event.head_commit.message, 'chore(release)')
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v3.5.3
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "2.7"
      - run: gem build
      # add RubyGems API key into the credentials file and update permissions
      - run: |
          cat << EOF > ~/.gem/credentials
          ---
          :rubygems_api_key: ${RUBYGEMS_API_KEY}
          EOF
          chmod 0600 ~/.gem/credentials
        env:
          RUBYGEMS_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
      - run: gem push *.gem

If your main branch is named something other than master (for example main), change the name on Line 8.

  1. Commit this new file to your project's main branch and push the changes to the GitHub origin repository.

The GitHub Action is now ready. For it to work, you first need to add the RUBYGEMS_API_KEY to GitHub secrets.

The Action runs when you push a commit to your main branch with a commit message that contains the phrase chore(release). If you followed the steps in the "Release a new version" section above, the publish Action fires the moment you merge the pull request that release-please opened.

Conclusion

You now have automated tools that release a new version of a Ruby SDK and publish it to RubyGems. With these steps in place, you free up time and budget that your team can spend on writing code instead.

If you need a hand with SDK development or automation, our team is ready to help. We can tighten your SDK development process, set up the automation, and save you time and money along the way.

Get in touch and let's talk about your SDK.


Need an automated release for another technology?


You might also be interested in:

Want to stay one step ahead?

Don't miss our best insights. No spam, just practical analyses, invitations to exclusive events, and podcast summaries delivered straight to your inbox.