This guide will walk you through setting up your development environment and deploying your Next.js application.
Before you can run the app locally, you must authenticate to Google Cloud and set up your local environment variables.
To run the server locally with Firebase Admin features, your machine needs to be authenticated.
- Install the Google Cloud CLI: If you haven't already, install the gcloud CLI.
- Log in to your account: Run the following command and follow the prompts in your browser.
gcloud auth login
- Set your project: Tell gcloud which project you're working on.
gcloud config set project report-card-generator-e3zkv - Set up Application Default Credentials: This is the crucial step that allows your local server to act as the service account.
gcloud auth application-default login
- In the root of your project, create a new file named
.env.local. - Copy the contents of
.env.exampleinto your new.env.localfile. - Get your Google AI API Key:
- Go to Google AI Studio and create an API key.
- Paste the key into
.env.local:GOOGLE_API_KEY="YOUR_API_KEY_HERE"
- Get your Firebase Web App Credentials:
- In your Firebase project, go to Project Settings (gear icon) > General tab.
- Scroll to "Your apps" and find your web app configuration object.
- Copy the values into the corresponding
NEXT_PUBLIC_FIREBASE_*variables in.env.local.
- Set Your Admin Email:
- In
.env.local, set theNEXT_PUBLIC_ADMIN_EMAILvariable:NEXT_PUBLIC_ADMIN_EMAIL="your-admin-email@example.com" - The first time you log in with this email, the app will automatically grant you the 'super-admin' role.
- In
npm run devThis is the recommended method for deploying your project for the first time.
In your project's root directory, run: firebase init hosting.
- Select a Firebase Project: Choose
report-card-generator-e3zkv. - Configure Hosting Option: Select
App Hosting: for web frameworks.... - Set Backend Region: Choose a region (e.g.,
us-central1). - Connect to GitHub: Follow the prompts to connect your GitHub account and repository.
Your live application needs secure keys. You will only store the most sensitive keys as secrets.
-
Enable the Secret Manager API for your Google Cloud project.
-
Generate a Service Account Key (if you don't have the file):
- If you haven't done so for local setup, go to the Google Cloud Console for Service Accounts. Find the service account with an email like
firebase-adminsdk-....@...gserviceaccount.com. - Click on it, go to the "Keys" tab, click "Add Key" > "Create new key", select JSON, and create it. A JSON file will be downloaded.
- If you haven't done so for local setup, go to the Google Cloud Console for Service Accounts. Find the service account with an email like
-
Run the following
gcloudcommands in your terminal to create the secrets:# Create the Google API Key secret from the value in your .env.local echo "YOUR_API_KEY_HERE" | gcloud secrets create GOOGLE_API_KEY --data-file=- # Create the Admin Email secret from the value in your .env.local echo "your-admin-email@example.com" | gcloud secrets create NEXT_PUBLIC_ADMIN_EMAIL --data-file=- # Create the Service Account secret from your downloaded JSON file gcloud secrets create FIREBASE_SERVICE_ACCOUNT --data-file=./path/to/your/downloaded-service-account-file.json
The public Firebase configuration does not need to be secret. Set these directly in App Hosting.
- Go to the Firebase Console and navigate to the App Hosting section.
- Select your backend (e.g.,
report-card-generator). - Go to the "Settings" tab.
- Under "Environment variables", click "Add variable".
- Add a variable for each
NEXT_PUBLIC_FIREBASE_*key from your.env.localfile and paste in its corresponding value.NEXT_PUBLIC_FIREBASE_API_KEYNEXT_PUBLIC_FIREBASE_AUTH_DOMAINNEXT_PUBLIC_FIREBASE_PROJECT_IDNEXT_PUBLIC_FIREBASE_STORAGE_BUCKETNEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_IDNEXT_PUBLIC_FIREBASE_APP_ID
Commit the firebase.json and apphosting.yaml files and push to your main branch.
git add .
git commit -m "Configure Firebase App Hosting"
git pushThe GitHub Action will automatically build and deploy your application.
Once your application is live, follow these steps to deploy any new changes you've made.
- If you added any new sensitive keys (like another API key), add them to Google Secret Manager following Step 2.2.
- If you added any new public environment variables, add them to your App Hosting backend settings following Step 2.3.
This is the standard process for deploying updates.
-
Add your changes: Stage all the files you've modified.
git add . -
Commit the changes: Give your update a descriptive message.
git commit -m "feat: Add new feature for student ranking" -
Push to your main branch: This is the action that triggers the automatic deployment.
git push origin main
After you push your changes, a new build and deployment will automatically start.
- Go to the Firebase Console.
- Navigate to the App Hosting section for your project.
- Select your backend.
- Go to the "Logs" tab to monitor the build and deployment process in real-time. Once it completes, your changes will be live.