Oan*_*hạm 6 environment-variables vue.js github-actions vite
我想在github action中设置一个环境变量。我可以在 Vite 项目运行时访问该变量。经验: .yaml
# staging.yaml - my github action file
...
env: VITE_INTERNAL = true
...
Run Code Online (Sandbox Code Playgroud)
// index.vue - my Vite project
function myTest(){
console.log(process.env.VITE_INTERNAL) // I hope to get "true" instead of "undefined"
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我,拜托。多谢!
要在 GitHub Action YAML 中设置环境变量,您可以使用env、jobs.<job_id>.env或jobs.<job_id>.steps[*].env:
name: Build
permissions:
contents: read
issues: read
checks: write
pull-requests: write
on: push
# global env
env:
VITE_INTERNAL: true
jobs:
build:
runs-on: ubuntu-latest
# or job-level env
env:
VITE_INTERNAL: true
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18.x
cache: 'npm'
- name: Install dependencies
run: npm i
- name: Build
run: npm run build
# or step-level env
env:
VITE_INTERNAL: true
Run Code Online (Sandbox Code Playgroud)
要从 JavaScript 访问环境变量,请使用import.meta.env.VITE_INTERNAL(not process.env.VITE_INTERNAL)。
您可以在此处阅读文档,但要公开环境变量,您可以按如下方式执行。
.env
VITE_INTERNAL = true
Run Code Online (Sandbox Code Playgroud)
.yaml
console.log(import.meta.env.VITE_INTERNAL)
Run Code Online (Sandbox Code Playgroud)