CircleCI Orbs使ってnext.jsアプリをECSにデプロイ

CircleCI Orbsってやつが出てたので、調べてたら下記のようなサンプルが見つかった。
これ見ただけで置き換える価値がありそうだなって思うよね！

```yaml
orbs:
  aws-cli: circleci/aws-cli@0.1.13
  aws-ecr: circleci/aws-ecr@6.2.0
  aws-ecs: circleci/aws-ecs@0.0.11
```

```yaml
      - aws-ecr/build-and-push-image:
          repo: "${AWS_RESOURCE_NAME_PREFIX}"
          tag: << parameters.env >>
      - aws-ecs/update-service:
          family: "${AWS_RESOURCE_NAME_PREFIX}"
          service-name: "${AWS_RESOURCE_NAME_PREFIX}-service"
          cluster-name: << parameters.cluster >>
```

# Orbsって？
configの標準化、パッケージングのために設定まとめたものみたい。
いろいろあるから興味があれば、レジストリ見てみよう。

https://circleci.com/orbs/registry/

# ブランチ別実行
ブランチ別で作業したい場合、以前はif文かいてたと思うけど、
変数渡したり、ブランチに `filter` かけて実行制御できるみたい。楽ちんだ。

Using Environment Variables - CircleCI - https://circleci.com/docs/2.0/env-vars/


```yaml
  deploy:
    parameters:
      env:
        type: enum
        enum: ["staging", "latest"]
      cluster:
        type: enum
        enum: ["awesomeApp-ECSCluster-stg"]
    executor:
      name: default
    steps:
      - checkout

workflows:
  setup_and_deploy:
    jobs:
      - setup
      - deploy:
          name: deploy_staging
          env: staging
          cluster: "awesomeApp-ECSCluster-stg"
          requires:
            - test
          filters:
            branches:
              only: staging
```


# サンプル
めんどいからサンプル全文貼る。
CircleCIのENVに色々挿してあるのでそこだけ注意。

```.circleci/config.yml
version: 2.1

orbs:
  aws-cli: circleci/aws-cli@0.1.13
  aws-ecr: circleci/aws-ecr@6.2.0
  aws-ecs: circleci/aws-ecs@0.0.11

executors:
  default:
    working_directory: ~/workspace
    docker:
      - image: circleci/node:10

commands:
  restore_pkg:
    steps:
      - restore_cache:
          name: Restore node pkg dependencies
          key: yarn-{{ checksum "yarn.lock" }}-{{ .Environment.CACHE_VERSION_NPM }}

  save_pkg:
    steps:
      - save_cache:
          name: Cache npm pkg dependencies
          key: yarn-{{ checksum "yarn.lock" }}-{{ .Environment.CACHE_VERSION_NPM }}
          paths:
            - ~/workspace/node_modules

jobs:
  setup:
    executor:
      name: default
    environment:
      AWS_DEFAULT_OUTPUT: json
    steps:
      - checkout
      - attach_workspace:
          at: workspace
      - aws-cli/install
      - aws-cli/configure:
          aws-access-key-id: AWS_ACCESS_KEY_ID
          aws-region: AWS_REGION
      - restore_pkg
      - run:
          name: Install dependencies
          command: yarn
      - save_pkg
  test:
    executor:
      name: default
    steps:
      - checkout
      - restore_pkg
      - run:
          name: Run test
          command: yarn test
  test_build:
    parameters:
      env:
        type: enum
        enum: ["development"]
    executor:
      name: default
    steps:
      - checkout
      - restore_pkg
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          name: next build
          command: yarn build
      - run:
          name: docker build
          command: docker build -t "${AWS_RESOURCE_NAME_PREFIX}:${CIRCLE_SHA1}" .
  deploy:
    parameters:
      env:
        type: enum
        enum: ["staging", "latest"]
      cluster:
        type: enum
        enum: ["awesomeApp-ECSCluster", "awesomeApp-ECSCluster-stg"]
    executor:
      name: default
    steps:
      - checkout
      - restore_pkg
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          name: next build
          command: yarn build
      - aws-ecr/build-and-push-image:
          repo: "${AWS_RESOURCE_NAME_PREFIX}"
          tag: << parameters.env >>
      - aws-ecs/update-service:
          family: "${AWS_RESOURCE_NAME_PREFIX}"
          service-name: "${AWS_RESOURCE_NAME_PREFIX}-service"
          cluster-name: << parameters.cluster >>

workflows:
  setup_and_deploy:
    jobs:
      - setup
      - test:
          requires:
            - setup
      - test_build:
          name: build_development
          env: development
          requires:
            - test
          filters:
            branches:
              ignore:
                - master
                - staging
      - deploy:
          name: deploy_staging
          env: staging
          cluster: "awesomeApp-ECSCluster-stg"
          requires:
            - test
          filters:
            branches:
              only: staging
      - deploy:
          name: deploy_production
          env: latest
          cluster: "awesomeApp-ECSCluster"
          requires:
            - test
          filters:
            branches:
              only: master
```