Next.js 部署 Firebase:GitHub Actions 自動化

更新 發佈閱讀 16 分鐘

因為在設定時遇到了許多問題,因此撰寫筆記提醒自己,也希望能夠幫助到其他需要的人!

  1. 建立 Next.js 專案
    確保有一個可正常 build 的專案,在本地可先用 npm run build 測試
  2. 安裝 Firebase CLI
    npm install -g firebase-tools
  3. 初始化 Firebase 專案
    npx firebase login
    npx firebase init
    在詢問你使用哪個功能的選單中選擇 App Hosting 選項,並依照指示命名並開通付款帳戶。
√ Are you ready to proceed? Yes
√ Which Firebase features do you want to set up for this
directory? Press Space to select features, then Enter to confirm
your choices. App Hosting: Enable web app deployments with App
Hosting
=== Project Setup
First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.
√ Please select an option: Use an existing project
√ Select a default Firebase project for this directory:
nextjs-firebase-demo-1 (nextjs-firebase-demo-1)
i Using project nextjs-firebase-demo-1 (nextjs-firebase-demo-1)
=== App Hosting Setup
√ App Hosting compute Service account is ready
i This command links your local project to Firebase App Hosting. You will be able to deploy your web app with `firebase deploy` after setup.
√ Please select an option Create a new backend
i === Set up your backend
√ Select a primary region to host your backend:
asia-east1
+ Location set to asia-east1.
√ Provide a name for your backend [1-30 characters]
nextjs-demo-backend
+ Name set to nextjs-demo-backend
+ Created a new Firebase web app named "nextjs-demo-backend"
√ Successfully created backend!
projects/nextjs-firebase-demo-1/locations/asia-east1/backends/nextjs-demo-backend
i === Deploy local source setup
√ Specify your app's root directory relative to your
firebase.json directory /
+ Wrote configuration info to firebase.json
i Writing default settings to apphosting.yaml...
+ Wrote C:\Users\xxxx\Desktop\nextjs-firebase-demo\apphosting.yaml
+ Firebase initialization complete!
+ Wrote configuration info to firebase.json
+ Wrote project information to .firebaserc
+ Firebase initialization complete!
  1. 設定 Firebase hosting
    npx firebase init hosting
You're about to initialize a Firebase project in this directory:

C:\Users\enip\Desktop\nextjs-firebase-demo

Before we get started, keep in mind:

* You are initializing within an existing Firebase project directory

√ Are you ready to proceed? Yes

=== Project Setup

First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.

i Using project nextjs-firebase-demo-1 (nextjs-firebase-demo-1)

=== Hosting Setup
√ Detected an existing Next.js codebase in the current
directory, should we use this? Yes
√ In which region would you like to host server-side content, if
applicable? asia-east1 (Taiwan)
√ Set up automatic builds and deploys with GitHub? Yes

i Detected a .git folder at C:\Users\enip\Desktop\nextjs-firebase-demo
i Authorizing with GitHub to upload your service account to a GitHub repository's secrets store.

Visit this URL on this device to log in:
Waiting for authentication...

+ Success! Logged into GitHub as enip2473

√ For which GitHub repository would you like to set up a GitHub
workflow? (format: user/repository)
enip2473/nextjs-firebase-demo

+ Created service account github-action-1011280354 with Firebase Hosting admin permissions.
+ Uploaded service account JSON to GitHub as secret FIREBASE_SERVICE_ACCOUNT_NEXTJS_FIREBASE_DEMO_1.
i You can manage your secrets at https://github.com/enip2473/nextjs-firebase-demo/settings/secrets.

√ Set up the workflow to run a build script before every deploy?
Yes
√ What script should be run before every deploy? npm ci && npm
run build

+ Created workflow file C:\Users\enip\Desktop\nextjs-firebase-demo\.github/workflows/firebase-hosting-pull-request.yml
√ Set up automatic deployment to your site's live channel when a
PR is merged? Yes
√ What is the name of the GitHub branch associated with your
site's live channel? main

