[Ruby] [Memorandum] CircleCI / Ruby (Minimum configuration of official doc.) Outline of each setting item and reference link

6 minute read

Introduction

Language Guide: Ruby-CircleCI is a memorandum memo that I checked each item of the configuration file. I tried a short explanation within the scope of interpretation, but overall it was close to the referenced links.

Purpose of this post

  • Understanding the structure of ``/.circleci/config.yml’’
  • Preparation for streamlining similar surveys
  • Posting experience

Main story

Links to the corresponding tags in the following references are pasted in various places. Reference of each setting item Set CircleCI-CircleCI

/.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/[email protected]
  node: circleci/[email protected]

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.

About the outline of each setting item Orbs, Jobs, Steps, Workflow-CircleCI

version:

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

version-Set CircleCI-CircleCI Specify the CircleCI version.

orbs:

orbs:
  ruby: circleci/[email protected]
  node: circleci/[email protected]

Orb documentation What is Orbs-CircleCI Use Orbs-CircleCI orbs-Set CircleCI-CircleCI

Orb is a sharable package that collects configuration elements such as jobs and commands. In this setting item, what is imported from Orbs in CircleCI Orb Registry is described.

Orb registry page used this time Ruby: CircleCI Orb Registry-circleci/ruby Node: CircleCI Orb Registry-circleci/node

(The Orb Source in the Orb registry documentation is just the source code for generating the documentation page. If you want to check the source information of Orb (including the file hierarchy), you need to refer to the repository of GitHub. )

jobs:

jobs-Configure CircleCI-CircleCI Jobs this time consists of two parallel jobs, build and 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-Configure CircleCI-CircleCI Set the Docker image, entry points, commands, etc. It is similar to the setting items of 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-Set CircleCI-CircleCI

checkout

checkout-Set CircleCI-CircleCI Specify the checkout directory. Default is working_directory

It seems that the Bourne shell script is run behind the scenes to check out the GitHub source code. It seems that it will also be a study of SSH, so I will check the opportunity again.

ruby/install-deps

This command is defined by Orb-circleci/ruby.

description: Install gems with Bundler. (Quoted from source)

Install the Gem using Bundler.

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-Set CircleCI-CircleCI It is a setting for parallel processing of job steps.

CircleCI has a mechanism to distribute the test to multiple machines and execute them in parallel in order to reduce the time required to test the project.Specify the number of parallel machines in the ``parallelism’’ key. I will. About parallel jobs: Parallel execution of tests-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: ""

Build a test environment.

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

environment-Configure CircleCI-CircleCI Set environment variables for 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-Configure CircleCI-CircleCI Item to call the command line program.

dockerize -wait tcp://localhost:5432 -timeout 1m Wait for the DB service to start up.Waiting for dependencies using #Dockerize-Database settings-CircleCI Official dockerize repository: jwilder/dockerize: Utility to simplify running applications in docker containers

bundle exec rails db:schema:load --trace Create a database from db/schema.rb without handling the migration file. Create database with schema file-Rake-Rails document [6.1 Meaning of Schema File-Active Record Migration-Rails Guide](https://railsguides.jp/active_record_migrations.html#%E3%82%B9%E3%82%AD%E3%83%BC%E3%83%9E%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E6%84%8F%E5%91%B3%E3%81%AB(%E3%81%A4%E3%81%84%E3%81%A6)

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-Configure CircleCI-CircleCI It is a setting for automating a job.

version: Version-Set CircleCI-CircleCI

build_and_test: Workflow_name-Set CircleCI-CircleCI

jobs: Jobs-workflow_name-Set CircleCI-CircleCI Clarify the job dependency with requires, and specify the job that should be completed before this job is executed. That is, test will be executed after build has been executed and completed.