If you introduce CircleCI to Rails and push it to GitHub, tests etc. will run automatically.
Rails app creation (created with MySQL)
$ rails new circleci_practice -d mysql
Create a suitable CRUD with scaffold.
circleci_practic $ rails g scaffold users name:string email:string
Add rspec to Gemfile and bundle install
Try writing one suitable RSpec.
Create a .circleci directory and create a config.yml
You cannot connect to MySQL on Docker as it is, so edit the host part. For the time being, I think the following feeling is fine.
test:
<<: *default
database: circleci_parctice_test
# ↓ Add this
host: <%= ENV['DATABASE_HOST'] } %>
version: 2
jobs:
test:
docker:
- image: circleci/ruby:2.7.1-node
environment:
RAILS_ENV: test
DATABASE_HOST: 127.0.0.1
- image: circleci/mysql:5.7
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_ROOT_PASSWORD: ''
MYSQL_DATABASE: circleci_parctice_test
working_directory: ~/circleci_parctice
steps:
- checkout
- run:
name: "bundle install"
command: bundle install --path vendor/bundle
#Wait until you can connect to MySQL
- run:
name: "waiting DB start"
command: dockerize -wait tcp://127.0.0.1:3306 -timeout 1m
- run:
name: "initialize DB"
command: |
bundle exec rake db:create
bundle exec rake db:migrate
- run:
name: RSpec
command: bundle exec rspec
workflows:
version: 2
workflows:
jobs:
- test
If you push with this, CI will run automatically.
I think it would be nice to add rubocop or slim-lint. It's faster if you use cache with yarn install or bundle install.
Recommended Posts