How to access environment variables during Capistrano deploy?

Mike A

I have my rails (4.2) app running through Passenger (5.0.28) + Apache (2.4.7) on an Ubuntu (14.02) system, ruby (2.3.0) managed with rbenv . I deploy with Capistrano (3.4.0).

All my environment variables are set in a very simple profile.d script.

#!/bin/sh
export VAR1=VAL1
export VAR2=VAL2

This works like a charm. My app ENV has all the correct variables, Secrets.yml is properly populated... everything works EXCEPT for when deploying with Capistrano over ssh.

In my deploy.rb I have the following that I think is relavant:

set :ssh_options, {
forward_agent: true,
paranoid: true,
keys: "~/.ssh/id_rsa.pub"
}

Capistrano docs being incredibly limited and ssh\server config not my strong point I can't seem to figure out why my ENV variables aren't seen by Capistrano. If I run puts ENV.inspect during the deploy flow, I get things such as "TERM_PROGRAM"=>"Apple_Terminal" and my local machine user info and whatnot. Why isn't Capistrano using the remote environment? How can I amend my configuration either server side or in my deploy script to fix this?

Thanks for the help.

Matt Brictson

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How set environment variable on capistrano deploy?

From Dev

How set environment variable on capistrano deploy?

From Dev

How do I set environment variables in Capistrano 3?

From Dev

Capistrano deploy.rb file based on environment

From Dev

Using Capistrano and error occurs during execution of cap deploy

From Dev

How to specify sensitive environment variables at deploy time with Elastic Beanstalk

From Java

How to access container environment variables with kubernetes env?

From Dev

How to access PHP environment variables in a bash script?

From Dev

How to access environment variables from JSP page

From Dev

how to access Jenkins environment variables with DSL?

From Dev

How is the C library able to access environment variables?

From Dev

How can I make environment variables available during Vagrant provisioning?

From Dev

How to deploy a specific revision with capistrano 3

From Dev

How to halt a Capistrano deploy from within a task

From Dev

How to synchronize access to user variables during single session?

From Dev

How do I access environment variables in an OpenFL project xml file

From Dev

How to access environment variables in gradle java build script

From Dev

How to return only user Path in environment variables without access to Registry?

From Dev

How can I access CURRENT values of environment variables in Python

From Dev

Pass environment variables to Dockerfile during build time

From Dev

Updating .bashrc and environment variables during Vagrant provisioning

From Dev

How deploy my environment with git?

From Dev

how to deploy jersey production environment

From Dev

Access environment variables from Foxx

From Dev

Access to environment variables by xaml code

From Dev

Log access to environment variables in windows

From Dev

Rails: How to set up db:schema:load for initial deploy with Capistrano

From Dev

capistrano 3 Don't know how to build task 'deploy:updated'

From Dev

How to execute bundle command from Capistrano deploy script

Related Related

  1. 1

    How set environment variable on capistrano deploy?

  2. 2

    How set environment variable on capistrano deploy?

  3. 3

    How do I set environment variables in Capistrano 3?

  4. 4

    Capistrano deploy.rb file based on environment

  5. 5

    Using Capistrano and error occurs during execution of cap deploy

  6. 6

    How to specify sensitive environment variables at deploy time with Elastic Beanstalk

  7. 7

    How to access container environment variables with kubernetes env?

  8. 8

    How to access PHP environment variables in a bash script?

  9. 9

    How to access environment variables from JSP page

  10. 10

    how to access Jenkins environment variables with DSL?

  11. 11

    How is the C library able to access environment variables?

  12. 12

    How can I make environment variables available during Vagrant provisioning?

  13. 13

    How to deploy a specific revision with capistrano 3

  14. 14

    How to halt a Capistrano deploy from within a task

  15. 15

    How to synchronize access to user variables during single session?

  16. 16

    How do I access environment variables in an OpenFL project xml file

  17. 17

    How to access environment variables in gradle java build script

  18. 18

    How to return only user Path in environment variables without access to Registry?

  19. 19

    How can I access CURRENT values of environment variables in Python

  20. 20

    Pass environment variables to Dockerfile during build time

  21. 21

    Updating .bashrc and environment variables during Vagrant provisioning

  22. 22

    How deploy my environment with git?

  23. 23

    how to deploy jersey production environment

  24. 24

    Access environment variables from Foxx

  25. 25

    Access to environment variables by xaml code

  26. 26

    Log access to environment variables in windows

  27. 27

    Rails: How to set up db:schema:load for initial deploy with Capistrano

  28. 28

    capistrano 3 Don't know how to build task 'deploy:updated'

  29. 29

    How to execute bundle command from Capistrano deploy script

HotTag

Archive