简体   繁体   English

在部署到 firebase 主机之前,在 github 操作中设置环境变量

[英]Set env variables in a github action before deploying to firebase hosting

I have a React application that makes use of the automatic deploy Github actions that Firebase provides.我有一个 React 应用程序,它使用 Firebase 提供的自动部署 Github 操作。 My issue is that I don't push my .env file to Github, and therefore there are no environment variables built into my code when the workflow script file runs yarn build in the Github action.我的问题是我没有将我的.env文件推送到 Github,因此当工作流脚本文件在 Github 操作中运行yarn build时,我的代码中没有内置环境变量。

I tried to get around this by setting variables on my Github settings for the repo.我试图通过在回购协议的 Github 设置上设置变量来解决这个问题。 I tried both Github secrets (under "Actions") and "Environments".我尝试了 Github 秘密(在“操作”下)和“环境”。

For actions -> repository secrets, I set up a secret called REACT_APP_GOOGLE_MAPS_API_KEY and added the following script to my steps :对于操作 -> 存储库机密,我设置了一个名为REACT_APP_GOOGLE_MAPS_API_KEY的机密,并将以下脚本添加到我的steps中:

REACT_APP_GOOGLE_MAPS_API_KEY=${{secrets.REACT_APP_GOOGLE_MAPS_API_KEY}} yarn && yarn build

Then I also created an environment called env , and on actions -> environment secrets I added the same, and changed the script to:然后我还创建了一个名为env的环境,并在 actions -> environment secrets 上添加了相同的内容,并将脚本更改为:

REACT_APP_GOOGLE_MAPS_API_KEY=${{env.REACT_APP_GOOGLE_MAPS_API_KEY}} yarn && yarn build

Neither of these worked;这些都不起作用; the env vars did not get bundled into the project and I get an error when I try to load maps on my live link because of a "missing api key".环境变量没有捆绑到项目中,当我尝试在我的实时链接上加载地图时出现错误,因为“缺少 api 密钥”。

How can I set secrets/variables in github and then utilise them in my workflow scripts?如何在 github 中设置机密/变量,然后在我的工作流脚本中使用它们?

For reference, here's the boilerplate script you get from firebase:作为参考,这是您从 firebase 获得的样板脚本:

name: Deploy to Firebase Hosting on PR
"on": pull_request
jobs:
  build_and_preview:
    if: "${{ github.event.pull_request.head.repo.full_name == github.repository }}"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: yarn && yarn build # I added my vars here, before `yarn` #
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: "${{ secrets.GITHUB_TOKEN }}"
          firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT_WEBPAGE_NAME }}"
          projectId: webpage_name

You can define environment variables at three levels:您可以在三个级别定义环境变量:

Workflow: available in the entire workflow.工作流程:在整个工作流程中可用。

name: Deploy to Firebase Hosting on PR
on: pull_request
env:
  REACT_APP_GOOGLE_MAPS_API_KEY: ${{ secrets.REACT_APP_GOOGLE_MAPS_API_KEY }}
jobs:
  build_and_preview:
    ...

Job: available in all the steps of the job.作业:在作业的所有步骤中可用。

name: Deploy to Firebase Hosting on PR
on: pull_request
jobs:
  build_and_preview:
    if: "${{ github.event.pull_request.head.repo.full_name == github.repository }}"
    runs-on: ubuntu-latest
    env:
      REACT_APP_GOOGLE_MAPS_API_KEY: ${{ secrets.REACT_APP_GOOGLE_MAPS_API_KEY }}
    steps:
      ...

Step: available only in a specific step within a job.步骤:仅在作业中的特定步骤中可用。

name: Deploy to Firebase Hosting on PR
on: pull_request
jobs:
  build_and_preview:
    if: "${{ github.event.pull_request.head.repo.full_name == github.repository }}"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: yarn && yarn build
        env:
          REACT_APP_GOOGLE_MAPS_API_KEY: ${{ secrets.REACT_APP_GOOGLE_MAPS_API_KEY }}

For your case, as you only need the API key during yarn build , declaring it in the step will be the best option.对于您的情况,因为您在yarn build期间只需要 API 键,所以在步骤中声明它是最好的选择。

Ref: https://docs.github.com/es/actions/learn-github-actions/environment-variables参考: https://docs.github.com/es/actions/learn-github-actions/environment-variables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM