3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails5で作成したアプリをDockerに対応したcircle2.0でテストを実行してみた

Posted at

circleciが2.0でDockerにしっかり対応したようなので試してたメモです
と言っても公式のドキュメントにほぼ書いてあるので最終的な設定ファイルを載せておきます

circle.yml
version: 2
jobs:
  build:
    working_directory: /root/my-project

    # ここは1.0でもあった実行対象ブランチ設定
    branches:
      only:
        - master

    # Dockerのimage設定はここで 今回はruby2.4.1とdatabaseにはpostgresql9.6.2(公式image)を利用
    docker:
      - image: ruby:2.4.1
      - image: postgres:9.6.2
        environment:
          POSTGRES_USER: root

    # ここの下に処理を書いて行く模様
    steps:
      # hosts設定
      - run: echo 127.0.0.1 circlehost | tee -a /etc/hosts

      # 必要なものをapt-getでインストール
      - run:
          command: |
            apt-get update -qq
            apt-get install -y build-essential libpq-dev nodejs git --no-install-recommends
            rm -rf /var/lib/apt/lists/*

      # コードのチェックアウト
      - checkout

      # bundle installのキャシュがあればそれを取ってくる
      - restore_cache:
          keys:
            - dependency-cache-{{ checksum "Gemfile.lock" }}
      
      # bundle install実行 上記でキャッシュがロードされればキャッシュが利用されるはず
      - run: bundle install --path vendor/bundle

      # bundle installのデータをキャッシュする
      - save_cache:
          key: dependency-cache-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle

      # お決まりのコマンドでDBを生成
      - run:
          name: create test DB
          environment:
            RAILS_ENV: test
            DATABASE_USER: root
            DATABASE_HOST: circlehost
          command: |
            mv config/database.ci.yml config/database.yml
            bin/rails db:create
            bin/rails db:migrate

      # テストの実行
      - run:
          name: run test
          command: bundle exec rspec
config/database.yml
test:
  adapter: postgresql
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch('DATABASE_USER') { 'root' } %>
  host: <%= ENV.fetch('DATABASE_HOST') { 'localhost' } %>
  port: <%= ENV.fetch('DATABASE_PORT') { 5432 } %>
  database: myapp_test

最初の環境構築が大分速くなったような気がします

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?