Circle CI 2.0 설정 및 업그레이드 방법 with Rails
15765 단어 RailsCircleCI시CircleCI2.0루비
개요
Circle Ci 2.0을 구성하고 업그레이드하는 방법을 소개합니다. 구체적인 설정 내용은 Ruby on Rails를 예로 합니다.

Version 2.0의 특징
Version 2.0의 주요 변경 사항을 간략하게 소개합니다.
워크플로우이 도입되었습니다
ref. Launching Today: CircleCI 2.0 Reaches General Availability | CircleCI Blog
ref. CircleCI 2.0이 공식적으로 출시되었으므로 즉시 전환되었습니다. | SmartHR Tech Blog
설정 방법
설정 파일
Version 1.0 에서는
circleci.yml
에 설정을 쓰고 있었습니다만, 2.0 에서는 .circle/config.yml
에 쓰게 되어 있습니다.※ 들여 쓰기 수에 따라 오류가 발생하므로주의하십시오. 본 기사 기재의 코드를 복사하면 문제 없음.
이미 Circle CI를 사용 중인 경우 이 구성 파일을 만들어 2.0으로 업그레이드할 수 있습니다.
Jobs key 지정
그러면 구체적으로 설정 파일을 작성하는 방법을 소개합니다. 설정 내용은 Launching Today: CircleCI 2.0 Reaches General Availability | CircleCI Blog 를 참고로 하고 있습니다.
먼저 Jobs key를 설정합니다. 이것은 테스트 빌드 프로세스를 나타냅니다.
job 에는 build key 와 Job 를 실행하기 위해서 디렉토리를 지정할 필요가 있습니다.
코드로 말하면 다음과 같은 느낌이 듭니다.
.circle/config.yml
version: 2
jobs:
build:
parallelism: 1 # 並行に実行する数を指定。無料プランだと 1 じゃないと実行できない
working_directory: ~/circleci-demo-ruby-rails # ディレクトリを指定
Docker 컨테이너 이미지 지정
Docker 컨테이너 이미지를 지정합니다.
환경에서 환경 변수를 지정하는 것이 포인트입니다.
.circle/config.yml
version: 2
# ...
docker:
- image: circleci/ruby:2.5.0-node
environment:
RAILS_ENV: test
- image: circleci/postgres:9.5-alpine
environment:
POSTGRES_USER: circleci-demo-ruby # DB の User 名を決める
POSTGRES_DB: rails_blog # データベースを指定
POSTGRES_PASSWORD: ""
※ 브라우저 테스트를 실시하는 경우는
ruby:2.5.0-node
는 아니고 ruby:2.5.0-node-browsers
를 지정합시다! !ref. CircleCI 2.0 Capybara feature specs - Selenium webdriver with Chrome headless
Steps 설정하기
Steps를 설정합니다. Steps 는 「Job 안에서 무엇을 실행해 가는 것인가」라고 하는 순서를 만드는 것과 같습니다.
Steps는 먼저 Checkout을 실행합니다.
[Checkout] tells our build to checkout our project code into the working directory.
steps:
- checkout
종속 라이브러리를 캐시합니다.
checkout 후에는 구체적인 step을 지정해 봅시다. Rails 환경을 실행하려면 라이브러리를 설치해야 합니다.
steps:
# ...
- run:
name: Bundle Install
command: bundle install --path vendor/bundle
이것만으로도 좋지만 캐시를 이용하는 것이 빠르기 때문에 다음과 같이 변경합시다.
.circle/config.yml
steps:
# ...
# キャッシュを使う
- restore_cache:
keys:
- rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
- rails-demo-bundle-v2-
- run:
name: Bundle Install
command: bundle install --path vendor/bundle
# キャッシュを保存
- save_cache:
key: rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
※ 첫회 실행시나 Gemfile.lock 에 변경이 있었을 경우는 캐쉬는 이용되지 않습니다.
webpacker나 yarn 사용하고 있는 경우는, 이하도 추가해 둡시다.
.circle/config.yml
steps:
# ...
- restore_cache:
keys:
- rails-demo-yarn-{{ checksum "yarn.lock" }}
- rails-demo-yarn-
- run:
name: Yarn Install
command: yarn install --cache-folder ~/.cache/yarn
- save_cache:
key: rails-demo-yarn-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
데이터베이스 설정
이제 라이브러리는 여러가지 들어갔으므로, 다음은 데이터베이스의 설정을 합시다.
.circle/config.yml
steps:
# ...
# Database setup
- run:
name: Wait for DB
command: dockerize -wait tcp://localhost:5432 -timeout 1m
- run:
name: Database setup
command: bundle exec rake db:create db:schema:load --trace
테스트 실행
라이브러리가 들어가 데이터베이스가 생겼으므로 드디어 테스트를 실행해 봅시다!
이번에는 rspec과 rubocop을 실행했습니다.
.circle/config.yml
steps:
# ...
- run:
name: RuboCop Style Check
command: bundle exec rubocop
- 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 \
--out /tmp/test-results/rspec.xml \
--format progress \
-- \
$TEST_FILES
assets:precompile
feature spec 에서 최초의 테스트가 떨어지는 일이 있었습니다만, assets compile 가 끝나지 않은 것이 원인이었습니다. 그래서 rspec 실행 전에 다음을 추가했습니다.
.circle/config.yml
steps:
# ...
- run:
name: Precompile assets
command: bundle exec rake assets:precompile
Push 하고 종료
이것으로 설정이 종료됩니다. Push하고 CircleCI에서 확인해 봅시다.
※ 2.0부터 로컬에서도 확인할 수 있게 되었습니다.
모든 코드
.circle/config.yml
version: 2
jobs:
build:
parallelism: 3
working_directory: ~/circleci-demo-ruby-rails
docker:
- image: circleci/ruby:2.5.0-node
environment:
RAILS_ENV: test
- image: circleci/postgres:9.5-alpine
environment:
POSTGRES_USER: circleci-demo-ruby
POSTGRES_DB: rails_blog
POSTGRES_PASSWORD: ""
steps:
- checkout
# Restore bundle cache
- restore_cache:
keys:
- rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
- rails-demo-bundle-v2-
- run:
name: Bundle Install
command: bundle install
# Store bundle cache
- save_cache:
key: rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
# Only necessary if app uses webpacker or yarn in some other way
- restore_cache:
keys:
- rails-demo-yarn-{{ checksum "yarn.lock" }}
- rails-demo-yarn-
- run:
name: Yarn Install
command: yarn install --cache-folder ~/.cache/yarn
# Store yarn / webpacker cache
- save_cache:
key: rails-demo-yarn-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
- run:
name: Wait for DB
command: dockerize -wait tcp://localhost:5432 -timeout 1m
- run:
name: Database setup
command: bundle exec rake db:create db:schema:load --trace
# Run rspec in parallel
- run:
command: |
bundle exec rspec --profile 10 \
--out test_results/rspec.xml \
--format progress \
$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
참고문헌
Launching Today: CircleCI 2.0 Reaches General Availability | CircleCI Blog
CircleCI 2.0이 공식적으로 출시되었으므로 즉시 전환되었습니다. | SmartHR Tech Blog=
Language Guide: Ruby | CircleCI 2.0
CircleCI 2.0 Capybara feature specs - Selenium webdriver with Chrome headless
Reference
이 문제에 관하여(Circle CI 2.0 설정 및 업그레이드 방법 with Rails), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/cohki0305/items/97d7e6a8938950f478b5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)