Skip to content

Instantly share code, notes, and snippets.

@sanjeed5
Created March 22, 2025 16:30
Show Gist options
  • Select an option

  • Save sanjeed5/2b9e1b662f8d340adeed292110d306ed to your computer and use it in GitHub Desktop.

Select an option

Save sanjeed5/2b9e1b662f8d340adeed292110d306ed to your computer and use it in GitHub Desktop.
Upload all .env to Cloudflare Pages with one script
#!/bin/bash
# Script to automatically upload all environment variables from .env to Cloudflare Pages
echo "Uploading environment variables to Cloudflare Pages project:"
# Read each non-comment line from the .env file
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip empty lines or comments
if [[ -z "$line" || "$line" == \#* ]]; then
continue
fi
# Extract variable name and value
if [[ "$line" =~ ^([^=]+)=(.*)$ ]]; then
varname="${BASH_REMATCH[1]}"
varvalue="${BASH_REMATCH[2]}"
# Remove quotes if present
varvalue="${varvalue%\"}"
varvalue="${varvalue#\"}"
varvalue="${varvalue%\'}"
varvalue="${varvalue#\'}"
echo "Adding secret: $varname"
# Use wrangler to set the secret
echo "$varvalue" | wrangler pages secret put "$varname" --project-name
fi
done < .env
echo "Environment variables upload complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment