Skip to content

Instantly share code, notes, and snippets.

@hmcfletch
Created September 21, 2018 19:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hmcfletch/1dfa61f6435c10e9556bbe2cc3b086ef to your computer and use it in GitHub Desktop.
Save hmcfletch/1dfa61f6435c10e9556bbe2cc3b086ef to your computer and use it in GitHub Desktop.
An example Circle CI workflow for a Ruby on Rails app
version: 2
jobs:
checkout_and_deps:
docker:
- image: circleci/ruby:2.5.1-node-browsers
steps:
- checkout
- run:
name: Get the Gemfile.lock from the master branch
command: |
git show master:Gemfile.lock > MasterGemfile.lock
- restore_cache:
keys:
- v1-dependencies-gem-{{ checksum "Gemfile.lock" }}
- v1-dependencies-gem-{{ checksum "MasterGemfile.lock" }}
- v1-dependencies-gem-
- run:
name: bundle install
command: |
bundle check --path vendor/bundle || bundle install --jobs=4 --retry=3 --path vendor/bundle
- run:
name: Dependencies security audit
command: |
bundle exec bundle-audit check --update
- run:
name: Clean up gems before we save
command: |
bundle clean
- save_cache:
key: v1-dependencies-gem-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
- restore_cache:
keys:
- v1-rubocop-cache-{{ checksum ".rubocop.yml" }}-{{ .Branch }}
- v1-rubocop-cache-{{ checksum ".rubocop.yml" }}-master
- v1-rubocop-cache-{{ checksum ".rubocop.yml" }}
- run:
name: Rubocop
command: bundle exec rubocop --parallel
- save_cache:
key: v1-rubocop-cache-{{ checksum ".rubocop.yml" }}-{{ .Branch }}-{{ epoch }}
paths:
- ../.cache/rubocop_cache
run_tests:
docker:
- image: circleci/ruby:2.5.1-node-browsers
environment:
RAILS_ENV: test
PGHOST: 127.0.0.1
PGUSER: root
PARALLEL_TEST_PROCESSORS: 4
- image: redis:3.0
- image: circleci/postgres:10.3-ram
environment:
POSTGRES_USER: root
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-gem-{{ checksum "Gemfile.lock" }}
- run:
name: Setup bundler path
command: |
bundle check --path vendor/bundle
- restore_cache:
keys:
- v1-bootsnap-cache-{{ checksum "Gemfile.lock" }}-{{ .Branch }}
- v1-bootsnap-cache-{{ checksum "Gemfile.lock" }}-master
# must be in run_tests step because requires a connection to Redis
- run:
name: Eager load classes to check for issues
command: |
bundle exec rails runner 'Rails.application.eager_load!'
- run:
name: Validate clockwork.rb
command: |
bundle exec ruby ./clockwork.rb
- run:
name: Wait for postgres
command: dockerize -wait tcp://localhost:5432 -timeout 1m
- run:
name: Wait for redis
command: dockerize -wait tcp://localhost:6379 -timeout 1m
- run:
name: Install Postgres Client
command: |
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt update
sudo apt install postgresql-client-10
- run:
name: Database Checksum
command: |
find db -type f -exec md5sum {} \; | sort -k 2 > db_dir_checksums.txt
echo $PARALLEL_TEST_PROCESSORS >> db_dir_checksums.txt
psql postgres -A -t -c 'SELECT version()' >> db_dir_checksums.txt
- run:
name: Cat db_dir_checksums.txt
command: |
cat db_dir_checksums.txt
- restore_cache:
keys:
- v1-database-schema-{{ checksum "db_dir_checksums.txt" }}
- run:
name: Database Setup
command: |
if [ -e postgres_dump.sql ]
then
echo "Restoring databases dump"
psql -U postgres -f postgres_dump.sql
else
echo "Setting up databases"
bundle exec rake parallel:setup
echo "Dumping databases"
pg_dumpall > postgres_dump.sql
fi
- save_cache:
key: v1-database-schema-{{ checksum "db_dir_checksums.txt" }}
paths:
- postgres_dump.sql
- run:
name: Create test results directory
command: |
mkdir tmp/test-results
- restore_cache:
keys:
- v1-test-times-{{ .Branch }}
- v1-test-times-master
# run tests!
- run:
name: Run specs
command: |
bundle exec parallel_test \
-t rspec \
--runtime-log tmp/test-results/parallel_runtime_rspec.log \
-- --format progress \
--format RspecJunitFormatter \
--out tmp/test-results/rspec.xml \
--format ParallelTests::RSpec::RuntimeLogger \
--out tmp/test-results/parallel_runtime_rspec.log \
-- $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
- save_cache:
key: v1-test-times-{{ .Branch }}-{{ epoch }}
paths:
- tmp/test-results/parallel_runtime_rspec.log
- save_cache:
key: v1-bootsnap-cache-{{ checksum "Gemfile.lock" }}-{{ .Branch }}-{{ epoch }}
paths:
- tmp/bootsnap_cache
# collect reports
- store_test_results:
path: tmp/test-results
- store_artifacts:
path: tmp/test-results
destination: test-results
- store_artifacts:
path: coverage
workflows:
version: 2
test_and_deploy:
jobs:
- checkout_and_deps
- run_tests:
requires:
- checkout_and_deps
@jeremybdk
Copy link

Thanks for the great blog post, I was wondering if you are still using this? I'm running into an issue with an invalid rspec.xml at the end of my rspec tests.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment