如何讓 Github 操作訪問我的 .env.local 檔案以構建我的 Next JS 應用程式,而不在我的 github 存盤庫上公開 .env.local 檔案?
目前,使用 Github Actions 構建時,構建將無法訪問 .env.local(因為該檔案未推送到 Github 存盤庫)。
我有一個看起來像的 next.config.js 檔案
/** @type {import('next').NextConfig} */
const isProd = process.env.NEXT_PUBLIC_ENVIRONMENT === "PROD";
const nextConfig = {
reactStrictMode: true,
basePath: isProd ? '/XXX' : '',
assetPrefix: isProd ? '/XXX' : ''
}
module.exports = nextConfig
還有一個用于 Github Actions 的 deploy.workflow.yaml,看起來像
name: deploy-workflow
on
push:
branches:
- main # Pushing a commit to the master branch is the event that triggers the workflow.
jobs:
deploy-job:
runs-on: ubuntu-latest # Configures the job to run on a fresh Ubuntu Linux virtual machine hosted by GitHub (aka the Runner).
steps:
- uses: actions/checkout@v2 # The action to check out the repo and download the code into the Runner.
- uses: actions/setup-node@v2 # The action to install Node.js in the Runner, and allow us to run npm commands.
with:
node-version: '16'
- uses: actions/cache@v2 # This action caches the node_modules folder across builds, and makes the Runner use the cache as long as package-lock.json doesn’t change.
with:
# Next.js stores its cache in the .next/cache directory. This will persist the cache across builds for faster application rebuilds. E.g., if I only updated my codebase but not the dependencies, this avoids re-bundling the dependencies.
path: |
${{ github.workspace }}/node_modules
${{ github.workspace }}/.next/cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
- run: npm install
- run: npm run build # Builds the static files
- uses: stefanzweifel/git-auto-commit-action@v4 # This action will commit changes made in the Runner environment, and push the commit to the GitHub repo. The default commit message will be “Automated publish”.
with:
commit_message: Automated publish
- name: Deploy
uses: JamesIves/[email protected]
with:
branch: gh-pages
folder: output
uj5u.com熱心網友回復:
除了.env.local通常用于包含僅在本地需要的內容的檔案之外,您還可以使用檔案.env、.env.development或.env.production. 然后可以簽入這些檔案。
您的.env檔案可以包含全域默認值(例如一些配置)。與其他人一起,您可以覆寫特定于該環境的配置。
與.env.local檔案相反,其他三個通常會被簽入。
有關更多資訊,請參閱他們的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/449824.html
標籤:github 下一个.js 环境变量 github-动作
上一篇:是否有必要為未來的PR再次分叉?
下一篇:“運行google-github-actions/setup-gcloud@master”上的Github操作錯誤。如何解決這個問題?
