Skip to content

Instantly share code, notes, and snippets.

@czj
Created May 6, 2019 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czj/c9ada17de443d1c35b7e4e507cedb542 to your computer and use it in GitHub Desktop.
Save czj/c9ada17de443d1c35b7e4e507cedb542 to your computer and use it in GitHub Desktop.
Circle CI 2.1 configuration step for Ruby / Rails / Redis / Postgres workflow with tests + system tests running in parallel
# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2.1
executors:
my-executor:
docker:
# Main image to be used within this configuration
- image: circleci/ruby:2.6.3-node-browsers
environment: # environment variables for database
BUNDLER_VERSION: 2.0.1
DATABASE_URL: "postgres://postgres@localhost:5432/test"
MINITEST_CI: 1
NODE_ENV: test
RACK_ENV: test
RAILS_ENV: test
- image: circleci/redis:3.2-alpine
# Environment variables for database need to match DATABASE_URL
- image: circleci/postgres:10-alpine-ram
environment:
POSTGRES_DB: test
POSTGRES_USER: postgres
references:
prepare_directories: &prepare_directories
run:
name: Restore Rails work directories missing from repository
command: mkdir -p log tmp
upgrade_gem_bundler: &upgrade_gem_bundler
run:
name: Upgrade rubygems and Bundler to latest versions
command: |
gem update --system
gem update bundler
install_dependencies: &install_dependencies
run:
name: Install Linux dependencies
command: sudo apt-get install graphicsmagick
# Install and cache all gems
restore_bundler_cache: &restore_bundler_cache
restore_cache:
keys:
- bundler-2019-05-06-{{ checksum "Gemfile.lock" }}
- bundler-2019-05-06
bundle_install: &bundle_install
run:
name: Install gems
command: bundle install --deployment --jobs=4 --retry=3
save_bundler_cache: &save_bundler_cache
save_cache:
key: bundler-2019-05-06-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
restore_yarn_cache: &restore_yarn_cache
restore_cache:
keys:
- yarn-2019-05-06-{{ checksum "package.json" }}
- yarn-2019-05-06
yarn_install: &yarn_install
run:
name: Install NPM packages
command: yarn install
save_yarn_cache: &save_yarn_cache
save_cache:
key: yarn-2019-05-06-{{ checksum "package.json" }}
paths:
- node_modules
restore_assets_cache: &restore_assets_cache
restore_cache:
keys:
- sprockets-webpacker-2019-05-06-{{ .Branch }}
- sprockets-webpacker-2019-05-06
precompile_assets: &precompile_assets
run: bundle exec rake assets:precompile
save_assets_cache: &save_assets_cache
save_cache:
key: sprockets-webpacker-2019-05-06-{{ .Branch }}
paths:
- public/assets
- public/packs
- public/packs-test
- tmp/cache/assets
- tmp/cache/webpacker
init_db: &init_db
run:
name: Initialize test database
command: bundle exec rake db:test:prepare
test: &test
run:
name: Run non-system tests
command: bundle exec rake test
test_system: &test_system
run:
name: Run system tests on Chrome
command: CAPYBARA_DRIVER=chrome bundle exec rake test:system
# Firefox is not reliable enough, we have way too much false positives
# We need to run its tests in a separate process, for speed and clarity
# - run:
# name: Run system tests on Firefox
# command: CAPYBARA_DRIVER=firefox bundle exec rake test:system
jobs:
prepare:
executor: my-executor
steps:
- checkout
- <<: *prepare_directories
- <<: *upgrade_gem_bundler
- <<: *restore_bundler_cache
- <<: *bundle_install
- <<: *save_bundler_cache
- <<: *restore_yarn_cache
- <<: *yarn_install
- <<: *save_yarn_cache
- <<: *restore_assets_cache
- <<: *precompile_assets
- <<: *save_assets_cache
tests:
executor: my-executor
steps:
- checkout
- <<: *prepare_directories
- <<: *upgrade_gem_bundler
- <<: *restore_bundler_cache
- <<: *bundle_install
- <<: *save_bundler_cache
- <<: *restore_yarn_cache
- <<: *yarn_install
- <<: *save_yarn_cache
- <<: *restore_assets_cache
- <<: *precompile_assets
- <<: *save_assets_cache
- <<: *install_dependencies
- <<: *init_db
- <<: *test
system-tests:
executor: my-executor
steps:
- checkout
- <<: *prepare_directories
- <<: *upgrade_gem_bundler
- <<: *restore_bundler_cache
- <<: *bundle_install
- <<: *save_bundler_cache
- <<: *restore_yarn_cache
- <<: *yarn_install
- <<: *save_yarn_cache
- <<: *restore_assets_cache
- <<: *precompile_assets
- <<: *save_assets_cache
- <<: *install_dependencies
- <<: *init_db
- <<: *test_system
workflows:
"Run tests":
jobs:
- prepare
- tests:
requires:
- prepare
- system-tests:
requires:
- prepare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment