Are you eager to elevate your security skills and safeguard your applications
against cyber threats? I created a Rails Security course is designed
specifically for developers like you who aim to build robust, secure Rails
applications!
Buy my course: Security for Rails Developers.
I started to use Travis CI for continous integration. They have a free plan for open source which makes a good opportunity to try out the service.
Travis supports various runtimes and databases and I was going to test my app against ruby 1.9 and ruby 2 and 3 different databases: sqlite, mysql, postgres. To setup these runtimes I created a travis.yml config file like this:
Buy my course: Security for Rails Developers.
language: ruby
rvm:
- 2.0.0
- 1.9.3
env:
- DB=sqlite
- DB=mysql
- DB=postgresql
DB
environment variable which we can use to determine which gem to bundle:
require 'yaml'
env = ENV["RAILS_ENV"] || 'development'
dbconfig = File.expand_path("../config/database.yml", __FILE__)
raise "You need to configure config/database.yml first" unless File.exists?(dbconfig)
require 'erb'
config = YAML.load(ERB.new(File.read(dbconfig)).result)
environment = config[env]
adapter = environment['adapter'] if environment
raise "Please set an adapter in database.yml for #{env} environment" if adapter.nil?
case adapter
when 'sqlite3'
gem 'sqlite3'
when 'postgresql'
gem 'pg'
when 'mysql2'
gem 'mysql2'
else
raise "Not supported database adapter: #{adapter}"
end
ENV['DB']
variable in the yaml file.
I don't have a database.yml in source control just an example for the different database options and I thought the best way would be to keep all the travis related db config in a separate file so I created a database.travis.yml file:
sqlite: &sqlite
adapter: sqlite3
database: db/<%= ENV['RAILS_ENV'] %>.sqlite3
mysql: &mysql
adapter: mysql2
username: root
password:
database: invoicer_<%= ENV['RAILS_ENV'] %>
postgresql: &postgresql
adapter: postgresql
username: postgres
password:
database: invoicer_<%= ENV['RAILS_ENV'] %>
min_messages: ERROR
defaults: &defaults
pool: 5
timeout: 5000
host: localhost
<<: *<%= ENV['DB'] || "sqlite" %>
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
language: ruby
rvm:
- 2.0.0
- 1.9.3
env:
- DB=sqlite
- DB=mysql
- DB=postgresql
before_install: cp config/database.travis.yml config/database.yml
script:
- export RAILS_ENV=test
- bundle exec rake db:create db:migrate
- bundle exec rake db:test:prepare
Or follow me on Twitter
I run an indie startup providing vulnerability scanning for your Ruby on Rails app.
It is free to use at the moment, and I am grateful for any feedback about it.If you would like to give it a spin, you can do it here: Vulnerability Scanning for your Ruby on Rails app!