Auto-sync .env File to GitHub Actions
Good to know
NEXTY.DEV v3.2.16 introduces two new production environment variable sync scripts. If your local code doesn't have these scripts, please download them from the source repository:
sync-env-to-github.mjs: Batch creates environment variables from your local.envfile into GitHub Actions Secrets and Variablesclear-env-form-github.mjs: Clears environment variables from GitHub Actions Secrets and Variables
This guide shows you how to use scripts to automatically batch create environment variables from your local .env file into GitHub Actions Secrets and Variables.
Prerequisites
- Install GitHub CLI
MacOS:
brew install ghWindows:
winget install --id GitHub.cli
or
scoop install gh- Login to GitHub
gh auth loginFollow the interactive prompts to complete the login.
- Ensure Repository Permissions
You need Admin permissions on the target repository to create Secrets and Variables.
Script Usage
In your package.json file, find the scripts configuration and add the following commands:
"scripts": {
"env:clear": "node scripts/clear-env-from-github.mjs",
"env:sync": "node scripts/sync-env-to-github.mjs",
}Before each sync, first clear the environment variables in GitHub Actions Secrets and Variables (optional but recommended):
npm run env:clearThen sync the environment variables from your local .env file to GitHub Actions Secrets and Variables:
npm run env:syncWhether clearing or syncing, the execution results will be displayed, for example:
✅ Done in 10.9s!
📦 Variables created: 19
🔐 Secrets created: 25
⏭️ Skipped: 9If you encounter a message indicating that some environment variables failed to clear or sync, simply run the command again.
Security Notes
- ⚠️ Never commit your
.envfile to Git - Ensure
.envis listed in.gitignore - Secrets are stored encrypted on GitHub. When editing Secrets, they will appear blank - this is normal behavior and nothing to worry about.