circleci+docker-compose備忘録


machine.imageはcircleci/classic

version: 2
jobs:
  build:
   machine:
    image: circleci/classic:edge

docker-composeを普通に使えるように

イメージキャッシュ

      - restore_cache:
          keys:
            - docker-image-{{ checksum "app.Dockerfile.development" }}

      - run:
          name: docker image cache
          command: |
            if [ ! -f ~/caches/images.tar ]; then
              docker-compose build app
              mkdir -p ~/caches
              docker save -o ~/caches/images.tar $(docker images --filter "dangling=false" --format "{{.Repository}}:{{.Tag}}")
            fi
      - save_cache:
          key: docker-image-{{ checksum "app.Dockerfile.development" }}
          paths:
           - ~/caches/images.tar

      - run:
          name: load docker images
          command: docker load -i ~/caches/images.tar

buildしたもののみキャッシュしてます

volume cache

      - restore_cache:
          keys:
            - gemfile_lock-cache-{{ checksum "Gemfile.lock" }}

      - restore_cache:
          keys:
            - yarn_lock-cache-{{ checksum "yarn.lock" }}

      - restore_cache:
          keys:
            - bundle-cache-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
            - bundle-cache-{{ .Branch }}
            - bundle-cache

      - restore_cache:
          keys:
            - node_modules-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - node_modules-cache-{{ .Branch }}
            - node_modules-cache

      - run:
          name: docker volume restore
          command: |
            if [ -f ~/caches/bundle-data.tar.bz2 ]; then
              cat ~/caches/bundle-data.tar.bz2 | docker run -i -v project_bundle-data:/volume --rm loomchild/volume-backup restore -
            fi
            if [ -f ~/caches/node_modules-data.tar.bz2 ]; then
             cat ~/caches/node_modules-data.tar.bz2 | docker run -i -v project_node_modules-data:/volume --rm loomchild/volume-backup restore -
            fi
      - run:
          name: bundle install && yarn install
          command: |
            docker-compose run --rm app sh -c "bundle install && yarn install"
      - run:
          name: create docker volume tarball
          command: |
            if [ -f ~/caches/Gemfile.lock ]; then
              md5sum ~/caches/Gemfile.lock > gemfile_lock_hash.txt
              if ! md5sum -c gemfile_lock_hash.txt; then
                docker run -v project_bundle-data:/volume --rm loomchild/volume-backup backup - > ~/caches/bundle-data.tar.bz2
              fi
            else
              docker run -v project_bundle-data:/volume --rm loomchild/volume-backup backup - > ~/caches/bundle-data.tar.bz2
            fi
            if [ -f ~/caches/yarn.lock ]; then
              md5sum ~/caches/yarn.lock > yarn_lock_hash.txt
              if ! md5sum -c yarn_lock_hash.txt; then
                docker run -v project_node_modules-data:/volume --rm loomchild/volume-backup backup - > ~/caches/node_modules-data.tar.bz2
              fi
            else
              docker run -v project_node_modules-data:/volume --rm loomchild/volume-backup backup - > ~/caches/node_modules-data.tar.bz2
            fi
      - run:
          name: copy Gemfile.lock and yarn.lock for cache
          command: |
            cp Gemfile.lock ~/caches/Gemfile.lock
            cp yarn.lock ~/caches/yarn.lock
      - save_cache:
          key: gemfile_lock-cache-{{ checksum "Gemfile.lock" }}
          paths:
           - ~/caches/Gemfile.lock

      - save_cache:
          key: yarn_lock-cache-{{ checksum "yarn.lock" }}
          paths:
           - ~/caches/yarn.lock

      - save_cache:
          key: bundle-cache-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
          paths:
           - ~/caches/bundle-data.tar.bz2

      - save_cache:
          key: node_modules-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
          paths:
           - ~/caches/node_modules-data.tar.bz2

node_modulesとbundleのvolumeをキャッシュするようにしてます。
lockファイルをキャッシュしておき前回のものとハッシュ値を比較して、ハッシュ値が異なる場合にvolumeのバックアップを作成し直してキャッシュするようにしてます。

参考文献