首页 文章

具有CircleCI集成的Firebase功能

提问于
浏览
0

在基本的Firebase Functions项目中,正在函数文件夹中创建package.json文件 . 现在我们将把CircleCI用于我们的项目 . 要使此CI工作,package.json需要位于您的repo的顶级文件夹中,而不是在此帖子中的任何其他子文件夹中:https://discuss.circleci.com/t/cant-run-npm-install/19012

现在,如果我将文件移动到根并更正所有路径,我就能够在项目上构建和使用lint,但部署失败并出现错误,firebase没有项目的目录路径 . 看起来firebase-tools中有一个硬编码方法阻止了package.json在functions文件夹之外的移动 .

如果我复制package.json并将其放回函数文件夹,一切正常,但这不是一个解决方案 .

Here is my desired structure:

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"
  ]
}

有没有人已经使用CircleCI尝试过Firebase功能,或者有任何线索如何使其工作?

1 回答

  • 1

    好的,我把这个东西搞定了 . 您需要为CircleCI中的每个命令指定工作目录,使其完全为 ~/project/functions . 现在,CircleCI正在查找package.json文件 .

    这是我们对于CircleCI的 config.yml

    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
    }
    

相关问题