LoginSignup
5
2

More than 3 years have passed since last update.

[備忘録] CircleCI / Ruby(公式doc.の最小構成)について各設定項目の概要と参照先リンク

Posted at

はじめに

Language Guide: Ruby - CircleCIにて、設定ファイルの各項目について調べた備忘録メモです。
解釈した範囲で短い説明を試みましたが、全体的として参照先のリンク集に近い形になりました。

本投稿の目的

  • /.circleci/config.ymlの構造の把握
  • 類似の調査の効率化準備
  • 投稿の経験値

本編

各所で下記のリファレンスの該当タグへのリンクを貼っています。
各設定項目のリファレンス
 CircleCI を設定する - CircleCI

/.circleci/config.yml

/.circleci/config.yml
version: 2.1 # Use 2.1 to enable using orbs and other features.

# Declare the orbs that we'll use in our config.
# read more about orbs: https://circleci.com/docs/2.0/using-orbs/
orbs:
  ruby: circleci/ruby@1.0 
  node: circleci/node@2

jobs:
  build: # our first job, named "build"
    docker:
      - image: cimg/ruby:2.7-node # use a tailored CircleCI docker image.
    steps:
      - checkout # pull down our git code.
      - ruby/install-deps # use the ruby orb to install dependencies
      # use the node orb to install our packages
      # specifying that we use `yarn` and to cache dependencies with `yarn.lock`
      # learn more: https://circleci.com/docs/2.0/caching/
      - node/install-packages: 
          pkg-manager: yarn
          cache-key: "yarn.lock"

  test:  # our next job, called "test"
    # we run "parallel job containers" to enable speeding up our tests;
    # this splits our tests across multiple containers.
    parallelism: 3 
    # here we set TWO docker images.
    docker:
      - image: cimg/ruby:2.7-node # this is our primary docker image, where step commands run.
      - image: circleci/postgres:9.5-alpine 
        environment: # add POSTGRES environment variables.
          POSTGRES_USER: circleci-demo-ruby
          POSTGRES_DB: rails_blog_test
          POSTGRES_PASSWORD: ""
    # environment variables specific to Ruby/Rails, applied to the primary container.
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: 127.0.0.1
      PGUSER: circleci-demo-ruby
      PGPASSWORD: ""
      RAILS_ENV: test
    # A series of steps to run, some are similar to those in "build".
    steps:
      - checkout 
      - ruby/install-deps 
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      # Here we make sure that the secondary container boots 
      # up before we run operations on the database.
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace
      # Run rspec in parallel
      - ruby/rspec-test

# We use workflows to orchestrate the jobs that we declared above.
workflows:
  version: 2
  build_and_test:     # The name of our workflow is "build_and_test"
    jobs:             # The list of jobs we run as part of this workflow.
      - build         # Run build first.
      - test:         # Then run test,
          requires:   # Test requires that build passes for it to run.
            - build   # Finally, run the build job.

各設定項目の概要について
 Orbs、ジョブ、ステップ、ワークフロー - CircleCI

version:

version: 2.1 # Use 2.1 to enable using orbs and other features.

version - CircleCI を設定する - CircleCI
 CircleCIのバージョンを指定します。

orbs:

orbs:
  ruby: circleci/ruby@1.0 
  node: circleci/node@2

Orbについてのドキュメント
 Orbs とは - CircleCI
 Orbs を使う - CircleCI
 orbs - CircleCI を設定する - CircleCI

Orbとは、ジョブやコマンドなどの設定要素をまとめた共有可能なパッケージのことです。
この設定項目では、CircleCI Orb RegistryにあるOrbsからインポートするものを記載します。

今回使用するOrbのレジストリページ
 Ruby : CircleCI Orb Registry - circleci/ruby
 Node : CircleCI Orb Registry - circleci/node

( OrbレジストリのドキュメントにあるOrb Sourceは、あくまでドキュメントページを生成するためのソースコードです。
(ファイル階層も含めた)Orb自体のソース情報を確認する場合はGitHubのリポジトリを参照する必要があります。 )

jobs:

jobs - CircleCI を設定する - CircleCI
 今回におけるjobsは、2つのパラレルジョブ、buildとtestから構成されます。

build:

  build: # our first job, named "build"
    docker:
      - image: cimg/ruby:2.7-node # use a tailored CircleCI docker image.
    steps:
      - checkout # pull down our git code.
      - ruby/install-deps # use the ruby orb to install dependencies
      # use the node orb to install our packages
      # specifying that we use `yarn` and to cache dependencies with `yarn.lock`
      # learn more: https://circleci.com/docs/2.0/caching/
      - node/install-packages: 
          pkg-manager: yarn
          cache-key: "yarn.lock"
docker:
    docker:
      - image: cimg/ruby:2.7-node # use a tailored CircleCI docker image.

docker - CircleCI を設定する - CircleCI
 Dockerのイメージや、エントリーポイント、コマンドなどを設定します。
 docker-composeの設定項目と似ています。

