我有一個使用 Firebase 提供的自動部署 Github 操作的 React 應用程式。我的問題是我沒有將檔案推送到 Github,因此當作業流腳本檔案在 Github 操作中.env運行時,我的代碼中沒有內置環境變數。yarn build
我試圖通過在我的 Github 設定中為 repo 設定變數來解決這個問題。我嘗試了 Github 的秘密(在“Actions”下)和“Environments”。
對于操作 -> 存盤庫機密,我設定了一個名為的機密REACT_APP_GOOGLE_MAPS_API_KEY并將以下腳本添加到我的steps:
REACT_APP_GOOGLE_MAPS_API_KEY=${{secrets.REACT_APP_GOOGLE_MAPS_API_KEY}} yarn && yarn build
然后我還創建了一個名為 的環境env,并在操作 -> 環境機密上添加了相同的內容,并將腳本更改為:
REACT_APP_GOOGLE_MAPS_API_KEY=${{env.REACT_APP_GOOGLE_MAPS_API_KEY}} yarn && yarn build
這些都不起作用。環境變數沒有捆綁到專案中,當我嘗試在實時鏈接上加載地圖時出現錯誤,因為“缺少 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
uj5u.com熱心網友回復:
您可以在三個級別定義環境變數:
作業流程:在整個作業流程中可用。
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 }}
對于您的情況,由于您在 期間只需要 API 密鑰yarn build,因此在步驟中宣告它將是最佳選擇。
參考:https ://docs.github.com/es/actions/learn-github-actions/environment-variables
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/460270.html
標籤:反应 火力基地 github github-动作 firebase 托管
