CircleCI 2.0 Config for Rails application deploying to AWS
TweetWith CircleCI forcing everyone to migrate to their 2.0 platform before August, I though to jump on the bandwagon well in advance. Here’s the setup necessary to migrate an existing rails app to CircleCI 2.0. There’s config added here to support postgres and redis databases if your app uses them. Additonaly, I’ve used the workflows feature to deploy the application to Elastic Beanstalk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 2 | |
jobs: | |
build: | |
parallelism: 3 | |
working_directory: ~/foo/bar | |
docker: | |
- image: circleci/ruby:2.3-node-browsers | |
- image: circleci/postgres:9.4 | |
- image: circleci/redis:3.2 | |
- image: circleci/rabbitmq:3.6.6 | |
steps: | |
- checkout | |
- add_ssh_keys | |
- restore_cache: | |
keys: | |
- foobar-dependency-cache-{{ .Branch }}-{{ checksum "Gemfile.lock" }} | |
- foobar-dependency-cache-{{ .Branch }}- | |
- foobar-dependency-cache- | |
- run: | |
name: Export env | |
command: echo -e "export RAILS_ENV=test\nexport RACK_ENV=test" >> $BASH_ENV | |
- run: | |
name: Bundle Install | |
command: | | |
bundle config without development:production | |
bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=3 --retry=3 | |
- save_cache: | |
key: foobar-dependency-cache-{{ .Branch }}-{{ checksum "Gemfile.lock" }} | |
paths: | |
- vendor/bundle | |
- run: | |
name: Wait for DB | |
command: dockerize -wait tcp://localhost:5432 -timeout 1m | |
- run: | |
name: Wait for Redis | |
command: dockerize -wait tcp://localhost:6379 -timeout 1m | |
- run: | |
name: Wait for Rabbit | |
command: dockerize -wait tcp://localhost:5672 -timeout 1m | |
- run: | |
name: Database setup | |
command: | | |
mkdir -p config && echo 'test: | |
adapter: postgresql | |
encoding: unicode | |
database: foobar_test | |
pool: 5 | |
username: postgres | |
host: localhost | |
' > config/database.yml | |
export RAILS_ENV="test" | |
export RACK_ENV="test" | |
bundle exec rake db:create db:schema:load --trace | |
- type: shell | |
command: | | |
bundle exec rspec --profile 10 \ | |
--format RspecJunitFormatter \ | |
--out test_results/rspec.xml \ | |
--format progress \ | |
$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings) | |
- store_test_results: | |
path: test_results | |
deploy: | |
machine: | |
enabled: true | |
working_directory: ~/foo/bar | |
steps: | |
- checkout | |
- add_ssh_keys | |
- run: | |
name: Deploy | |
command: | | |
sudo apt-get -y -qq update | |
sudo apt-get install python-pip python-dev build-essential | |
sudo pip install awsebcli --upgrade | |
echo $EB_KEY | sed 's/\\n/\n/g' > ~/.ssh/aws-eb-key.pem | |
if [[ "${CIRCLE_BRANCH}" == master ]]; then | |
eb use foobar-dev | |
elif [[ "${CIRCLE_BRANCH}" == release ]]; then | |
eb use foobar-prod | |
fi | |
eb deploy -l $CIRCLE_SHA1 --timeout 15 | |
workflows: | |
version: 2 | |
build-and-deploy: | |
jobs: | |
- build: | |
filters: | |
branches: | |
only: | |
- release | |
- master | |
- /.*fix.*/ | |
- /.*feature.*/ | |
- deploy: | |
requires: | |
- build | |
filters: | |
branches: | |
only: | |
- release | |
- master | |