CircleCIのorbsをあまり使ってことなかったのですが、使ってみるとだいぶ便利だったので使い方をMEMOしておきます📝
CircleCI orbsとは?
CircleCI Orbs は、ジョブ、コマンド、Executor などの構成要素をまとめた共有可能なパッケージです。 CircleCI では承認済み Orbs に加え、CircleCI パートナーによってオーサリングされたサードパーティ製の Orbs も提供しています。 https://circleci.com/docs/ja/2.0/orb-intro/
CircleCIでの処理をまとめたパッケージがorbsという感じのようです。
実際に使ってみる
まずorbsを使うには設定ファイルでorbs配下に使用するorbsを羅列します。
version: 2.1 orbs: ruby: circleci/ruby@1.1.1
これでcircleci/ruby@1.1.1をrubyという名前で設定ファイル上にて使用できるようになりました。
circleci/ruby@1.1.1は何が出来るのかというのは下記のドキュメントに記載があります📘
https://circleci.com/orbs/registry/orb/circleci/ruby
このorbsのinstall-depsを使用して依存関係を解決するようにしてみます💎
今回は以下のようなcommandsを用意してorbsの機能を利用するようにしてみました。
commands: install_ruby_deps: steps: - ruby/install-deps: key: v1-dependencies-{{ checksum "Gemfile.lock" }} path: ./vendor/bundle with-cache: true
keyにcacheのkey、pathにcacheするパス、with-cacheにcacheするかどうかを渡しています。
orbsを使わないとcacheのrestoreやbundlerのinstall等を自分で書かないといけないと思うのですが、orbsを使うと便利ですね✨
commands: configure_bundler: steps: - run: name: Configure Bundler command: | echo 'export BUNDLER_VERSION=$(cat Gemfile.lock | tail -1 | tr -d " ")' >> $BASH_ENV source $BASH_ENV gem install bundler -v $BUNDLER_VERSION install_ruby_deps: steps: - run: name: install dependencies command: bundle install --jobs=4 --clean --path ./vendor/bundle cache_ruby_deps: steps: - save_cache: name: Cache ruby dependencies paths: - ./vendor/bundle key: v1-dependencies-{{ checksum "Gemfile.lock" }} restore_ruby_deps: steps: - restore_cache: name: Restore ruby dependencies keys: - v1-dependencies-{{ checksum "Gemfile.lock" }} - v1-dependencies-
※commandsについては以前使い方をまとめたので、よろしければこちらも参考にしてください。
以下にrubocopとrspecを実行するサンプルを書いてみました。orbsを使うと非常にスッキリかけますね✨
version: 2.1 orbs: ruby: circleci/ruby@1.1.1 web: &web - image: circleci/ruby:2.7.1-node-browsers executors: web: docker: - <<: *web commands: attach_current: steps: - attach_workspace: at: . install_ruby_deps: steps: - ruby/install-deps: key: v1-dependencies-{{ checksum "Gemfile.lock" }} path: ./vendor/bundle with-cache: true jobs: build: executor: name: web steps: - checkout - persist_to_workspace: root: . paths: - . ruby_build: executor: name: web steps: - attach_current - install_ruby_deps ruby_lint: executor: name: web steps: - attach_current - install_ruby_deps - run: name: run rubocop command: bundle exec rubocop --parallel ruby_test: executor: name: web steps: - attach_current - install_ruby_deps - run: name: run tests command: bundle exec rspec spec/ workflows: version: 2 build: jobs: - build - ruby_build: requires: - build - ruby_lint: requires: - ruby_build - ruby_test: requires: - ruby_build
今回は使っていませんが、circleci/rubyにはrubocopとrspecを実行する機能もあるので、気になった方はそちらも使ってみてはいかがでしょうか💎
それでは。