Capistrano reads from .env on a deployment server

I am trying to back up a database as part of the deployment of a Capistrano (v3) script (for a NON-Rails application).

The script works fine - if I configure the hardcode database.

Now I want to load the database configuration from the .env file. On my local machine, my .env file (in the root of the repo next to the Capfile) looks like this:

DB_NAME='local_name'
DB_USER='local_user'
DB_PASSWORD='local_pw'
DB_HOST='127.0.0.1'

On the server, the .env file (which Capistrano placed in the folder sharedand marked in the folder current) looks like this:

DB_NAME='dev_name'
DB_USER='dev_user'
DB_PASSWORD='dev_pw'
DB_HOST='127.0.0.1'

However, when I start deploying the cover, I get the following:

INFO [292e2535] Running /usr/bin/env mysqldump -u local_user --password='local_pw' --databases local_name -h 127.0.0.1 | bzip2 -9 > /var/www/vhosts/xxxxx/backups/database_local_name_2014-05-29_22:52:07.sql.bz2 on <server>

.. .env, .env, . , .env.production, !

script ( Dotenv gem):

require 'dotenv'
Dotenv.load '.env'
username, password, database, host = ENV['DB_USER'], ENV['DB_PASSWORD'], ENV['DB_NAME'], ENV['DB_HOST']

!

+4
2

Dotenv::Parser.call(string). Capistrano 2.14 ( !)

desc <<-desc
  ENV test
desc
task :test do
  text = capture "cat #{shared_path}/.env"
  ENV2 = Dotenv::Parser.call(text)
  puts ENV2['DB_NAME']
end
+5

Capistrano 3

task :load_remote_environment do
  on roles(:app) do
    set :default_environment, Dotenv::Parser.call(capture("cat #{shared_path}/.env"))
  end
end
after 'deploy:set_current_revision', 'load_remote_environment'

Capfile

require 'dotenv'
0

Source: https://habr.com/ru/post/1542566/


All Articles