在部署到firebase托管之前,将env变量设置为github动作中
我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在三个级别上定义环境变量:
工作流程:在整个工作流程中可用。
工作:在工作的所有步骤中都可以使用。
步骤:仅在作业的特定步骤中可用。
对于您的情况,由于您在
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.
Job: available in all the steps of the job.
Step: available only in a specific step within a job.
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