CircleCI, PostgreSQL, ActiveRecord, Sinatra

If you enjoy this article you may be interested in the book I am working on called Building and Deploying Crypto Trading Bots. You can find more info about it here.

Let's memoize how to set up CircleCI, Postgres  and Sinatra!

Assumed Dependencies:

  1. ActiveRecord
  2. PostgreSQL

Ensure that your database.yml has a test key setup because the config runs with RACK_ENV and APP_ENV in test.

# Other configs
	
test:
   <<: *default
   database: test_development
config/database.yml

Then here is the configuration file:

version: 2.1

executors:
  default:
    working_directory: ~/app
    docker:
      - image: circleci/ruby:2.5.5
        environment:
          RACK_ENV: test
          APP_ENV: test
      - image: circleci/postgres:9.6
        environment:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: app_db

jobs:
  build:
    executor: default
    steps:
      - checkout
      - run: |
            sudo apt-get update
            sudo apt-get install -y postgresql-client
         # Restore bundle cache
      - restore_cache:
          name: gems
          key: app-{{ checksum "Gemfile.lock" }}

      - run:
          name: install gems
          command: |
            gem install bundler:2.0.2
            bundle install --jobs=5 --path vendor/bundle
      - save_cache:
          name: save gem cache
          key: app-{{ checksum "Gemfile.lock" }}
          paths:
              - vendor/bundle
      - run:
          name: db
          command: bundle exec rake db:drop db:create db:schema:load --trace
      - run:
          name: specs
          command: |
            bundle exec rspec --profile 10 --format progress

workflows:
   version: 2
   integration:
      jobs:
         - build

Testing Config Locally

If you add the above configuration file to your application, test the changes locally using the following steps to ensure the config is correct and functional.

# Sets up CircleCI to run on your local machine
# You will need to grab a app token
circleci setup 

# validate config 
circleci config validate

# generate process yml
circleci config process .circleci/config.yml > process.yml

# ignore
 echo "process.yml" >> .gitignore
 
# execute
circleci local execute -c process.yml --job build

Have fun.