CircleCI2.1の新機能使ってブログのCI設定をDRYに書いた

GatsbyJSで制作したブログのFirebase HostingへのデプロイをCircleCIで自動化する方法」の続編的な感じです。

想定読者

  • CircleCI2.0は知っている

ブログで使っているCI設定

.circleci/config.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
version: 2.1

executors:
  node: # 複数jobで使用するので定義
    docker:
      - image: circleci/node:10

commands:
  deploy_to_firebase: # こいつは無駄にcommandsに入ってます。staging作ろうかなーと思ったときの名残
    parameters:
      firebase_token:
        type: env_var_name
        default: FIREBASE_DEPLOY_TOKEN
    steps:
      - run: |
                    npm run deploy-production --  --token=${<<parameters.firebase_token>>}

  prepare_dependencies: # 複数jobで使用するので定義
    steps:
      - restore_cache:
          keys:
            - node_modules-cache-{{ .Branch }}-{{ checksum "package-lock.json" }}
            - node_modules-cache-{{ .Branch }}
            - node_modules-cache

      - run:
          name: npm install
          command: npm install

      - save_cache:
          key: node_modules-cache-{{ .Branch }}-{{ checksum "package-lock.json" }}
          paths:
           - ./node_modules

jobs:
  build:
    executor: node # executorsで定義したものを使用
    steps:
      - checkout

      - prepare_dependencies # commandsで定義したものを使用

      - run: |
                    npm run type-check
 
  deploy_production:
    executor: node
    steps:
      - checkout

      - prepare_dependencies

      - deploy_to_firebase

workflows:
  version: 2
  main:
    jobs:
      - build
      - deploy_production:
          filters:
            branches:
              only: master
          requires:
            - build

execturosとcommands使って書くとDRYに書けるのでおすすめです。

Built with Hugo
テーマ StackJimmy によって設計されています。