如何在CircleCI中运行Ruby 2.7.6或3.1.2(附代码)

255 阅读2分钟

作为一个个体户,我赚钱的方式之一是维护Rails应用程序。就在刚才,我正在为我的一个客户的应用程序工作,我开始用bummr更新宝石,这是我工作的首选工具。

然后我想把Ruby从2.7.5更新到2.7.6。6 我认为这将是一个非常简单的变化,在CircleCI镜像中用config.yml ,取代5

image: circleci/ruby:2.7.6-node-browsers

然而,CircleCI的构建失败了,出现了这个错误:

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

这是因为以circleci/ 为前缀的图像在2021年12月31日被废弃了。现在,它们仍然可以用于运行构建,但它们不再被更新为新版本。

CircleCI现在有更快的以cimg/ 为前缀的 "方便图像"。他们还推出了orbs ,其中内置了缓存。这意味着在config.yml 中需要维护的代码更少。然而,为了在浏览器中运行端到端的测试,它需要比他们以前以-browsers 结尾的图像多一点设置。

因此,除了使用较新的图像,如cimg/ruby:2.7-browsers ,你还需要拉入browser-tools orb并安装你需要的浏览器:

# .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

有了这个设置,我就可以摆脱所有与依赖关系有关的代码了:

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

下面是完整的CircleCI配置文件,其中还包括Postgres和向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

如果你想使用Ruby 3.1.2,请使用cimg/ruby:3.1-browsers 镜像。你也可以在CircleCI开发者网站上搜索其他图像和球体。