1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

"Error: Failed to authenticate, have you run firebase login?"が発生した際の対処法

Posted at

はじめに

GitHub Actions を利用して CI/CD パイプラインを構築し、Firebase へのデプロイに FirebaseExtended/action-hosting-deploy@v0 を使用したところ、認証エラー が発生しました。今回は、エラーの詳細と解決策をまとめます。

エラー内容

Firebase にデプロイするために、サービスアカウントの JSON キーファイルを取得し、それを、それを BASE64 にエンコード した値を GitHub Actions の secrets に設定しました。

Github_secrets.png

そして、公式アクションのFirebaseExtended/action-hosting-deploy@v0 を利用して以下のようなymlファイルを作成しました。

参考にしたデプロイ方法①

🔗 Deploy to Firebase Hosting (GitHub Actions)

修正前のyml

name: CI/CD

on: [push]

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Build project
        run: npm run build

      - name: Upload dist
        uses: actions/upload-artifact@v4
        with:
          name: build
          path: dist

  deploy:
    name: Deploy
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download build artifact
        uses: actions/download-artifact@v4
        with:
          name: build
          path: dist

      # 🔴Firebase のサービスアカウントキーを `secrets` から取得
      - name: Deploy to Firebase Hosting
        uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT }}"
          projectId: "${{ secrets.FIREBASE_PROJECT_ID }}"
          channelId: live

発生したエラー

しかし、デプロイ時に以下のエラーメッセージが表示され、認証エラー が発生しました。

▶︎ Deploying to production site
  /usr/local/bin/npx firebase-tools@latest deploy --only hosting --project "project-id" --json
  npm warn exec The following package was not found and will be installed: firebase-tools@13.33.0
  {
    "status": "error",
    "error": "Failed to authenticate, have you run \u001b[1mfirebase login\u001b[22m?"
  }
  
  The process '/usr/local/bin/npx' failed with exit code 1
  Retrying deploy with the --debug flag for better error output
  
  [2025-XX-XXTXX:XX:XX.XXXX] 🔴 **SyntaxError: Unexpected token 'e', "eXXXXXXXX"... is not valid JSON**
      at JSON.parse (<anonymous>)
      at ReadStream.<anonymous> (/home/runner/.../google-auth-library/.../googleauth.js:511:43)
  
  Error: Failed to authenticate, have you run firebase login?

エラーのポイント

  • Failed to authenticate → Firebase の認証に失敗
  • SyntaxError: Unexpected token 'e' → JSON のパースエラー が発生

有識者にも確認した結果、エラーの内容から キー情報(JSON)が適切に読み込まれていない 可能性が高いと判断しました。

対応内容

① キー情報の再設定

  1. Firebase の サービスアカウント JSON キーファイルを再取得
  2. BASE64 にエンコードし直して GitHub Secrets に設定

しかし、こちらの方法では 同様のエラーが発生 し、解消されませんでした。


② デプロイ方法を変更

公式アクション(FirebaseExtended/action-hosting-deploy)を使用せず、Google Cloud の認証を活用する方法へ変更 しました。
この方法により、認証エラーが解消され、デプロイが成功 しました。

📝 変更内容

変更前

  • 使用アクション: FirebaseExtended/action-hosting-deploy@v0
  • 認証方法: Firebase サービスアカウントキー(BASE64 エンコード)

変更後

  • 使用アクション: google-github-actions/auth@v2
  • 認証方法:
    1. Google Cloud 認証(OIDC)
    2. Firebase CLI を手動実行

参考にしたデプロイ方法②

🔗 Enabling Keyless Authentication from GitHub Actions


修正後のyml
name: CI/CD

on: [push]

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Build project
        run: npm run build

      - name: Upload dist
        uses: actions/upload-artifact@v4
        with:
          name: build
          path: dist

  deploy:
    name: Deploy
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download build artifact
        uses: actions/download-artifact@v4
        with:
          name: build
          path: dist

      # 🔵 **Google Cloud 認証(OIDC)**
      - name: Authenticate to Google Cloud
        uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }}

      # 🔵 **Firebase CLI のセットアップ**
      - name: Setup Firebase CLI
        run: npm install -g firebase-tools

      # 🔵 **Firebase へデプロイ**
      - name: Deploy to Firebase
        run: firebase deploy --only hosting
        env:
          FIREBASE_PROJECT_ID: ${{ secrets.FIREBASE_PROJECT_ID }}

終わりに

今回は Firebase の公式 GitHub Actions を使用したデプロイで認証エラーが発生したため、Google Cloud の認証を用いる方法に切り替えました。
今後、原因の詳細を調査し、可能であれば公式アクションの利用に再挑戦したいと考えています!

参考サイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?