Add .env loading to capistrano deployment.

This commit is contained in:
2020-08-22 12:41:27 +01:00
parent 611f1b94ba
commit d9d7929604
2 changed files with 35 additions and 0 deletions

View File

@@ -41,3 +41,7 @@ set :keep_releases, 5
set :keep_assets, 5
# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure
namespace :deploy do
after :starting, 'envvars:load'
end

View File

@@ -0,0 +1,31 @@
namespace :envvars do
desc 'Load environment variables'
task :load do
# Grab the current value of :default_env
environment = fetch(:default_env, {})
on roles(:app) do
# Read in the environment file
env_file = fetch(:env_file, "#{fetch(:deploy_to)}/shared/.env")
lines = capture("cat #{env_file}")
lines.each_line do |line|
# Remove the "export " keyword, we have no use for that here
line = line.sub /^export /, ""
# Clean up the input by removing line breaks, tabs etc
line = line.gsub /[\t\r\n\f]+/, ""
# Grab the key and value from the line
key, value = line.split("=")
# Remove surrounding quotes if present
value = value.slice(1..-2) if value.start_with?('"') and value.end_with?('"')
# Store the value in our :default_env copy
environment.store(key, value)
end
# Finally, update the global :default_env variable again
set :default_env, environment
end
end
end