1
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?

【Next.js】Firebaseで、App Hosting/Hosting 両方デプロイしてしまった時の対処法

Posted at

問題

Next.jsプロジェクトで、Firebaseに誤って Hosting, AppHosting の両方をあげてしまった。

解決策として、以下対応をしたい。

  • Hostingはリクエストされたとき、Site Not Foundページに遷移させる
    (削除できないため)
  • AppHostingは動くようにする

不具合

(1)Hostingを disable にしても、改めてdeployすると復活してしまう
(CLIにも、「Deploy a new version to re-enable」と記載されている)

% firebase hosting:disable 
Are you sure you want to disable Firebase Hosting for the site tech-blog-2025 This will immediately make your site inaccessible! Yes 
✔ Hosting has been disabled for tech-blog-2025. Deploy a new version to re-enable.

理由

  • firebase hosting:disable は、一時的に停止するだけで、既存のサイト設定やFirebaseプロジェクト上のHosting情報は残っている
  • その状態で再度firebase deployすると、Hostingが復活して再公開される

(2)デフォルトHostingサイトは削除できない
FirebaseのデフォルトHostingサイト(.web.app / .firebaseapp.com)は削除できない。

% firebase hosting:sites:delete tech-blog-2025 --project tech-blog-2025 

Deleting a site is a permanent action. If you delete a site, Firebase doesn't maintain records of deployed files or deployment history, and the site tech-blog-2025 cannot be reactivated by you or anyone else. ? Are you sure you want to delete the Hosting site tech-blog-2025 for project tech-blog-2025? Yes 
Error: HTTP Error: 400, Cannot delete default Hosting Site tech-blog-2025.

解決方法

以下コマンドは行い、Hostingを停止状態にする。

% firebase hosting:disable

別で、firebase.json、GitHubActionsのymlファイルを修正し、AppHostingのみをデプロイ対象にする

エラーとなったソースコード

firebase.json
// deploy先がhostingになっている

{
    "hosting": {
        "source": ".",
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ],
        "frameworksBackend": {
            "region": "asia-east1",
            "maxInstances": 1
        }
    }
}
githubactions.yml
# 中略

# Deploy 部分
# - name: Deploy to Firebase Hosting
- name: Deploy to Firebase App Hosting
uses: FirebaseExtended/action-hosting-deploy@v0
with:
  repoToken: ${{ secrets.GITHUB_TOKEN }}
  firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_TECH_BLOG_2025 }}
  projectId: tech-blog-2025
  channelId: live
  firebaseToolsVersion: "13.0.2"
env:
  FIREBASE_CLI_EXPERIMENTS: webframeworks

修正後のソースコード

firebase.json
{
  "apphosting": [
    {
      "backendId": "tech-blog-backend-new2",
      "rootDir": ".",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log",
        "functions"
      ]
    }
  ]
}
githubactions.yml
# containerを使っているのでdeploy部分以外も全て記載

name: Test App Hosting Deployment (Docker Container)

on:
  push:
    branches:
      - main

jobs:
  test_and_deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    # container  ジョブ内の全ステップを指定したDockerコンテナの中で実行できる
    # gcloudというツールが最初から入っているDockerコンテナを選んで使うことで、自分で gcloud をインストールする手間を省いている
    container:
      image: google/cloud-sdk:latest
      # options: --user rootは、GitHub Actions の container のオプション
      # コンテナ内で権限エラーを避けるためにrootユーザーで実行
      options: --user root

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Configure Git safe directory
      # git configは、Gitの動作や設定を変更するコマンド
      # git config --global --add safe.directory ... で安全なディレクトリとして明示する必要がある
        run: git config --global --add safe.directory /__w/box-7/tech-blog

      - name: Set up Node.js
        run: |
          curl -fsSL https://deb.nodesource.com/setup_20.x | bash -

          apt-get update
          apt-get install -y nodejs

      - name: Install dependencies
        run: npm ci

      - name: Run unit tests
        run: npm test

      - name: Install Firebase CLI (firebase-tools)
        run: npm install -g firebase-tools

      - name: Authenticate gcloud with Service Account

        run: |
          cat <<EOF > /tmp/key.json

      # ${{ secrets.FIREBASE_SERVICE_ACCOUNT_TECH_BLOG_2025 }} にはGitHub Actionsのシークレット(サービスアカウントJSON)が展開されるので、その内容を/tmp/key.json に書き込む

          ${{ secrets.FIREBASE_SERVICE_ACCOUNT_TECH_BLOG_2025 }}
          EOF
      # サービスアカウントを使って Google Cloud にログインする
      # --key-file=/tmp/key.json:JSON ファイルでサービスアカウントの秘密鍵を指定
      # --project=tech-blog-2025:この認証で操作するデフォルトの GCP プロジェクトを指定
          gcloud auth activate-service-account --key-file=/tmp/key.json \
            --project=tech-blog-2025
      # デフォルトプロジェクトを設定
          gcloud config set project tech-blog-2025

      - name: Trigger Firebase App Hosting Deployment
        if: success()
        # export コマンドは、シェル(bashなど)で環境変数を設定・公開するためのコマンド

        # GOOGLE_APPLICATION_CREDENTIALS 環境変数にサービスアカウントのJSONファイルのパス(/tmp/key.json)を指定することで、
        # Firebase CLI(firebase-tools)が、その認証情報を使ってGoogle Cloud/Firebaseにアクセスできるようになる

        # firebase deploy コマンドを実行すると、時々「本当にデプロイしますか?」などの確認プロンプトが表示される
        # echo "Y" で「Yes」と自動応答し、その内容をパイプ(|)で firebase deploy に渡すことで、手動入力なしで自動的にデプロイが進むようにしている
        # --non-interactive オプションは、対話モードを無効化し、CI/CD環境で自動実行できるようにする
        run: |
          export GOOGLE_APPLICATION_CREDENTIALS="/tmp/key.json"
          echo "Y" | firebase deploy --project tech-blog-2025 --non-interactive

新しくデプロイしても、Hostingには、赤枠の部分の最新リリースがされず、残っているのは以前のリリースのみ。

image.png

urlを叩くとSite Not Foundが表示される。
image.png

おわりに

とても難しかったです。
AIに聞くときは、何をやろうとしているか、ドキュメントはどうなっているかを確認し、理解した上で進めます。

参考情報

issue GitHub Actionsのコンテナ内でのgit config safe.directoryの設定が必要
https://github.com/actions/runner/issues/2033
https://github.com/actions/checkout/issues/1169

GOOGLE_APPLICATION_CREDENTIALS 環境変数
https://cloud.google.com/docs/authentication/application-default-credentials?hl=ja

non-interactiveについてドキュメントはこれしか確認できなかった
https://firebase.google.com/docs/data-connect/manage-schemas-and-connectors?hl=ja

1
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
1
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?