LoginSignup
5
3

More than 1 year has passed since last update.

CircleCI 2.1 設定サンプル(Node + MySQL + 複数DB起動)

Last updated at Posted at 2020-02-06

環境

設定例

version: 2.1

orbs:
  node: circleci/node@3.0.0

executors:
  default:
    docker:
      - image: cimg/node:16.13.1
  extended:
    docker:
      - image: cimg/node:16.13.1
      - image: cimg/mysql:8.0l
        environment:
          MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
          MYSQL_DATABASE: your_database_name_comes_here

commands:
  restore_yarn_cache:
    steps:
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - v1-yarn-packages-{{ checksum "yarn.lock" }}
  install_yarn_dependencies:
    steps:
      - run:
          name: Install Yarn Dependencies
          # to avoid "Extracting tar content of undefined failed"
          # https://github.com/yarnpkg/yarn/issues/7212
          command: yarn install --frozen-lockfile --cache-folder ~/.cache/yarn --network-concurrency 1
  save_yarn_cache:
    steps:
      - save_cache:
          name: Save Yarn Package Cache
          key: v1-yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - .yarn/cache
  wait_for_db_start_up:
    steps:
      - run:
          name: Wait for db start up
          command: dockerize -wait tcp://127.0.0.1:3306 -timeout 1m
  run_test:
    steps:
      - run:
          name: Run test
          command: yarn run test
  run_lint:
    steps:
      - run:
          name: Run lint
          command: yarn run lint

jobs:
  build:
    executor: default
    steps:
      - checkout
      - restore_yarn_cache
      - install_yarn_dependencies
      - save_yarn_cache
  test:
    executor: extended
    steps:
      - checkout
      - restore_yarn_cache
      - wait_for_db_start_up
      - run_test
  lint:
    executor: default
    steps:
      - checkout
      - restore_yarn_cache
      - run_lint

workflows:
  build_and_test:
    jobs:
      - build
      - lint:
          requires:
            - build
      - test:
          requires:
            - build

ポイント

  • 2.1の機能(executorscommands)を使って、設定をわかりやすくする。
  • executorを最適化する。
    • 例えば、DBのセットアップは数十秒かかるので(※実測で20秒程)、不要なところでは行わない。
    • 上記の例では、buildyarn install)にDBは不要なので、DBが無いexecutor(= default)を使っています。
    • executorの名前(defaultやextended)は任意です。
  • テスト実行前に、DBの起動待ちをする(dockerize -wait)。
  • step内でnameを付ける。
    • 無くてもいいですが、その場合はCircleCIのデフォルトが使われます。restore_cacheなどは全てRestoring Cacheとなり、中を見ないとどのステップか分からなくなるので、付けています。

参照先

Caching Dependencies

By default, cache storage duration is set to 15 days. This can be customized on the CircleCI web app
by navigating to Plan > Usage Controls. Currently, 15 days is also the maximum storage duration you can set.

Tip: Caches are immutable, so it is helpful to start all your cache keys with a version prefix, for example v1-.... This allows you to regenerate all of your caches just by incrementing the version in this prefix.

yarn install--frozen-lockfile オプションを付ける

version: 2.1 では workflows の version 指定は不要。

その他

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