Rails with Wercker and Heroku
Wercker is an incredibly versatile CI/CD Automation Platform. The fact that it’s based on Docker means that all Docker containers are available for your CI environment.
TLDR; Here’s the configuration for running Rails automated tests (including capybara features) using Wercker.
box: guzart/rails-app:020407070021 build: services: - id: mysql tag: 5.7 env: MYSQL_ROOT_PASSWORD: 123SecretPassword MYSQL_DATABASE: fancy_app_test steps: # https://github.com/wercker/step-npm-install/blob/master/run.sh - script: name: yarn-install cwd: web/ code: | mkdir -p "$WERCKER_CACHE_DIR/wercker/yarn" yarn config set cache-folder "$WERCKER_CACHE_DIR/wercker/npm" yarn install - wercker/bundle-install - script: name: wait-for-mysql code: | curl -O https://raw.githubusercontent.com/vishnubob/wait-for-it/55c54a5abdfb32637b563b28cc088314b162195e/wait-for-it.sh chmod +x ./wait-for-it.sh ./wait-for-it.sh $MYSQL_PORT_3306_TCP_ADDR:$MYSQL_PORT_3306_TCP_PORT -t 30 - script: name: rails-setup-database code: | export FANCY_APP_DATABASE_HOST="$MYSQL_PORT_3306_TCP_ADDR" export FANCY_APP_DATABASE_PASSWORD="123SecretPassword" export FANCY_APP_DATABASE_PORT="$MYSQL_PORT_3306_TCP_PORT" export FANCY_APP_DATABASE_USERNAME="root" export FANCY_APP_DATABASE="fancy_app_test" bundle exec rails db:migrate - script: name: rspec code: xvfb-run -a bundle exec rspec
We start by defining the container to use, if you are running exclusively a rails application you can use the ruby:2.4 container, however since I am running a Rails application in conjuction with a React SPA, I created my own container that includes Ruby, Node.js and Yarn.
box: guzart/rails-app:020407070021
Next, we start the declaration of the build pipeline; the build pipeline will be where we get the container ready to run the tests and then run the tests.
Add any database services that your application needs, in this example we add a MySQL database.
build: services: - id: mysql tag: 5.7 env: MYSQL_ROOT_PASSWORD: 123SecretPassword MYSQL_DATABASE: fancy_app_test
After declaring the services needed by our application, we start the declaration of the steps that are needed to get the application ready for running the tests.
Because in my project there is a React SPA application in the web/ subdirectory, we install the SPA node modules using Yarn. Notice we change the CWD (current working dictory) to the web subdirectory.
# https://github.com/wercker/step-npm-install/blob/master/run.sh - script: name: yarn-install cwd: web/ code: | mkdir -p "$WERCKER_CACHE_DIR/wercker/yarn" yarn config set cache-folder "$WERCKER_CACHE_DIR/wercker/npm" yarn install
Then install the bundle gems, using a pre-existing Step.
We must make sure that the MySQL service is up and running before continuing with the next steps, We’ve run the dependency installations first to give the MySQL service time to fire up the engines.
- script: name: wait-for-mysql code: | curl -O https://raw.githubusercontent.com/vishnubob/wait-for-it/55c54a5abdfb32637b563b28cc088314b162195e/wait-for-it.sh chmod +x ./wait-for-it.sh ./wait-for-it.sh $MYSQL_PORT_3306_TCP_ADDR:$MYSQL_PORT_3306_TCP_PORT -t 30
Now we can create the Rails application database and run the migrations.
- script: name: rails-setup-database code: | export FANCY_APP_DATABASE_HOST="$MYSQL_PORT_3306_TCP_ADDR" export FANCY_APP_DATABASE_PASSWORD="123SecretPassword" export FANCY_APP_DATABASE_PORT="$MYSQL_PORT_3306_TCP_PORT" export FANCY_APP_DATABASE_USERNAME="root" export FANCY_APP_DATABASE="fancy_app_test" bundle exec rails db:migrate
This steps assumes that you have a database.yml with the following configuration:
default: &default adapter: mysql2 collation: utf8mb4_unicode_ci encoding: utf8mb4 host: password: pool: 5 port: socket: /tmp/mysql.sock username: test:
And finally we run the tests using the xvfb-run utility.
- script: name: rspec code: xvfb-run -a bundle exec rspec