Capistrano and environment variables
You might be best looking at the difference between ENVIRONMENT VARIABLES
and SHELL VARIABLES
When you fire SSH, your app will load the SHELL variables which are defined in your .bashrc
file. These only exist for the life of the shell, and therefore, we don't use them as much as ENV
vars
You may be better putting the ENV
vars in:
/etc/environment
Like this:
export ENVIRONMENT_VAR=value
This will make the variables available throughout the system, not just in different shell sessions
Update
Have you tried
Capistrano: Can I set an environment variable for the whole cap session?
set :default_env, {
'env_var1' => 'value1',
'env_var2' => 'value2'
}
Capistrano not setting env vars AWS EC2
I've found my answer here: Capistrano and API keys in ENV variables?
I had to follow any of the methods described there to set the env vars. I've actually followed the second method, using the dotenv gem.
How to access environment variables during Capistrano deploy?
First I think some clarification of terminology and Capistrano's execution model is needed.
Capistrano is a program that runs on your local machine. So ENV
within Capistrano sees your local environment, not the server's. There is no way for Capistrano to "see" the remote ENV
with plain Ruby code because the Ruby code that makes up Capistrano is not executing there.
What Capistrano does do is use SSH to send commands to the server to be executed there. Commands like mkdir
, bundle install
, and so on.
To see this illustrated, add a Capistrano task to your deployment flow that does this:
task :puts_remote_env do
on roles(:all) do
remote_env = capture("env")
puts remote_env
end
end
This will run the env
command on the remote server, capture the result, and print it to your console.
I hope this makes it more clear how Capistrano works.
So, as you can see from the puts_remote_env
output, the variables defined in your profile.d
script are not there. Why?
It is because Capistrano is using a non-login, non-interactive SSH session. In that SSH session, your profile.d
script is not being evaluated. This is explained in detail in the Capistrano FAQ: http://capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/
You need to find another way to set those variables other than profile.d
script.
You could specify them in your Capistrano configuration itself (e.g. production.rb
) like this:
set :default_env, { var1: "val1", var2: "val2" }
Capistrano will then explicitly set up that environment when it executes SSH commands.
Or you could use a tool like dotenv, which allows Rails to read variables variables from a special file instead of relying on the execution environment.
Or you could experiment with different dot file locations to see if there are some that are still evaluated even in a non-login, non-interactive session. On Ubuntu, I've had success exporting variables at the very top of ~/.bashrc
.
capistrano set and get environment variables from config to rake
Thank you for your answer, I finally got it fixed by using set :environment, "preprodv1"
Instead of set :default_env
, { 'environment' => 'preprodv1' }
And by keeping #{fetch(:environment)}
Thanks a lot for your help :)
How do I set environment variables in Capistrano 3?
use:
set :default_env, {
'PATH' => 'PATH=$PATH:/opt/rubies/ruby-2.1.1/bin'
}
as of at least capistrano 3.1
capistrano + sidekiq / unable to pick environments variables
If you are running Ubuntu, you should just be able to export the environment variables by putting them at the very top of ~/.bashrc
.
Here's an example from a machine running Ubuntu 14.04:
# ~deployer/.bashrc
export HELLO=world
To demonstrate, I have a Capistrano task that simply prints the environment:
task :env do
on roles(:all) do
execute "env"
end
end
And when I run it, you can see that HELLO=world
is indeed in the environment as expected:
$ cap production env
00:00 env
01 env
01 XDG_SESSION_ID=328
01 SHELL=/bin/bash
01 HELLO=world
01 SSH_CLIENT=xx.xx.xx.xx 16582 22
01 USER=deployer
01 SSH_AUTH_SOCK=/tmp/ssh-Twu5TDVnXb/agent.32558
01 MAIL=/var/mail/deployer
01 PATH=/home/deployer/.rbenv/shims:/home/deployer/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/b…
01 PWD=/home/deployer
01 LANG=en_US.UTF-8
01 RBENV_SHELL=bash
01 SHLVL=1
01 HOME=/home/deployer
01 LOGNAME=deployer
01 SSH_CONNECTION=xx.xx.xx.xx 16582 xx.xx.xx.xx 22
01 XDG_RUNTIME_DIR=/run/user/1000
01 _=/usr/bin/env
✔ 01 deployer@xx.xx.xx.xx 2.131s
For other Capistrano-compatible ways to pass environment variables to a Ruby process, see this answer: https://stackoverflow.com/a/39628182/4625365
using dotenv variables inside capistrano 3 custom task
I found it, I need to use Dotenv.load
before using ENV and add require 'dotenv'
in Capfile
it's actually documented in dotenv readme: https://github.com/bkeepers/dotenv#sinatra-or-plain-ol-ruby
should have read it more carefully..
Related Topics
How to Get the Current Test Filename from Rspec
How to Batch Convert Mp4 Files to Ogg with Ffmpeg Using a Bash Command or Ruby
Encryption-Decryption in Rails
Rails Fastercsv "Unquoted Fields Do Not Allow \R or \N"
How to Run Code After Each Line in Ruby
Access Google Contacts API on Ruby
Why Doesnt My Ruby Coding for Finding Prime Numbers Work
Undefined Method 'Group_By_Day' - Rails 3.2
Match Sequences of Consecutive Characters in a String
Install Nokogiri 1.6.1 Under Ruby 2.0.0P353 (Rvm Based Installation) Fails (Osx Mavericks)
Can't Get to Work Cocoapods and Yosemite
Why Does Rails 4.2 + Responders Keeps Telling Me to Add Responders to the Gemfile
Generate All "Unique" Subsets of a Set (Not a Powerset)