LoginSignup
3
3

More than 3 years have passed since last update.

コピペで使うFirebase【CircleCI編】

Last updated at Posted at 2019-08-14

概要

firebaseを使用する際に、コピペで動かしたいと思い設定手順を投稿しようと思いました。
前回:Firebase【テスト編】

CircleCI / アカウント登録

CircleCIの設定

環境変数の登録

Firebaseトークンを取得

firebase login:ci
...
✔  Success! Use this token to login on a CI server:

xxxxxxxxxxxxxxxxxxxxx

Example: firebase deploy --token "$FIREBASE_TOKEN"

CircleCI > JOBS > 該当プロジェクトの設定(歯車アイコン) > Enviroment Variables > Add Variable
へ取得したトークンを登録する。

設定ファイルを作成

$ mkdir .circleci
$ cd .circleci
$ touch config.yml
circleci/config.yml
version: 2
jobs:
  build:
    docker:
      - image: circleci/openjdk:11-jdk-sid-node
    # 対象のブランチはmasterのみ
    branches:
      only:
        - master

    working_directory: ~/repo

    steps:
      # ソースコードのチェックアウト
      - checkout
      # キャッシュのリストア処理。
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "functions/package.json" }}
            - v1-dependencies-
      # ルートのパッケージをインストール
      - run:
          name: package install
          command: npm install
      # rulesのテストに使用するエミュレーターのセットアップ
      - run:
          name: emulators install
          command: ./node_modules/.bin/firebase setup:emulators:firestore
      # functionsのパッケージのインストール
      - run:
          name: functions package install
          command: cd functions/ && npm install
      # キャッシュの保存処理。
      - save_cache:
          paths:
            - node_modules
            - functions/node_modules
          key: v1-dependencies-{{ checksum "functions/package.json" }}
      # functions内にfirebase-toolsのインストール
      - run:
          name: firebase-tools install
          command: cd functions/ && npm install -D firebase-tools
      # functions内のテストの実行
      - run:
          name: run test
          command: cd functions/ && npm run test
      # デプロイの実行
      - run:
          name: deploy to Firebase Hosting
          command: ./node_modules/.bin/firebase --project default deploy --token=$FIREBASE_TOKEN

備考

$ npm run test
# functions/package.json内の "test": "npm run serve-firestore & jest"

このコマンド実行時にテストがよく失敗していた。
以下を追加・修正することでテストが成功するようになった。
※原因は不明

functions/package.json
  "scripts": {
   ...
-    "test": "npm run serve-firestore & jest"
+    "jest": "jest",
+    "test": "npm run serve-firestore & npm run jest"
  },

最後に

以上でGIthubにプッシュ後にCircleCIでテスト→デプロイまでできるようになりました。

ルートのpackage.jsonとfunctions/package.jsonをまとめればもっと簡単になりますが、
本記事ではできるだけ自動生成されたディレクトリー構造のままでやっています。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3