在部署到firebase托管之前,将env变量设置为github动作中

发布于 2025-01-22 20:56:45 字数 1441 浏览 0 评论 0原文

我有一个React应用程序,该应用程序利用Firebase提供的自动部署GITHUB动作。我的问题是,我不会将我的.env文件推向github,因此,当工作流脚本文件运行yarn build时,我的代码中没有内置的环境变量 github动作。

我试图通过在我的github设置上为仓库设置变量来解决此问题。我尝试了GitHub Secret(在“动作”)和“环境”中。

用于行动 - >存储库秘密,我设置了一个名为react_app_google_maps_api_key的秘密,并将以下脚本添加到我的steps

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

然后我还创建了一个称为envy> envy的环境,关于行动 - >环境秘密我添加了相同的内容,然后将脚本更改为:

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

这些都没有起作用; Env Vars没有捆绑到项目中,由于“缺少API密钥”,我尝试在实时链接上加载映射时会出现错误。

如何在GitHub中设置秘密/变量,然后在工作流脚本中使用它们?

作为参考,这是您从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

I have a React application that makes use of the automatic deploy Github actions that Firebase provides. 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.

I tried to get around this by setting variables on my Github settings for the repo. I tried both Github secrets (under "Actions") and "Environments".

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=${{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:

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".

How can I set secrets/variables in github and then utilise them in my workflow scripts?

For reference, here's the boilerplate script you get from 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-01-29 20:56:45

您可以在三个级别上定义环境变量:

工作流程:在整个工作流程中可用。

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:
    ...

工作:在工作的所有步骤中都可以使用。

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:
      ...

步骤:仅在作业的特定步骤中可用。

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 }}

对于您的情况,由于您在YARN构建中只需要API键,因此在步骤中声明它是最好的选择。

参考: https://docs.github.com/es/es/actions/actions/actions/actions/actions/actions/actions/actions /学习github-actions/环境变异

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文