Keeping your GitHub Actions secrets in sync with your local .env files can be a repetitive and error-prone process. Manually copying variables into GitHubβs UI every time you update .env or .env.local isnβt scalable.
In this guide, youβll learn how to:
- Automatically detect the current GitHub repository
- Upload all variables from
.envand.env.localto GitHub Actions Secrets - Use a safe and reusable Bash script with GitHub CLI
This script solves the pain of manual secret syncing by:
- π« Preventing errors from mismatched values
- β‘οΈ Speeding up onboarding or project bootstrap
- π Avoiding exposing secrets in Git
Perfect for teams, CI setups, and anyone who hates manual UI work.
To run the script, youβll need:
- β
GitHub CLI (
gh) installed β Install it here - β Authentication set up:
gh auth login- β
Your
.envand.env.localfiles present (but ignored in.gitignore)
#!/bin/bash
FILES=(".env" ".env.local")
# === Detect GitHub Repository Automatically ===
if command -v gh &>/dev/null; then
REPO=$(gh repo view --json nameWithOwner -q .nameWithOwner)
else
REPO=$(git config --get remote.origin.url | sed -E 's|.*github\.com[:/](.*)\.git|\1|')
fi
if [[ -z "$REPO" ]]; then
echo "β Could not detect GitHub repository. Set REPO manually."
exit 1
fi
echo "π Target GitHub repo: $REPO"
# === Check gh CLI is installed ===
if ! command -v gh &>/dev/null; then
echo "β GitHub CLI (gh) is not installed. Install it from https://cli.github.com/"
exit 1
fi
# === Check auth ===
if ! gh auth status &>/dev/null; then
echo "β You are not authenticated with gh CLI. Run: gh auth login"
exit 1
fi
# === Process each env file ===
for FILE in "${FILES[@]}"; do
if [ -f "$FILE" ]; then
echo "π Reading from $FILE"
while IFS='=' read -r key value; do
if [[ "$key" =~ ^\s*# || -z "$key" ]]; then
continue
fi
key=$(echo "$key" | xargs)
value=$(echo "$value" | sed -e 's/^["'"'"']//' -e 's/["'"'"']$//' | xargs)
gh secret set "$key" --repo "$REPO" --body "$value"
echo "β
Secret set: $key"
done < "$FILE"
else
echo "β οΈ File $FILE not found. Skipping."
fi
done
echo "π All secrets uploaded to $REPO"- Ignores comments (
#) and blank lines - Trims quotes from values like
TOKEN="abc123" - Works with both
.envand.env.local - Skips missing files gracefully
- Save it as
upload-secrets.sh - Make it executable:
chmod +x upload-secrets.sh- Run it inside your Git project:
./upload-secrets.shYou'll see output like:
π Reading from .env
β
Secret set: API_KEY
β
Secret set: DB_PASSWORD
...
π All secrets uploaded to fornfun/18plus
- This script uploads to repository-level secrets (available in all workflows).
- For environment-specific secrets (
production,staging), you can extend the script with--env:
gh secret set "$key" --env "production" --repo "$REPO" --body "$value"- Never commit
.envor.env.localto Git - Rotate secrets periodically
- Restrict Actions access to trusted users and branches
This tiny script makes life easier when managing secrets in GitHub Actions. Whether you're syncing development keys or automating CI/CD, it saves time, reduces errors, and keeps your workflow clean.
Author: @sh20raj π‘ Built for devs who love automation and hate manual UI work.