+ Created workflow file C:\Users\enip\Desktop\nextjs-firebase-demo\.github/workflows/firebase-hosting-merge.yml

i Action required: Visit this URL to revoke authorization for the Firebase CLI GitHub OAuth App:
https://github.com/settings/connections/applications/89cf50f02ac6aaed3484
i Action required: Push any new workflow file(s) to your repo

+ Wrote configuration info to firebase.json
+ Wrote project information to .firebaserc

+ Firebase initialization complete!
有時候會報找不到 service account 錯誤,再執行一次 npx firebase init hosting即可。
  1. 更改 .github/workflows/firebase-hosting-merge.yml,確保在 build_and_deploy 的 job 後面加上 env FIREBASE_CLI_EXPERIMENTS: webframeworks,來啟用 firebase 的測試功能。
如果 FIREBASE_CLI_EXPERIMENTS 已經存在可忽略這一步
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
on:
push:
branches:
- main
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: ${{ secrets.GITHUB_TOKEN }}
firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_xxxxxx}}
channelId: live
projectId: xxxxxx
env:
FIREBASE_CLI_EXPERIMENTS: webframeworks
  1. Google Cloud Console 中,找到你新的 project,開啟其 Cloud Billing APICloud Function APICompute Engine APIFirebase Extensions APIEventArc API 功能
  2. IAM & Admin 中,找到 Github Action 的帳號,點擊右邊的 Edit,給予它以下 role
    Cloud Functions Admin、Cloud Functions Developer、Cloud Run Viewer、Cloud RuntimeConfig Admin、Firebase Authentication Admin、Firebase Extensions Viewer、Firebase Hosting Admin、Service Account User
  3. Push 到 Github,等待 Github Workflow 執行
    git add . && git commit -m "Initialize Github Action" && git push
  4. 等待五分鐘左右,應該就能看到成功部署,若遇到問題可仔細觀看 Error Log,會顯示缺少的權限名稱。

以上就是完整的教學啦!希望有成功幫助到你!

我提供了一個 github repo 作為 template,裡頭的 github action workflow 以及 commit 紀錄、action 出現的錯誤訊息等都可以作為你的參考。

https://github.com/enip2473/nextjs-firebase-demo

如果覺得有幫助的話,可以追蹤我,了解更多有趣知識!