steps:
    steps:
      - checkout # pull down our git code.
      - ruby/install-deps # use the ruby orb to install dependencies
      # use the node orb to install our packages
      # specifying that we use `yarn` and to cache dependencies with `yarn.lock`
      # learn more: https://circleci.com/docs/2.0/caching/
      - node/install-packages: 
          pkg-manager: yarn
          cache-key: "yarn.lock"

steps - CircleCI を設定する - CircleCI

checkout

checkout - CircleCI を設定する - CircleCI
 チェックアウトディレクトリを指定します。
 デフォルトではworking_directory

裏ではBourne shellのスクリプトが実行されてGitHubのソースコードをチェックアウトしているようです。
SSH周りの勉強にもなりそうなので、また機会を改めて調べてみます。

ruby/install-deps

Orb - circleci/rubyで定義されるコマンドです。

description: Install gems with Bundler.
(引用:ソースから)

Bundlerを使用してGemをインストールします。

test:

  test:  # our next job, called "test"
    # we run "parallel job containers" to enable speeding up our tests;
    # this splits our tests across multiple containers.
    parallelism: 3 
    # here we set TWO docker images.
    docker:
      - image: cimg/ruby:2.7-node # this is our primary docker image, where step commands run.
      - image: circleci/postgres:9.5-alpine 
        environment: # add POSTGRES environment variables.
          POSTGRES_USER: circleci-demo-ruby
          POSTGRES_DB: rails_blog_test
          POSTGRES_PASSWORD: ""
    # environment variables specific to Ruby/Rails, applied to the primary container.
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: 127.0.0.1
      PGUSER: circleci-demo-ruby
      PGPASSWORD: ""
      RAILS_ENV: test
    # A series of steps to run, some are similar to those in "build".
    steps:
      - checkout 
      - ruby/install-deps 
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      # Here we make sure that the secondary container boots 
      # up before we run operations on the database.
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace
      # Run rspec in parallel
      - ruby/rspec-test
parallelism:
parallelism: 3

parallelism - CircleCI を設定する - CircleCI
 ジョブのステップを並列処理するための設定です。

CircleCIには、プロジェクトのテストに掛かる時間を短縮するために、テストを複数のマシンに分散して並列に実行するための仕組みがあり、parallelismのキーに並列マシンの個数を指定します。
 パラレルジョブについて:テストの並列実行 - CircleCI

docker:
    docker:
      - image: cimg/ruby:2.7-node # this is our primary docker image, where step commands run.
      - image: circleci/postgres:9.5-alpine 
        environment: # add POSTGRES environment variables.
          POSTGRES_USER: circleci-demo-ruby
          POSTGRES_DB: rails_blog_test
          POSTGRES_PASSWORD: ""

テスト環境を構築します。

environment:
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: 127.0.0.1
      PGUSER: circleci-demo-ruby
      PGPASSWORD: ""
      RAILS_ENV: test

environment - CircleCI を設定する - CircleCI
 Rails + PostgreSQLのための環境変数を設定します。

steps:
    # A series of steps to run, some are similar to those in "build".
    steps:
      - checkout 
      - ruby/install-deps 
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      # Here we make sure that the secondary container boots 
      # up before we run operations on the database.
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace
      # Run rspec in parallel
      - ruby/rspec-test
run
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace

run - CircleCI を設定する - CircleCI
 コマンドラインプログラムを呼び出すための項目です。

dockerize -wait tcp://localhost:5432 -timeout 1m
 DBサービスの立ち上がりを待機します。
  #Dockerize を使用した依存関係の待機 - データベースの設定 - CircleCI
  dockerizeの公式リポジトリ:jwilder/dockerize: Utility to simplify running applications in docker containers

bundle exec rails db:schema:load --trace
 マイグレーションファイルは扱わず、db/schema.rbからデータベースを作成します。
  スキーマファイルでデータベースを作成 - Rake - Railsドキュメント
  6.1 スキーマファイルの意味について - Active Record マイグレーション - Railsガイド

workflows:

# We use workflows to orchestrate the jobs that we declared above.
workflows:
  version: 2
  build_and_test:     # The name of our workflow is "build_and_test"
    jobs:             # The list of jobs we run as part of this workflow.
      - build         # Run build first.
      - test:         # Then run test,
          requires:   # Test requires that build passes for it to run.
            - build   # Finally, run the build job.

workflows - CircleCI を設定する - CircleCI
 ジョブを自動化するための設定です。

version:
 version - CircleCI を設定する - CircleCI

build_and_test:
 workflow_name - CircleCI を設定する - CircleCI

jobs:
 jobs - workflow_name - CircleCI を設定する - CircleCI
  requiresでジョブの依存関係を明確化し、このジョブが実行されるまでに完了されるべきジョブを指定します。
  即ち、buildが実行され完了した後にtestは実行されます。

5
2
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
5
2