Skip to content

Instantly share code, notes, and snippets.

@arbaaz
Created September 16, 2024 06:15
Show Gist options
  • Save arbaaz/23be0228873f25e4a79909f14bfc5e02 to your computer and use it in GitHub Desktop.
Save arbaaz/23be0228873f25e4a79909f14bfc5e02 to your computer and use it in GitHub Desktop.
Set Fly.io secrets in batch from .envrc
#!/usr/bin/env python3
import os
import subprocess
import tempfile
def process_line(line):
# Skip comments and empty lines
if line.strip().startswith('#') or not line.strip():
return None
# Remove 'export' and split into key-value
key, value = line.replace('export ', '', 1).split('=', 1)
# Remove quotes from the value
value = value.strip().strip('"')
return f"{key.strip()}={value}"
# Read .envrc and process lines
with open('.envrc', 'r') as envrc:
secrets = [processed for line in envrc if (processed := process_line(line))]
# Create a temporary file
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as temp_file:
temp_file.write('\n'.join(secrets))
temp_file_path = temp_file.name
# Import secrets using fly secrets import
try:
print("Importing secrets to Fly.io")
subprocess.run(['fly', 'secrets', 'import'], input='\n'.join(secrets), text=True, check=True)
print("Secrets have been imported to Fly.io")
except subprocess.CalledProcessError as e:
print(f"Error importing secrets: {e}")
# Remove the temporary file
os.unlink(temp_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment