Rails + PosgreSQL + RSpec + SimpleCov + Rubocop(pronto) + Heroku
How to use
1. Init Rails app
1.1 Run (skip for exist app)
rails new AppName -d postgresql
1.2 configurate db connect config/database.yml
1.3 create db config for GitLab CI config/database.yml.gitlab
test:
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
host: postgres
username: rails_ci_db
password:
database: rails_ci_db
2. RSpec (tests engine)
2.1 Add to gem file (development, test groups)
group :development, :test do
gem 'rspec-rails'
end
2.2 Configurate
bundle install
rails generate rspec:install
3. SimpleCov (coverage analysis tool)
3.1 Add to gem file (development, test groups)
group :development, :test do
gem 'simplecov', require: false
end
3.2 Install
bundle install
3.3 Configurate
Load and launch SimpleCov at the very top of your spec_helper.rb
example:
require 'simplecov'
SimpleCov.start do
add_filter '/test/'
add_filter '/config/'
add_filter '/spec/'
add_group 'Models', 'app/models'
add_group 'Controllers', 'app/controllers'
add_group 'Libs', 'lib'
add_group 'Helpers', 'app/helpers'
end
# Previous content of test helper now starts here
RSpec.configure do |config|
3.4 Check result
$ rspec
No examples found.
Finished in 0.0006 seconds (files took 0.24259 seconds to load)
0 examples, 0 failures
Coverage report generated for RSpec to /home/dev/dev/test-ci-pg/coverage. 0.0 / 0.0 LOC (100.0%) covered.
4. Rubocop(pronto) (code analysis tool)
4.1 Add to gem file (development, test groups)
group :development, :test do
gem 'pronto'
gem 'pronto-rubocop'
end
4.2 Install
bundle install
4.3 Configurate .rubocop.yml
(example):
AllCops:
DisplayCopNames: true
Exclude:
- 'db/**/*'
- 'spec/**/*'
- 'config/**/*'
- 'bin/**/*'
- 'Gemfile'
- 'Rakefile'
- 'config.ru'
Documentation:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Metrics/BlockLength:
Enabled: false
Lint/UnusedBlockArgument:
Enabled: false
4.4 Check result
$ rubocop
Inspecting 7 files
.......
7 files inspected, no offenses detected
5. GitLab repository settings
5.1 Go to CI/CD Repository settings /settings/ci_cd
and open Secret variables
, add next variables (no protected):
Key | Value | Description |
---|---|---|
GITLAB_PRONTO_API_KEY | API_KEY | [How to get GitLab API key] PlGlA |
HEROKU_STAGING_API_KEY | API_KEY | [How to get Heroku API key] PlHA |
HEROKU_APP_NAME | APP_NAME |
.gitlab-ci.yml
5. Create Git Lab CI/CD Config image: "ruby:2.4"
services:
- postgres:latest
variables:
POSTGRES_DB: rails_ci_db
POSTGRES_USER: rails_ci_db
POSTGRES_PASSWORD: ""
cache:
untracked: true
key: ruby-cache
paths:
- cache/
stages:
- build
- test
- check
- deploy
.rails_template: &rails_definition
before_script:
- mkdir -p cache/apt
- apt-get update -q && apt-get -o dir::cache::archives="cache/apt" install nodejs cmake -yqq
- gem install bundler --no-ri --no-rdoc
- gem install rugged -v '0.26.0'
- bundle install -j $(nproc) --path=cache/bundler
- cp config/database.yml.gitlab config/database.yml
build:
<<: *rails_definition
stage: build
tags:
- rails
script:
- RAILS_ENV=test bundle exec rails about
only:
- master
rspec:
<<: *rails_definition
stage: test
dependencies:
- build
tags:
- rails
- rspec
script:
- RAILS_ENV=test bundle exec rake db:create db:migrate db:seed
- RAILS_ENV=test rspec
artifacts:
paths:
- coverage/
only:
- master
- dev
- /^dev-.*/
- /^fix-.*/
- /^issue-.*/
code review:
<<: *rails_definition
stage: check
dependencies:
- build
tags:
- rails
- pronto
only:
- dev
- /^dev-.*/
- /^fix-.*/
- /^issue-.*/
script:
- PRONTO_GITLAB_API_ENDPOINT="http://gitlab.digital-era.ru/api/v4" PRONTO_GITLAB_API_PRIVATE_TOKEN=$GITLAB_PRONTO_API_KEY bundle exec pronto run -f gitlab -c=origin/master --exit-code
heroku:
stage: deploy
dependencies:
- build
tags:
- rails
script:
- gem install dpl
- dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_STAGING_API_KEY
only:
- master