diff --git a/config/deploy.rb b/config/deploy.rb index 0bf0aa0..98ed98e 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -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 \ No newline at end of file diff --git a/lib/capistrano/tasks/envvars.rake b/lib/capistrano/tasks/envvars.rake new file mode 100644 index 0000000..8ed06dc --- /dev/null +++ b/lib/capistrano/tasks/envvars.rake @@ -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 \ No newline at end of file