How to Run Ruby 2.7.6 or 3.1.2 in CircleCI

Published

One of the ways I earn money as a solopreneur is by maintaining Rails applications. Just now I was working on one of my clients’ apps, and I started by updating gems with bummr, my preferred tool for the job.

Then I wanted to update Ruby from 2.7.5 to 2.7.6. I thought this would be a very simple change, replacing the 5 with a 6 in the CircleCI image defined in config.yml:

image: circleci/ruby:2.7.6-node-browsers

However, the CircleCI build failed with this error:

Warning: No authentication provided, using CircleCI credentials for 
pulls from Docker Hub.
  image cache not found on this host, downloading circleci/ruby:2.7.6-node-browsers

Error response from daemon: manifest for circleci/ruby:2.7.6-node-browsers 
not found: manifest unknown: manifest unknown

That’s because images that start with the circleci/ prefix were deprecated on December 31, 2021. For now, they’re still available for running builds, but they’re not being updated with new versions anymore.

CircleCI now has faster “convenience images” with the cimg/ prefix. They also introduced orbs, which come with caching built in. This means less code overall to maintain in config.yml. However, to run end-to-end tests in a browser, it requires a tiny bit more setup than their previous images that ended with -browsers.

So, in addition to using a newer image like cimg/ruby:2.7-browsers, you also need to pull in the browser-tools orb and install the browser(s) you need:

# .circleci/config.yml
version: 2.1

orbs:
  ruby: circleci/ruby@1.8.0
  node: circleci/node@5.0.2
  browser-tools: circleci/browser-tools@1.3.0

jobs:
  build:
    docker:
      - image: cimg/ruby:2.7-browsers
    environment:
      BUNDLE_JOBS: "4"
      BUNDLE_RETRY: "3"
    steps:
      - browser-tools/install-chrome
      - browser-tools/install-chromedriver
      - checkout
      - ruby/install-deps

With this setup, I was able to get rid of all the code related to dependencies:

steps:
  - restore-cache:
      key: project-name-{{ checksum "Gemfile.lock" }}
  - run:
      name: Install dependencies
      command: |
        gem install bundler
        bundle install --deployment --jobs=4 --retry=3 --without development production --path vendor/bundle
        sudo apt update
        sudo apt-get install postgresql-client
  - save-cache:
      key: project-name-{{ checksum "Gemfile.lock" }}
      paths:
        - vendor/bundle

Here’s the full CircleCI config file that also includes Postgres and reporting test results to Code Climate:

# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2.1

orbs:
  ruby: circleci/ruby@1.8.0
  node: circleci/node@5.0.2
  browser-tools: circleci/browser-tools@1.3.0

jobs:
  build:
    docker:
      # Specify the Ruby version you desire here
      - image: cimg/ruby:2.7-browsers

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      - image: cimg/postgres:10.20
        environment:
          POSTGRES_USER: circleci
          POSTGRES_DB: project_name_test
          POSTGRES_PASSWORD: ""

    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: 127.0.0.1
      PGUSER: circleci
      PGPASSWORD: ""
      RAILS_ENV: test
      CC_TEST_REPORTER_ID: some_long_token
      COVERAGE: true

    steps:
      - browser-tools/install-chrome
      - browser-tools/install-chromedriver
      - checkout
      - ruby/install-deps

      - run:
          name: Wait for db
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      - run:
          name: Install Code Climate Test Reporter
          command: |
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
            chmod +x ./cc-test-reporter

      - run:
          name: Test Setup
          command: |
            cp config/application.example.yml config/application.yml
            psql -U circleci -q -d project_name_test -f db/structure.sql -h localhost -p 5432
            bundle exec rake assets:precompile

      - run:
          name: Run Tests
          command: |
            mkdir /tmp/test-results
            ./cc-test-reporter before-build

            bundle exec rspec --format progress

      - run:
          name: Upload Test Results to Code Climate
          command: |
            ./cc-test-reporter format-coverage -t simplecov $CIRCLE_ARTIFACTS/coverage/.resultset.json
            ./cc-test-reporter upload-coverage

      # collect reports
      - store_test_results:
          path: /tmp/test-results
      - store_artifacts:
          path: /tmp/test-results
          destination: test-results

If you want to use Ruby 3.1.2, use the cimg/ruby:3.1-browsers image. You can also search for other images and orbs on the CircleCI Developer site.