Open4

firebase functions & monorepo の tips メモ

nbstshnbstsh
nbstshnbstsh

codebase を設定する

firebase.json
"functions": {
  "codebase": "my-codebase"
}

fiebase.json の codebase を設定すると、firebase functions を deploy する際に、そのソース配下の functions のみを分割して deploy できる。

? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. (y/N)

これがないと、deploy するたびに上記のような質問が prompt が表示される。別の workspace で管理する functions を削除しないように注意しなければならず、最悪ミスると functions が削除されてしまい危険。

複数指定する場合は配列を利用 ↓

firebase.json
"functions": [
  {
    "source": "teamA",
    "codebase": "team-a"
  },
  {
    "source": "teamB",
    "codebase": "team-b"
  },
]
nbstshnbstsh

functions を grouping する

functions を object に nest させることで grouping が可能。

index.ts
export const myCompany = {
  teamA: {
    func1: functions.https.onRequest((request, response) => {
      // ...
    }),    
    func2: functions.https.onRequest((request, response) => {
      // ...
    }),    
  },
  teamB: {
    func3: functions.https.onRequest((request, response) => {
      // ...
    }),    
    func4: functions.https.onRequest((request, response) => {
      // ...
    }),    
  }
}

deploy する際に、function 名を指定することでスコープを制御することができる。

firebase deploy --only functions:myCompany.teamA

=> func1, func2 が deploy される

firebase deploy --only functions:myCompany.teamB

=> func3, func4 が deploy される

今は codebase による管理ができるようになったので、このようなスコープの制御は不要になったかもしれない...

https://firebase.google.com/docs/functions/organize-functions?gen=2nd#group_functions

nbstshnbstsh

codebase & grouping を利用した functions の deploy

codebase を指定した上で、かつ grouping した functions の一部を deploy する場合は、対象の functions を functions:{codebase}:{grouping} で指定する。

firebase deploy --only functions:my-codebase:myGroup.myFunc

この場合、my-codebasemyGroup.myFunc の functions が deploy される。

"functions": [
  {
    "codebase": "my-codebase"
  },
]
index.ts
export const myGroup = {
  // ↓この function を指定 
  myFunc: functions.https.onRequest((request, response) => {
      // ...
  }),
  // ...
}

deploy 時のこの挙動については、こちらの issue で詳細に説明されている↓

https://github.com/firebase/firebase-tools/issues/6008#issuecomment-1600867191

Running firebase deploy --only functions:hadsignup means that the deployment logic will first look for a codebase named hadsignup; if no codebase by that name is found, then we will look for a specific function named hadsignup in the default codebase to deploy. However, if your hadsignup function is not in the default codebase, you can specify a codebase prefix for the function, e.g. [codebase-name]:hadsignup.

functions:foo を指定した場合、

  • まず、foo という名前の codebase を探す
  • なければ、foo という名前の function を default codebase から探す

という挙動になる。