0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Firebase Functionsの関数デプロイの際にStagingとProductionに上げる関数を分ける

Posted at

特定の状況では、特定の環境にだけ関数をアップロードしたい場合もあるかもしれません。
今回、2手法思いついたのでご紹介致します。

--onlyオプションを利用する

一番、シンプルで公式な手段としては
関数のデプロイに関しては、このようにデプロイ対象を指定すれば良さそうです。

% firebase deploy --only "functions:helloWorld2"

すでにデプロイしてしまったものはこちらで削除します。

% firebase functions:delete myFunction

参考URL:
https://firebase.google.com/docs/functions/manage-functions?hl=ja

ただ

% firebase deploy

で一気に処理できなくなるので少し面倒かもしれません。

環境変数を利用する

検証コードを書いたところ
このようなコードで選択的にデプロイできるようになりました。

import * as functions from 'firebase-functions';

const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

const helloWorld2 = functions.https.onRequest((request, response) => {
    functions.logger.info("Hello logs!", {structuredData: true});
    response.send("Hello from Firebase!");
});

exports.helloWorld = helloWorld;

if (process.env.NODE_ENV === 'production') {
  exports.helloWorld2 = helloWorld2;
}

このように環境変数 process.env.NODE_ENV を使うことにより

すべてデプロイする場合はこのように

% NODE_ENV=production firebase deploy

次のようにした場合は helloWorld2 はデプロイされないようになります。

% firebase deploy
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?