Next.js Discord

Discord Forum

GitHub Actions Firebase how to write files in workflow

Unanswered
Western yellowjacket posted this in #help-forum
Open in Discord
Western yellowjacketOP
I'm using NextJS 14 app router with Firebase. A file containing service account credentials is required, reasonably I won't push those creds into my repo so how do I include that file in my workflow to build the app using github secrets? I tried a lot of stuff but nothing worked, it kept throwing a "Module not found".

// server.ts
import {
    ServiceAccount,
    cert,
    getApps,
    initializeApp,
} from "firebase-admin/app";
import { Firestore, getFirestore } from "firebase-admin/firestore";
import serviceAccount from "./serviceAccount.json";
import { Auth, getAuth } from "firebase-admin/auth";

let firestore: Firestore | undefined = undefined;
let auth: Auth | undefined = undefined;

const currentApps = getApps();
if (currentApps.length <= 0) {
    if (process.env.NEXT_PUBLIC_APP_ENV === "emulator") {
        process.env["FIRESTORE_EMULATOR_HOST"] =
            process.env.NEXT_PUBLIC_EMULATOR_FIRESTORE_PATH;
        process.env["FIREBASE_AUTH_EMULATOR_HOST"] =
            process.env.NEXT_PUBLIC_EMULATOR_AUTH_PATH;
    }

    // const projectId = process.env.NEXT_PUBLIC_SA_PROJECT_ID;
    // const privateKey = process.env.NEXT_PUBLIC_SA_PRIVATE_KEY;
    // const clientEmail = process.env.NEXT_PUBLIC_SA_CLIENT_EMAIL;

    // const serviceAccount: ServiceAccount = {
    //     projectId: projectId,
    //     clientEmail: clientEmail,
    //     privateKey: privateKey
    // }
    
    const app = initializeApp({
        credential: cert(serviceAccount as ServiceAccount),
    });

    firestore = getFirestore(app);
    auth = getAuth(app);
} else {
    firestore = getFirestore(currentApps[0]);
    auth = getAuth(currentApps[0]);
}

export { firestore, auth };


I've tried creating a ServiceAccount object and use the set env variables as commented above but firebase wasn't functioning as well as when it was in a specified json file. The json file typically has 11 sets of data but idk why the ServiceAccount object only requires those 3 params.

4 Replies

Western yellowjacketOP
Directories
root(appname)/firebase/server.ts  // File that imports the SA creds
root(appname)/firebase/serviceAccount.json


// A snippet of my workflow
- name: Set Environment Variables
        run: |
          echo NEXT_PUBLIC_FIREBASE_API_KEY='${{ secrets.NEXT_PUBLIC_FIREBASE_API_KEY }}' > .env
          echo NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN='${{ secrets.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN }}' >> .env
          echo NEXT_PUBLIC_FIREBASE_DATABASE_URL='${{ secrets.NEXT_PUBLIC_FIREBASE_DATABASE_URL }}' >> .env
          echo NEXT_PUBLIC_FIREBASE_PROJECT_ID='${{ secrets.NEXT_PUBLIC_FIREBASE_PROJECT_ID }}' >> .env
          echo NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET='${{ secrets.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET }}' >> .env
          echo NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID='${{ secrets.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID }}' >> .env
          echo NEXT_PUBLIC_FIREBASE_APP_ID='${{ secrets.NEXT_PUBLIC_FIREBASE_APP_ID }}' >> .env

      - name: Create JSON
        id: create-json
        uses: jsdaniell/create-json@v1.2.3
        with:
          name: "serviceAccount.json"
          json: ${{ secrets.NEXT_SERVICE_ACCOUNT }}
          dir: "firebase/" // <-- I've logged the paths and determine this was definitely in the correct directory

      # Build and push image to Google Container Registry
      - name: Build
        run: gcloud builds submit --tag gcr.io/$PROJECT_ID/$SERVICE:$GITHUB_SHA
> appname@0.1.0 build
> next build
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

  ▲ Next.js 14.2.4
  - Environments: .env

   Creating an optimized production build ...
Failed to compile.

./firebase/server.ts
Module not found: Can't resolve './serviceAccount.json'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./app/page.tsx


> Build failed because of webpack errors
I was trying implement cicd with gcloud and docker to build and deploy my nextjs app to google cloud run

If you'd rather help on SO, I also posted here
https://stackoverflow.com/questions/78688850/missing-firebase-sa-credentials-in-github-actions
Western yellowjacketOP
I've tried this
      - name: Create file
        run: cat /home/runner/work/appname/appname/firebase/serviceAccount.json | base64

      - name: Putting data
        env:
          DATA: ${{ secrets.NEXT_SERVICE_ACCOUNT }}
        run: echo $DATA > /home/runner/work/appname/appname/firebase/serviceAccount.json

and this
- name: Create file
        run: |
          mkdir -p ./firebase
          echo '${{ secrets.NEXT_SERVICE_ACCOUNT }}' > ./firebase/serviceAccount.json