如何通过github操作部署firebase函数?

问题描述

在我的项目中,它具有如下所示的文件夹结构:

dev-internal-web
-.firebase
-.github
-e2e
-functions
-node_modules
-src
(misc files)

我有一个functions文件夹,其中包含firebase函数。我希望通过我的动作脚本自动进行部署,如下所示:

name: CI

on:
  push:
    branches:
    - master

jobs:
  hosting-deploy:
    name: Deploy Hosting
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@master
    - uses: actions/setup-node@master
      with:
        node-version: '10.x'
    - run: npm install
    - run: npm run build
    - uses: w9jds/firebase-action@master
      with:
        args: deploy --only hosting --project=tombstone-roleplay
      env:
        FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}

  functions-deploy:
      name: Deploy Functions
      runs-on: ubuntu-latest

      steps:
      - uses: actions/checkout@master
      - uses: chrissank/[email protected]
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
          TARGET: default

文件夹内Angular项目上的Firebase托管操作正常,但第二个脚本失败并出现以下错误

=== deploying to 'tombstone-roleplay'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /github/workspace/functions
> tslint --project tsconfig.json

sh: 1: tslint: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists,but node_modules missing,did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /github/home/.npm/_logs/2020-10-04T14_06_06_215Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1

按照注释中的说明删除tslint并将脚本更新为本文https://medium.com/mainlycoding-com/automate-firebase-functions-deployment-with-github-actions-ci-a0eb10fa308d中的内容后,我收到以下错误

=== deploying to 'project-name'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /github/workspace/functions
> tslint --project tsconfig.json

sh: 1: tslint: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists,did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /github/home/.npm/_logs/2020-10-04T14_06_06_215Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1

功能部署”部分中我在做什么错了?

解决方法

deploy 步骤与 build 步骤不同(不同的文件夹),因此您需要再次运行 npm install 或将部署 node_module 依赖项带入您的构建神器。

这是我的firebase.json

{
  "functions": {
    "predeploy": [
      "npm --prefix ./functions/ install","npm --prefix ./functions/ run lint","npm --prefix ./functions/ run build"
    ],"source": "functions"
  }
}

这里有一些讨论: https://github.com/w9jds/firebase-action/issues/30

此外,请确保 tslinteslinttsc 位于 devDependencies 文件中的 package.json 中。