英文:
.env variables not being accessed in deployment in Github pages (Vite + react app)
问题
Repo: https://github.com/SyntaxWarrior30/Java-Docs-Generator
我将站点部署为主分支的 gh-pages,使用 dist 文件夹。
由于 .env 文件中存储的 API 密钥在 .gitignore 中被忽略,因此 Github 页面不起作用,因为无法访问该密钥。我在我的 Github 页面环境中创建了一个秘密变量,但仍然不起作用。请问如何将 .env 变量添加到我的 Github 页面,以便我的 fetch 函数正常工作。谢谢您的时间。
我的 .env.production 文件如下:
VITE_API_KEY={My API Key}
我尝试通过导航到设置 > 秘密 > 新存储库秘密 添加一个新的存储库秘密。将秘密命名为与我在 .env 中定义的变量名相同,不包括 VITE_ 前缀。然而,这并未解决问题。
我还尝试了在 Github 中为秘密变量名称包括 VITE_ 前缀的解决方案。
如果需要,以下是我用来部署项目的 static.yml:
# 部署静态内容到 GitHub 页面的简单工作流
name: 部署静态内容到 Pages
on:
# 当推送目标默认分支时运行
push:
branches: ['main']
# 允许您从操作选项卡手动运行此工作流
workflow_dispatch:
# 将 GITHUB_TOKEN 权限设置为允许部署到 GitHub 页面
permissions:
contents: read
pages: write
id-token: write
# 允许一个并发部署
concurrency:
group: 'pages'
cancel-in-progress: true
jobs:
# 由于我们只是部署,所以只有一个部署工作
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: 检出
uses: actions/checkout@v3
- name: 设置 Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: 安装依赖项
run: npm install
- name: 设置 Pages
uses: actions/configure-pages@v3
- name: 上传构件
uses: actions/upload-pages-artifact@v1
with:
# 上传 dist 存储库
path: './dist'
- name: 部署到 GitHub 页面
id: deployment
uses: actions/deploy-pages@v1
错误证据:查看图像描述
英文:
Repo:https://github.com/SyntaxWarrior30/Java-Docs-Generator
I deployed the site as a gh-pages of the main branch using the dist folder.
The Github pages site doesn’t work since the api key, stored in the .env file, which was ignored with .gitignore, is not being accessed. I created a secret variable in my Github pages environment yet it is still not working. How do I add .env variables to my Github pages so my fetch function works properly. Thank you for your time.
What my .env.production file looks like:
VITE_API_KEY={My API Key}
I tried adding a new repository secret by navigating to Settings > Secrets > New repository secret. Naming the secret the same as the variable name I defined in .env, without the VITE_ prefix. However, this did not fix the problem.
I also tried the solution where I include the VITE_ prefix for the Secret variable name in Github.
Here is the static.yml that i used to deploy the project, if needed:
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ['main']
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets the GITHUB_TOKEN permissions to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow one concurrent deployment
concurrency:
group: 'pages'
cancel-in-progress: true
jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: Install dependencies
run: npm install
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload dist repository
path: './dist'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
Evidence of error:enter image description here
答案1
得分: 1
Vite 需要使用 vite build
(或等效的包管理器命令)进行构建和部署。在 vite build
过程中,这些值将被注入到新的代码中。这是因为它是在浏览器上运行的客户端代码,所以必须这样做。这些值可以来自.env
文件、特殊的.env.production
文件或环境变量。请参考 Vite 文档中的 .env 文件 和 部署到生产环境。
在部署到 Github 之前,请确保完成这些步骤。
如果你想使用 Github actions 进行构建和部署,请参考 vite-deploy-demo 以及 Vite 文档中的 Github Pages 部署部分。
在 vite build
步骤中,你的 VITE 值必须作为环境变量可用。
- 将你的生产环境 VITE 值添加为 Github Secrets。
- 使它们在构建步骤中可用。
- name: 构建项目
run: vite build
env:
VITE_API_KEY: ${{ secrets.VITE_API_KEY }}
英文:
Vite needs to be built and deployed with vite build
(or your package manager's equivalent). During vite build
, the values are injected into the new code. They'd have to be since this is client code run on the browser. They can come from either .env
, or a special .env.production
, or environment variables. See .env files in the Vite docs and Deploying For Production.
Make sure this is done before deploying to Github.
If you want to build and deploy with Github actions, see vite-deploy-demo and the Vite docs for Github Pages.
Your VITE values must be available as environment variables during the vite build
step.
- Add your production VITE values as Github Secrets.
- Make them available to the build step.
- name: Build project
run: vite build
env:
VITE_API_KEY: ${{ secrets.VITE_API_KEY }}
答案2
得分: 0
这是您的YAML文件的翻译:
# 我使程序工作了,以下是我的 .yml 文件供参考:
# 用于将静态内容部署到 GitHub Pages 的简单工作流
name: 部署静态内容到 Pages
on:
# 在推送到默认分支时运行
push:
branches: ['main']
# 允许您从“Actions”选项卡手动运行此工作流
workflow_dispatch:
# 设置 GITHUB_TOKEN 权限以允许部署到 GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# 允许一次并发部署
concurrency:
group: 'pages'
cancel-in-progress: true
jobs:
# 由于我们只是部署,所以只有一个部署任务
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: 检出
uses: actions/checkout@v3
- name: 设置 Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: 安装依赖项
run: npm install
- name: 构建
run: npm run build
env:
VITE_API_KEY: ${{ secrets.VITE_API_KEY }}
- name: 设置 Pages
uses: actions/configure-pages@v3
- name: 上传构建结果
uses: actions/upload-pages-artifact@v1
with:
# 上传 dist 存储库
path: './dist'
- name: 部署到 GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
请注意,代码部分未被翻译。
英文:
I got the program to work, below is my .yml file for reference:
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ['main']
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets the GITHUB_TOKEN permissions to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow one concurrent deployment
concurrency:
group: 'pages'
cancel-in-progress: true
jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
env:
VITE_API_KEY: ${{ secrets.VITE_API_KEY }}
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload dist repository
path: './dist'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论