Github Actions - 如何使我的环境变量(存储在 .env 文件中)在我的工作流程中可用

Tar*_*ngh 8 environment-variables firebase firebase-hosting github-actions building-github-actions

我会尽量说得清楚。我也询问过相关问题,但没有得到令人信服的答复。
我使用Reactfirebase进行托管。
另外,我将firebase Web API 密钥存储在我的.env文件中。
我使用 Firebase CLI 设置 Firebase 托管,并选择根据合并或拉取请求自动部署。
安装完成后,在我的工作目录中创建了一个.github包含文件的文件夹。.yml

   .github
      - workflows
          -firebase-hosting-merge.yml
          -firebase-hosting-pull-request.yml
Run Code Online (Sandbox Code Playgroud)

因此,现在当我通过运行手动将项目(无需推送到 GitHub)部署到 firebase 时,firebase deploy一切正常,并且我的应用程序已启动并运行。
然而,当我进行更改并将更改推送到 Github 时。触发 Github 操作并开始自动部署到 firebase 进程。构建通过了所有检查。在此输入图像描述

但是,当我访问托管 URL 时,控制台中出现错误,提示Your API key is invalid, please check you have copied it correctly.
我尝试了一些解决方法,例如将我的 Firebase Web API 密钥存储到 Github 机密中并在我的.yml文件中访问它。

# 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:
      - master
jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: npm ci && npm run build --prod
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_EVENTS_EASY }}'
          channelId: live
          projectId: my-project
        env:
          REACT_APP_API_KEY: ${{secrets.REACT_APP_API_KEY}}
          FIREBASE_CLI_PREVIEWS: hostingchannels
Run Code Online (Sandbox Code Playgroud)

但我仍然收到错误。我感觉这个错误肯定是环境变量的原因。
我已将 Firebase Web API 密钥存储在.env.production根目录中的文件中。不知何故,GitHub 操作没有使用我定义的环境变量。
请让我知道如何管理我的环境变量,以便我的工作流程可以访问它。

Cam*_*oel 30

答案是将自定义环境变量放在作业之前的第一级:

name: Deploy to Firebase Hosting on merge
'on':
  push:
    branches:
      - master

env: # <--- here
  REACT_APP_API_KEY: ${{secrets.REACT_APP_API_KEY}} # <--- here

jobs:
  build_and_deploy:
...
Run Code Online (Sandbox Code Playgroud)

并在 Github > 你的项目 > 设置 > Secrets 中添加此秘密

  • 你在这里彻底救了我的命。为什么其他地方都不清楚?谢谢你太棒了。 (5认同)