留言
avatar-img
松。科技隨筆
0會員
3內容數
22 歲,軟體工程師。分享軟體開發、生成式 AI 應用、區塊鏈等我有興趣的主題~
你可能也想看
Thumbnail
你好,在下最近在學習開發web,學了html css js,也得出一些心得,由於網路上已有許多教學,所以我會著重在如何開發出to do List,以及解釋我寫的程式碼。相關的教學我會直接貼網址。如果我有什麼地方出錯,或者是可以寫得更好,歡迎在下方留言,討論。 首先先介紹我的開發環境: 我用了vs
Thumbnail
你好,在下最近在學習開發web,學了html css js,也得出一些心得,由於網路上已有許多教學,所以我會著重在如何開發出to do List,以及解釋我寫的程式碼。相關的教學我會直接貼網址。如果我有什麼地方出錯,或者是可以寫得更好,歡迎在下方留言,討論。 首先先介紹我的開發環境: 我用了vs
Thumbnail
一開始你先把你的專案push上去後,修改vite.config.ts ,要在裡面新增  base: "/Cart/" (/放自己的專案名稱/) build: {outDir: "docs"}, 接下來你要去你的github setting 裡面 -> Page ->選Deploy fro
Thumbnail
一開始你先把你的專案push上去後,修改vite.config.ts ,要在裡面新增  base: "/Cart/" (/放自己的專案名稱/) build: {outDir: "docs"}, 接下來你要去你的github setting 裡面 -> Page ->選Deploy fro
Thumbnail
Express 是一個流行的 web 框架,使用 JavsScript 實現,執行在 node 環境上,主要用來寫後端應用。
Thumbnail
Express 是一個流行的 web 框架,使用 JavsScript 實現,執行在 node 環境上,主要用來寫後端應用。
Thumbnail
在過年期間,利用集中時間跟完Mosh Hamedani的Next.js課程,掌握了前端至後端的技術應用。本文分享了學習過程中的收穫與成果,以及對於未來應用的展望。
Thumbnail
在過年期間,利用集中時間跟完Mosh Hamedani的Next.js課程,掌握了前端至後端的技術應用。本文分享了學習過程中的收穫與成果,以及對於未來應用的展望。
Thumbnail
5 月將於臺北表演藝術中心映演的「2026 北藝嚴選」《海妲・蓋柏樂》,由臺灣劇團「晃晃跨幅町」製作,本文將以從舞台符號、聲音與表演調度切入,討論海妲・蓋柏樂在父權社會結構下的困境,並結合榮格心理學與馮.法蘭茲對「阿尼姆斯」與「永恆少年」原型的分析,理解女人何以走向精神性的操控、毀滅與死亡。
Thumbnail
5 月將於臺北表演藝術中心映演的「2026 北藝嚴選」《海妲・蓋柏樂》,由臺灣劇團「晃晃跨幅町」製作,本文將以從舞台符號、聲音與表演調度切入,討論海妲・蓋柏樂在父權社會結構下的困境,並結合榮格心理學與馮.法蘭茲對「阿尼姆斯」與「永恆少年」原型的分析,理解女人何以走向精神性的操控、毀滅與死亡。
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
背景:從冷門配角到市場主線,算力與電力被重新定價   小P從2008進入股市,每一個時期的投資亮點都不同,記得2009蘋果手機剛上市,當時蘋果只要在媒體上提到哪一間供應鏈,隔天股價就有驚人的表現,當時光學鏡頭非常熱門,因為手機第一次搭上鏡頭可以拍照,也造就傳統相機廠的殞落,如今手機已經全面普及,題
Thumbnail
背景:從冷門配角到市場主線,算力與電力被重新定價   小P從2008進入股市,每一個時期的投資亮點都不同,記得2009蘋果手機剛上市,當時蘋果只要在媒體上提到哪一間供應鏈,隔天股價就有驚人的表現,當時光學鏡頭非常熱門,因為手機第一次搭上鏡頭可以拍照,也造就傳統相機廠的殞落,如今手機已經全面普及,題
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
Thumbnail
在本篇文章中,將會設定 Cloud Run,以便每當將程式修改並推送到 GitHub 時,它都會使用 Cloud Build 自動構建和部署應用程序的最新版本。
Thumbnail
在本篇文章中,將會設定 Cloud Run,以便每當將程式修改並推送到 GitHub 時,它都會使用 Cloud Build 自動構建和部署應用程序的最新版本。
Thumbnail
本章節將引導你如何建立一個TypeScript開發環境,這包括安裝Node.js、npm和TypeScript,建立一個TypeScript項目,設置編輯器,以及編寫和編譯TypeScript代碼。在完成這些步驟之後,你將能夠編寫、編譯和運行TypeScript代碼。
Thumbnail
本章節將引導你如何建立一個TypeScript開發環境,這包括安裝Node.js、npm和TypeScript,建立一個TypeScript項目,設置編輯器,以及編寫和編譯TypeScript代碼。在完成這些步驟之後,你將能夠編寫、編譯和運行TypeScript代碼。
Thumbnail
本章目的是為讀者提供有關如何設置JavaScript開發環境的知識,包括在瀏覽器、Node.js和各種編輯器和IDE中編寫和運行JavaScript的信息。此外,本章還介紹了如何架設本地開發伺服器以模擬實際的網頁環境。這些知識對於希望開發前端應用或後端服務的JavaScript開發者來說都是必要的。
Thumbnail
本章目的是為讀者提供有關如何設置JavaScript開發環境的知識,包括在瀏覽器、Node.js和各種編輯器和IDE中編寫和運行JavaScript的信息。此外,本章還介紹了如何架設本地開發伺服器以模擬實際的網頁環境。這些知識對於希望開發前端應用或後端服務的JavaScript開發者來說都是必要的。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News