この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
CircleCI 2.1とは
CircleCIは2.0がリリースされてから久しいですが、現在の最新バージョンは 2.1 となっています。
CircleCI 2.1で追加された主要機能は以下の通りです。
commands
:steps
で使うコマンドを共通化executors
: 実行環境(Docker Image)を共通化parameters
:commands
を実行する際のパラメータを定義orbs
:jobs
,executors
,commands
などのまとまりを他のプロジェクトに共有steps
の条件分岐(when
,unless
の使用)pre-steps
とpost-steps
の導入
詳しくは以下に書いてあります。
そんな中 いまからWebアプリを継続的にデプロイするにはどう書けばベストなのか? というのを知りたかったので、WebアプリをAmazon S3にデプロイする簡単な構成を試してみました。
早速の config.yml
というわけで早速ですが完成した config.yml
を紹介しつつ、ポイントを要所的に解説したいと思います。
config.yml
version: 2.1
orbs:
aws-s3: circleci/aws-s3@1.0.11
executors:
default:
docker:
- image: circleci/node:10
- image: circleci/python:2.7
commands:
npm_install:
steps:
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: install npm
command: yarn install
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- node_modules
jobs:
build:
executor: default
working_directory: ~/repo
steps:
- checkout
- npm_install
deploy:
executor: default
working_directory: ~/repo
steps:
- checkout
- npm_install
- run:
name: build
command: yarn build:prod
- aws-s3/sync:
from: dist
to: s3://<S3 Bucket Name>
overwrite: true
workflows:
version: 2
continuous-deploy:
jobs:
- build
- deploy:
requires:
- build
filters:
branches:
only: master
orbs
3行目の orbs
は、早速新しい機能です。今回はS3にデプロイする一通りのジョブが揃っている circleci/aws-s3
を使っています。
orbs:
aws-s3: circleci/aws-s3@1.0.11
orbs
は44行目でデプロイする際に使っています。
from
と to
, overwrite
は circleci/aws-s3
のカスタムパラメータです。
- aws-s3/sync:
from: dist
to: s3://<S3 Bucket Name>
overwrite: true
なお circleci/aws-s3
を使うには、Environment VariablesにAWSのIAM Userのクレデンシャルを渡す必要があります。
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION
executors
6行目の executors
は実行する Docker Image の指定を共通化します。
executors:
default:
docker:
- image: circleci/node:10
- image: circleci/python:2.7
build
と deploy
のジョブ内で指定しています。
jobs:
build:
executor: default
commands
12行目の commands
ではジョブ内のコマンドを共通化します。
commands:
npm_install:
steps:
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: install npm
command: yarn install
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- node_modules
ここでは npm module のインストール、キャッシュの保存・リストアをまとめて npm_install
として共通化しています。
各ジョブ内で使っています。
jobs:
build:
executor: default
working_directory: ~/repo
steps:
- checkout
- npm_install
実行してみる
これで動かしてみると正常に動いていることが確認できます。
かなりシンプルにまとまるので超おすすめ
2.1
は 2.0
で冗長だった部分が共通化できるのでかなりシンプルにまとめることができます。また、今回はデプロイするだけのシンプルな例でしたが、ワークフローを拡張したい場合も、既に共通化済みの要素の組み合わせれば、かなり簡略化できます。
既に 2.0
で設定されている方は、移行も簡単なのでぜひ移行してみてください。
参考記事
本記事に執筆にあたり、以下の記事を参考にしました。