Skip to content

Instantly share code, notes, and snippets.

@henrypoydar
Created November 20, 2017 00:19
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save henrypoydar/3685d1cd38bd591bbe566e5053c229d6 to your computer and use it in GitHub Desktop.
Save henrypoydar/3685d1cd38bd591bbe566e5053c229d6 to your computer and use it in GitHub Desktop.
Rails 5.1 CircleCI 2.0 Configuration
version: 2
jobs:
build:
environment:
working_directory: ~/circleci-myapp
docker:
- image: circleci/ruby:2.4.2-node-browsers
environment:
CC_TEST_REPORTER_ID: XXXYYY
RAILS_ENV: test
TEST_REPORT_PATH: "test/reports"
- image: circleci/postgres:9.6.2-alpine
environment:
POSTGRES_USER: circleci
parallelism: 2
steps:
- checkout
# Restore bundle cache
- type: cache-restore
key: myapp-bundle-{{ checksum "Gemfile.lock" }}
# Restore yarn cache
- type: cache-restore
key: myapp-yarn-{{ checksum "yarn.lock" }}
# Install gem dependencies
- run: bundle install --path vendor/bundle
# Install Javascript dependencies
- run: bin/yarn install
# Store bundle cache
- type: cache-save
key: myapp-bundle-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
# Store yarn cache
- type: cache-save
key: myapp-yarn-{{ checksum "yarn.lock" }}
paths:
- ~/.yarn-cache
# Database setup
- run: bin/rails db:create
- run: bin/rails db:schema:load
# Code Climate setup
- run: curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- run: chmod +x ./cc-test-reporter
- run: sudo apt-get -y -qq install awscli
- run: aws s3 rm s3://myapp/coverage/$CIRCLE_PREVIOUS_BUILD_NUM --recursive
# Run Ruby unit tests and app integration tests in parallel
- type: shell
command: |
bin/rails test $(circleci tests glob "test/**/*_test.rb" | circleci tests split --split-by=timings)
# Run Javascript unit tests
- run: yarn test
# Code Climate coverage, split for parallel nodes
- run: ./cc-test-reporter format-coverage --output "coverage/codeclimate.$CIRCLE_NODE_INDEX.json"
- run: aws s3 sync coverage/ "s3://myapp/coverage/$CIRCLE_BUILD_NUM"
# Run security scan
- run: bundle exec brakeman
# Save artifacts
- type: store_test_results
path: test/reports
# `deploy` runs only on node 0 after parallel steps have finished
# Upload results from all parallel nodes to Code Climate
- deploy:
command: |
aws s3 sync "s3://myapp/coverage/$CIRCLE_BUILD_NUM" coverage/
./cc-test-reporter sum-coverage --output - --parts $CIRCLE_NODE_TOTAL coverage/codeclimate.*.json | ./cc-test-reporter upload-coverage --input -
@monfresh
Copy link

Thanks for this gist. When I run it in Circle CI 2.0, I get A client error (AccessDenied) occurred when calling the ListObjects operation: Access Denied. I tried running the same aws command locally with the same access key id and secret and it works. What did you have to do to get it to work in Circle CI? I set the following environment variables in the Circle CI web UI:

AWS_ACCESS_KEY_ID
AWS_DEFAULT_REGION
AWS_REGION
AWS_SECRET_ACCESS_KEY

Thanks!

@monfresh
Copy link

I fixed it by replacing sudo apt-get -y -qq install awscli with this:

- run:
    name: Install AWS CLI
    command: |
      sudo apt-get update
      sudo apt-get install python-pip python-dev jq
      sudo pip install awscli

@KarllosSouza
Copy link

@monfresh thank you very much for sharing!

@HoyaBoya
Copy link

HoyaBoya commented Aug 2, 2018

@monfresh nice comment. I needed to do the same steps with sudo pip install awscli as well to get around a seek error in my upload of coverage results.

upload failed: coverage/.resultset.json.lock to s3://virgent.codeclimate/coverage/1134/.resultset.json.lock seek() takes 2 positional arguments but 3 were given

Nice contribution @henrypoydar.

@Bilal1122
Copy link

Bilal1122 commented Aug 28, 2018

@monfresh @HoyaBoya i am having an error at this line - run: ./cc-test-reporter format-coverage --output "coverage/codeclimate.$CIRCLE_NODE_INDEX.json" at this line and it says Error: could not find any viable formatter. available formatters: clover, cobertura, coverage.py, excoveralls, gcov, gocov, jacoco, lcov, simplecov

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