Github Actions在使用NextJS13和Firebase时存在环境机密的问题。

huangapple go评论69阅读模式
英文:

Github Actions problem with environment secrets using NextJS13 with Firebase

问题

我正在尝试使用 NextJS 13 和 Firebase 创建 GitHub 工作流程,我在开发环境中使用了 .env.local,并且已经在 GitHub 环境密钥中进行了设置。但经过多次尝试,似乎无法获取正确的密钥,我已经在 GitHub 环境中删除并重新添加了它,但仍然在 GitHub Actions 中出现错误。

以下是我的 .yml 文件:

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x, 18.x]

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - name: Make envfile
        uses: SpicyPizza/create-envfile@v1
        with:
          NEXT_PUBLIC_API_KEY: ${{ secrets.NEXT_PUBLIC_API_KEY }}
          NEXT_PUBLIC_authDomain: ${{ secrets.NEXT_PUBLIC_AUTHDOMAIN }}
          NEXT_PUBLIC_projectId: ${{ secrets.NEXT_PUBLIC_PROJECTID }}
          NEXT_PUBLIC_storageBucket: ${{ secrets.NEXT_PUBLIC_STORAGEBUCKET }}
          NEXT_PUBLIC_messagingSenderId: ${{ secrets.NEXT_PUBLIC_MESSAGINGSENDERID }}
          NEXT_PUBLIC_appId: ${{ secrets.NEXT_PUBLIC_APPID }}
          NEXT_PUBLIC_measurementId: ${{ secrets.NEXT_PUBLIC_MEASUREMENTID }}
          file_name: .env
          directory: ./
      - run: npm install
      - run: npm ci
      - run: npm run build

我的 config.ts 文件:

// 对于 Firebase JS SDK 版本 7.20.0 及更高版本,measurementId 是可选的
const firebaseConfig = {
  apiKey: process.env.NEXT_PUBLIC_API_KEY,
  authDomain: process.env.NEXT_PUBLIC_authDomain,
  projectId: process.env.NEXT_PUBLIC_projectId,
  storageBucket: process.env.NEXT_PUBLIC_storageBucket,
  messagingSenderId: process.env.NEXT_PUBLIC_messagingSenderId,
  appId: process.env.NEXT_PUBLIC_appId,
  measurementId: process.env.NEXT_PUBLIC_measurementId,
};

我的 GitHub Secrets 图片可以在以下链接中找到:

Github Actions在使用NextJS13和Firebase时存在环境机密的问题。

我的 GitHub Actions 日志图片可以在以下链接中找到:

Github Actions在使用NextJS13和Firebase时存在环境机密的问题。

英文:

I am attempting to create a github workflow using nextJS13 and firebase, i am using .env.local on my development environment and already set-up on my github environments secrets. but after so many attempts it does seems to get the correct keys, i already delete and add it back again on github environments but still error on my github actions.

here is my .yml file

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x, 18.x]

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - name: Make envfile
        uses: SpicyPizza/create-envfile@v1
        with:
          NEXT_PUBLIC_API_KEY: ${{ secrets.NEXT_PUBLIC_API_KEY }}
          NEXT_PUBLIC_authDomain: ${{ secrets.NEXT_PUBLIC_AUTHDOMAIN }}
          NEXT_PUBLIC_projectId: ${{ secrets.NEXT_PUBLIC_PROJECTID }}
          NEXT_PUBLIC_storageBucket: ${{ secrets.NEXT_PUBLIC_STORAGEBUCKET }}
          NEXT_PUBLIC_messagingSenderId: ${{ secrets.NEXT_PUBLIC_MESSAGINGSENDERID }}
          NEXT_PUBLIC_appId: ${{ secrets.NEXT_PUBLIC_APPID }}
          NEXT_PUBLIC_measurementId: ${{ secrets.NEXT_PUBLIC_MEASUREMENTID }}
          file_name: .env
          directory: ./
      - run: npm install
      - run: npm ci
      - run: npm run build

my config.ts

// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: process.env.NEXT_PUBLIC_API_KEY,
  authDomain: process.env.NEXT_PUBLIC_authDomain,
  projectId: process.env.NEXT_PUBLIC_projectId,
  storageBucket: process.env.NEXT_PUBLIC_storageBucket,
  messagingSenderId: process.env.NEXT_PUBLIC_messagingSenderId,
  appId: process.env.NEXT_PUBLIC_appId,
  measurementId: process.env.NEXT_PUBLIC_measurementId,
};

and my github secrets

Github Actions在使用NextJS13和Firebase时存在环境机密的问题。

on my github actions log

Github Actions在使用NextJS13和Firebase时存在环境机密的问题。

答案1

得分: 0

解决方法是将环境变量移到作业级别,希望这能帮助遇到相同问题的人。

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      NEXT_PUBLIC_API_KEY: ${{ secrets.NEXT_PUBLIC_API_KEY }}
      NEXT_PUBLIC_authDomain: ${{ secrets.NEXT_PUBLIC_AUTHDOMAIN }}
      NEXT_PUBLIC_projectId: ${{ secrets.NEXT_PUBLIC_PROJECTID }}
      NEXT_PUBLIC_storageBucket: ${{ secrets.NEXT_PUBLIC_STORAGEBUCKET }}
      NEXT_PUBLIC_messagingSenderId: ${{ secrets.NEXT_PUBLIC_MESSAGINGSENDERID }}
      NEXT_PUBLIC_appId: ${{ secrets.NEXT_PUBLIC_APPID }}
      NEXT_PUBLIC_measurementId: ${{ secrets.NEXT_PUBLIC_MEASUREMENTID }}
    strategy:
      matrix:
        node-version: [16.x, 18.x]

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install
      - run: npm ci
      - run: npm run build
英文:

Solve it by moving the env to job level, hope this will help anyone who encounter the same problem.

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      NEXT_PUBLIC_API_KEY: ${{ secrets.NEXT_PUBLIC_API_KEY }}
      NEXT_PUBLIC_authDomain: ${{ secrets.NEXT_PUBLIC_AUTHDOMAIN }}
      NEXT_PUBLIC_projectId: ${{ secrets.NEXT_PUBLIC_PROJECTID }}
      NEXT_PUBLIC_storageBucket: ${{ secrets.NEXT_PUBLIC_STORAGEBUCKET }}
      NEXT_PUBLIC_messagingSenderId: ${{ secrets.NEXT_PUBLIC_MESSAGINGSENDERID }}
      NEXT_PUBLIC_appId: ${{ secrets.NEXT_PUBLIC_APPID }}
      NEXT_PUBLIC_measurementId: ${{ secrets.NEXT_PUBLIC_MEASUREMENTID }}
    strategy:
      matrix:
        node-version: [16.x, 18.x]

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install
      - run: npm ci
      - run: npm run build

huangapple
  • 本文由 发表于 2023年7月20日 17:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定