Функции Firebase с интеграцией CircleCI - PullRequest
0 голосов
/ 24 апреля 2018

В базовом проекте Firebase Functions файл package.json создается в папке функций.Теперь мы собираемся использовать CircleCI для нашего проекта.Чтобы заставить этот CI работать, package.json должен находиться в верхней папке вашего репозитория, а не в любой другой подпапке, как показано в этом посте здесь: https://discuss.circleci.com/t/cant-run-npm-install/19012

Теперь, если я переместу файл вroot и исправляя все пути, я могу построить и использовать lint в проекте, но развертывание завершается с ошибкой, что у firebase нет пути к каталогу проекта.Похоже, что в firebase-tools есть жестко закодированный метод, который предотвращает перемещение файла package.json за пределы папки функций.

Если я продублирую package.json и верну его обратно в папку функцийвсе работает нормально, но это не решение.

Вот моя желаемая структура:

myproject
 |
 +- .firebaserc
 +- firebase.json
 +- package.json
 +- tsconfig.json
 +- tslint.json
 |
 +- functions/
      |
      +- src/
      |   |
      |   +- index.ts
      |
      +- lib/
          |
          +- index.js
          +- index.js.map

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "functions/lib/index.js",
  "dependencies": {
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "devDependencies": {
    "tslint": "^5.8.0",
    "typescript": "^2.5.3"
  },
  "private": true
}

tsconfig.json

{
  "compilerOptions": {
    "lib": ["es6"],
    "module": "commonjs",
    "noImplicitReturns": true,
    "outDir": "./functions/lib",
    "sourceMap": true,
    "target": "es6"
  },
  "compileOnSave": true,
  "include": [
    "./functions/src"
  ]
}

Кто-нибудь уже пробовал Firebase Functions с CircleCI или имеет какое-либо представление о том, как заставить это работать?

1 Ответ

0 голосов
/ 25 апреля 2018

Хорошо, я запустил эту штуку.Вам нужно указать рабочий каталог для каждой команды в CircleCI, чтобы он был точно ~/project/functions.Теперь CircleCI находит файл package.json.

Это наш config.yml для CircleCI:

version: 2
jobs:
    build:
        docker:
            # specify the version you desire here
            - image: circleci/node:6.14


        steps:
            - checkout

            # Download and cache dependencies
            - restore_cache:
                keys:
                    - v1-dependencies-{{ checksum "./functions/package.json" }}
                    # fallback to using the latest cache if no exact match is found
                    - v1-dependencies-

            # Install all needed dependencies from package.json
            - run: 
                working_directory: ~/project/functions
                command: yarn install

            # Save the cache including node_modules folder
            - save_cache:
                paths:
                    - ~/project/functions/node_modules
                key: v1-dependencies-{{ checksum "./functions/package.json" }}

            # Create the folder for your unit test results
            - run: 
                working_directory: ~/project/functions
                command: mkdir junit

            # run tests with mocha!
            - run:
                working_directory: ~/project/functions
                command: yarn test_ci
                environment:
                    MOCHA_FILE: junit/test-results.xml
                when: always

            - store_test_results:
                path: ~/project/functions/junit

            - store_artifacts:
                path: ~/project/functions/junit

И package.json с дополнительной командой test_ci для модульного тестирования на CircleCI:

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "test": "mocha --require ts-node/register ./src/test/*.spec.ts",
    "test_ci": "mocha --require ts-node/register --reporter mocha-junit-reporter ./src/test/*.spec.ts"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/firestore": "^0.13.1",
    "@google-cloud/storage": "^1.6.0",
    "@google-cloud/vision": "^0.17.0",
    "firebase-admin": "^5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "devDependencies": {
    "@types/chai": "^4.1.3",
    "@types/mocha": "^5.2.0",
    "@types/sinon": "^4.3.1",
    "chai": "^4.1.2",
    "firebase-functions-test": "^0.1.1",
    "firebase-tools": "^3.18.0",
    "mocha": "^5.1.1",
    "mocha-junit-reporter": "^1.17.0",
    "sinon": "^4.5.0",
    "ts-node": "^5.0.1",
    "tslint": "^5.8.0",
    "typescript": "^2.5.3"
  },
  "private": true
}
...