※完全自分用メモです。
かなり簡潔に書いています。
1.CircleCIの導入
ルートディレクトリ直下に「.circleci」というディレクトリを作成し、これ以下に「config.yml」というファイルを作成。
また、configディレクトリ以下に「database.yml.ci」というファイルを作成。
config.yml
version: 2
jobs:
build:
docker:
- image: circleci/ruby:2.5.3-node-browsers
environment:
- BUNDLER_VERSION: 2.0.2
- RAILS_ENV: 'test'
- image: circleci/mysql:5.7
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
- MYSQL_ROOT_HOST: '127.0.0.1'
working_directory: ~/sample_app #自身のアプリディレクトリ名を設定
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
- v1-dependencies-
- run:
name: install dependencies
command: |
gem install bundler -v 2.0.2
bundle install --jobs=4 --retry=3 --path vendor/bundle
- save_cache:
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
# Database setup
- run: mv ./config/database.yml.ci ./config/database.yml
# Database setup
- run:
name: Databasesetup
command: |
bundle exec rake db:create
bundle exec rake db:schema:load
#webpack
- run:
name: yarn Install
command: yarn install
- run: bundle exec bin/webpack
# run tests!
- run:
name: Run rspec
command: |
mkdir /tmp/test-results
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
circleci tests split --split-by=timings)"
bundle exec rspec \
--format progress \
--format RspecJunitFormatter \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
# collect reports
- store_test_results:
path: /tmp/test-results
- store_artifacts:
path: /tmp/test-results
destination: test-results
- deploy:
name: Deploy Master to Heroku
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master
fi
database.yml.ci
test:
adapter: mysql2
encoding: utf8
pool: 5
username: 'root'
port: 3306
host: '127.0.0.1'
database: sample_app_test #database.ymlのテスト環境のデータベース名を参照
2.heroku.ymlの導入
ルートディレクトリ直下にheroku.ymlを作成。
heroku.yml
build:
docker:
web: Dockerfile
run:
web: bundle exec puma -C config/puma.rb
3.Rspecの導入
Gemfile
group :development do
gem "rspec-rails" #追加
end
group :test do
gem 'rspec_junit_formatter' #追加
end
$ cd ルートディレクトリ
$ docker-compose run web bundle install
$ docker-compose build
ビルド完了後
$ docker-compose run web rails g rspec:install
4.GithubにPush&Heroku自動デプロイ
※heroku createでアプリを作り、最初のデプロイは済ませておく。
※CircleCIにGithubアカウントでログインして初期セッティングを済ませる。
①CircleCI管理画面での設定
右上のProject Settingdeをクリックし、Environment Variablesから「Add Variables」をクリックして、
「HEROKU_API_KEY」と「HEROKU_APP_NAME」を埋め込む。
※HEROKU_API_KEYはHEROKU管理画面に記載されている。
$ cd ルートディレクトリ
$ git init
$ git add -A
$ git commit -m "first auto deploy"
$ git remote add 登録名 登録したいgithubレポジトリURL
$ git push 登録名 master
#GithubへのPush完了後Herokuへも自動デプロイ