Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Created December 4, 2025 22:22
Show Gist options
  • Select an option

  • Save dpaluy/0b6e6f9b46e1aba6a618f9f46d19d94d to your computer and use it in GitHub Desktop.

Select an option

Save dpaluy/0b6e6f9b46e1aba6a618f9f46d19d94d to your computer and use it in GitHub Desktop.
Merge Rails encrypted credentials
#!/usr/bin/env ruby
require 'open3'
environment = ARGV[0] || 'default'
creds_path = "config/credentials#{environment == 'default' ? '' : '/' + environment}.yml.enc"
puts "\n=== Merging credentials conflict for #{creds_path} ===\n"
# Extract both versions
`git checkout --ours #{creds_path}`
ours = `rails encrypted:show #{creds_path} 2>/dev/null || rails credentials:show -e #{environment} 2>/dev/null`
File.write('tmp/ours.yml', ours)
`git checkout --theirs #{creds_path}`
theirs = `rails encrypted:show #{creds_path} 2>/dev/null || rails credentials:show -e #{environment} 2>/dev/null`
File.write('tmp/theirs.yml', theirs)
# Show visual diff
puts "Your version (ours) vs Incoming (theirs):"
puts `diff -u tmp/ours.yml tmp/theirs.yml || true`
puts "\n"
# Open editor for manual merge
`rails credentials:edit -e #{environment}`
puts "Credentials merged and encrypted. Stage with: git add #{creds_path}"
`rm tmp/ours.yml tmp/theirs.yml 2>/dev/null`
@dpaluy
Copy link
Author

dpaluy commented Dec 4, 2025

  1. Add to bin/merge_credentials.sh

  2. chmod +x bin/merge_credentials.sh

  3. Usage:

# For default credentials
bin/merge_credentials.sh

# For environment-specific (e.g., production)
bin/merge_credentials.sh production

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment