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
留言分享你的想法!
avatar-img
松。科技隨筆
0會員
3內容數
22 歲,軟體工程師。分享軟體開發、生成式 AI 應用、區塊鏈等我有興趣的主題~
你可能也想看
Thumbnail
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
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
你好,在下最近在學習開發web,學了html css js,也得出一些心得,由於網路上已有許多教學,所以我會著重在如何開發出to do List,以及解釋我寫的程式碼。相關的教學我會直接貼網址。如果我有什麼地方出錯,或者是可以寫得更好,歡迎在下方留言,討論。 首先先介紹我的開發環境: 我用了vs
Thumbnail
你好,在下最近在學習開發web,學了html css js,也得出一些心得,由於網路上已有許多教學,所以我會著重在如何開發出to do List,以及解釋我寫的程式碼。相關的教學我會直接貼網址。如果我有什麼地方出錯,或者是可以寫得更好,歡迎在下方留言,討論。 首先先介紹我的開發環境: 我用了vs
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開發者來說都是必要的。
Thumbnail
在本篇文章中,將會設定 Cloud Run,以便每當將程式修改並推送到 GitHub 時,它都會使用 Cloud Build 自動構建和部署應用程序的最新版本。
Thumbnail
在本篇文章中,將會設定 Cloud Run,以便每當將程式修改並推送到 GitHub 時,它都會使用 Cloud Build 自動構建和部署應用程序的最新版本。
Thumbnail
Express 是一個流行的 web 框架,使用 JavsScript 實現,執行在 node 環境上,主要用來寫後端應用。
Thumbnail
Express 是一個流行的 web 框架,使用 JavsScript 實現,執行在 node 環境上,主要用來寫後端應用。
Thumbnail
在過年期間,利用集中時間跟完Mosh Hamedani的Next.js課程,掌握了前端至後端的技術應用。本文分享了學習過程中的收穫與成果,以及對於未來應用的展望。
Thumbnail
在過年期間,利用集中時間跟完Mosh Hamedani的Next.js課程,掌握了前端至後端的技術應用。本文分享了學習過程中的收穫與成果,以及對於未來應用的展望。
Thumbnail
npm 做為 node 套件版本管理工具,今天來學習如何使用 npm 升級 package.json 版號管理,可以對一個專案版號管理更加制式化 major 目標升級版號 指令 npm version --new-version major 範例 v1.0.0 -> v2.0.0 mi
Thumbnail
npm 做為 node 套件版本管理工具,今天來學習如何使用 npm 升級 package.json 版號管理,可以對一個專案版號管理更加制式化 major 目標升級版號 指令 npm version --new-version major 範例 v1.0.0 -> v2.0.0 mi
Thumbnail
此篇教學 : 使用GitHub架設免費的部落格網站,輕鬆擁有自己的Blog雛型,記錄生活點滴。
Thumbnail
此篇教學 : 使用GitHub架設免費的部落格網站,輕鬆擁有自己的Blog雛型,記錄生活點滴。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News