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?

【エラー対応】GitHub ActionsでFirebaseデプロイ時に発生した “Directory ‘dist’ for Hosting does not exist.” の解決方法

Posted at

はじめに

GitHub Actionsを使ってFirebaseにデプロイしようとした際にエラーが発生しました。あわせてGitHub Actions自体の仕組みも理解できていなかったため、学びの記録として記事にまとめました。

問題

GitHub ActionsでFirebaseにデプロイしようとしたところ、以下のエラーが発生しました。

エラーメッセージ
Run ./node_modules/.bin/firebase deploy

=== Deploying to '***'...

i  deploying hosting
i  hosting[***]: beginning deploy...

Error: Directory 'dist' for Hosting does not exist.
Error: Process completed with exit code 1.

問題のコードになります。

studyRecord.yml
name: studyRecord
on:
    push:
      branches: [main]
    workflow_dispatch:
jobs:
    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: "22"

            - name: Install dependencies
              run: npm install

            - name: Run build
              run: npm run build

    test:
        runs-on: ubuntu-latest
        needs: build
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
            
            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                node-version: "22"

            - name: Install dependencies
              run: npm install

            - name: Run tests
              run: npm run test   
              
    deploy:
        runs-on: ubuntu-latest
        needs: test
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
            
            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                node-version: "22"

            - name: Install firebase-tools
              run: npm install --save-dev firebase-tools

            - name: Decode Firebase service account key
              run: |
                echo "${{ secrets.FIREBASE_KEY }}" | base64 -d > ./firebase-key.json
                echo "GOOGLE_APPLICATION_CREDENTIALS=${{ github.workspace }}/firebase-key.json" >> $GITHUB_ENV
            - name: change space
              run: ./node_modules/.bin/firebase use ${{ secrets.FIREBASE_PROJECT_ID }}
            - name: Deploy to Firebase Hosting
              run: |
                ./node_modules/.bin/firebase deploy
                env:
                FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
                FIREBASE_CLI_EXPERIMENTS: webframeworks
            - name: delete GOOGLE_APPLICATION_CREDENTIALS
              run: rm $GOOGLE_APPLICATION_CREDENTIALS
              if: ${{ always() }}

解決方法

原因は、buildジョブで生成された成果物(distフォルダ)が、deployジョブに引き継がれていなかったことでした。ビルド成果物を artifact としてアップロード・ダウンロードする必要がありました。
以下に修正後のコードになります。

    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: "22"

            - name: Install dependencies
              run: npm install

            - name: Run build
              run: npm run build
              
              ##ビルドでできた dist フォルダをデプロイで使えるように保存
            - name: Upload build-artifacts
              uses: actions/upload-artifact@v4
              with:
                name: build-artifacts
                path: dist
    deploy:
        runs-on: ubuntu-latest
        needs: test
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
            
            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                node-version: "22"

            - name: Install dependencies
              run: npm install
        
              ## ビルドジョブで生成&保存されたdistフォルダを取得
            - name: Download build artifacts
              uses: actions/download-artifact@v4
              with:
                name: build-artifacts
                path: dist

            - name: Install firebase-tools
              run: npm install --save-dev firebase-tools

            - name: Decode Firebase service account key
              run: |
                echo "${{ secrets.FIREBASE_KEY }}" | base64 -d > ./firebase-key.json
                echo "GOOGLE_APPLICATION_CREDENTIALS=${{ github.workspace }}/firebase-key.json" >> $GITHUB_ENV
            
            - name: change space
              run: ./node_modules/.bin/firebase use ${{ secrets.FIREBASE_PROJECT_ID }}

            - name: Deploy to Firebase Hosting
              run: |
                ./node_modules/.bin/firebase deploy
              env:
                FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
                FIREBASE_CLI_EXPERIMENTS: webframeworks

            - name: delete GOOGLE_APPLICATION_CREDENTIALS
              run: rm $GOOGLE_APPLICATION_CREDENTIALS
              if: ${{ always() }}
    

おわりに

GitHub Actionsでは、ジョブごとに別々の仮想環境(VM)で実行されるため、ジョブ間でファイルを直接引き継ぐことはできないようです。
そのため、ビルド成果物をアーティファクトとしてアップロードし、他のジョブでダウンロードすることで間接的に共有する必要がありました。

ビルドファイルの参照先が間違っているのだと思い込んでしまい、原因の特定に時間がかかってしまいました。
しかし今回の経験を通して、GitHub Actionsの仕組みや、ジョブ間でのデータのやり取りについて理解を深めることができました。

参考

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?