Cloud9 environment variables - ruby-on-rails

I am developing a Rails 4 app on cloud9 (c9.io). When I placed SECRET="geheim" in config file, it works fine. I tried setting an environment variable using
echo "export SECRET=geheim" >> ~/.profile
and then using ENV['SECRET'] in config file, but it doesn't work. When I type printenv SECRET in console, it returns nothing, meaning the variable is not set. How can I fix this? Thanks.

You can add environment variables on cloud9 only if you are using the run panel to run your application. In the run panel theres a ENV button at the far right side where you can set your environment variables.
Heres some documentation about setting up your run command:
https://docs.c9.io/v1.0/docs/run-an-application
Unfortunately, this doesnt work if you're running your app from the terminal as cloud9 doesnt seem to support environment variables directly from the terminal.

In the linux terminal:
% export <env-variable-name> = <env-variable-value>
For example, setting an AWS-S3 bucket:
% export PHOTOS_BUCKET='s3://edx-photo-lab/photos/'
source: https://docs.aws.amazon.com/cloud9/latest/user-guide/env-vars.html
The above solution has the problem that the variables are cleared when the Cloud9 EC2 is rebooted.
To persist the variable you must add the export statement to the ~/.bashrc. You could use vim for edit:
% sudo vim ~/.bashrc

Related

why environment variable removed from my OS?

I have several environment variable from a text.txt file , i set them with export variable = value manually one by one myself in terminal Ubuntu 18.04 but now no one of them appear in printenv !
I need to set them somehow never remove again(unless i delete them myself).any idea?thank you
Add your variables in ~/.bashrc or in /etc/environment this way on every reboot they will be exported.
in bashrc should be like this
export VARIABLE='<value>'
in /etc/environment
VARIABLE=<value>

Environment variables not found in IntelliJ using zsh

I switched to zsh from bash. I updated the shell in Preferences > Terminal settings inside RubyMine.
But, now environment variables are not being loaded inside my Rails application. I can still access them inside the terminal in RubyMine editor!
I tried printing the value of environment variable inside a yml file (where all the DB related environment variables are required). I could access the home variable but not custom variables set by me.
Database.yml file:
Output while starting Rails server in Rubymine:
Output inside Rubymine terminal:
My /etc/zshrc:
DB settings inside my vaibhavatul47_zsh_profile.sh file:
Automatic loading Environment variables from bash into IntelliJ works while reading and loading from zsh doesn't work for Intellij.
Starting IntelliJ from Terminal will load environment variables from zsh too, please try following:
open -a "IntelliJ IDEA"
Note: here IntelliJ IDEA is name of my application, in case you have renamed your IntelliJ application to something else please enter that.
Hope this helps!
Check that your env var are loaded correctly in the terminal and then open IDE from the terminal. Then check if the build configuration env vars contains the profile env vars.
Before opening the idea make sure your environment variables are actually loaded when running the terminal:
open your zsh profile (vim ~/.zshrc)
insert a test env var (something like TEST_1="mytest")
restart the terminal and check if you see TEST_1 value (echo $TEST_1)
In case you see TEST_1 value open intelij idea by entering "idea ."
Now, open your module build configuration and look for the env vars list, check if they contain your zsh env vars (or you can type "echo TEST_1" in the idea terminal)
gl:)

Unable to Access ENV variable in rails

I am trying to use env variables in my rails app and set those variable values in ubuntu 14.04
I tried setting using export command
export mongodb_username="abc"
export mongodb_password="cde"
and also tried setting them in /etc/environment and in ~/.bashsrc
and printenv gives following results
>> printenv mongodb_username
=> abc
>> printenv mongodb_password
=> cde
BUT in RAILS APP or irb the output is following
>> ENV['mongodb_password']
=> nil
>> ENV['mongodb_username']
=> nil
I am missing something? Please help!!!
When setting an environment variable's value using export, that value is available only in the shell in which it was set, and its subshells. So you'll need to export those variables in every shell in which you need them.
However, you can automate this, of course.
If you have variables that you need frequently, one approach is to put their assignments in a shell script and then source the shell script in any shells you need them in (see http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x237.html for more on this).
If it's ok to have the variables be in effect in all your shells, then a simpler way is to export them from your startup script (probably ~/.bashrc).

Ubuntu: Environment variable is deleted after closing session

I am setting an environment variable in Ubuntu 14.04 for a script to use it.
I opened the terminal and did:
export VARNAME=/home/me/folder/folder2
And then run the script and everything works fine. But anyway as soon as I close my session, the variable seems to disappear and I have to declare it again like the first time.
To set an environment variable that doesn't get erased with the closing of the terminal (Ubuntu 16.04), follow these steps:
Open .bashrc file by using the text editor of your choice. For me, it was
code ~/.bashrc as I use VS Code, but you can use vi ~/.bashrc or subl ~/.bashrc.
Add the environment variable using export VARNAME=/home/me/folder/folder2
Save the file and close.
The variable will persist even after the terminal is closed.
Actually if you set the variable via terminal it will last till shutdown. If you want to set permanent variable you have to do the following.
$ vi ~/.bash_proflle
// set the variable in the file
exit by pressing esc key and type :wq Now the path is set.

Passing many environment variables to Rails application ran by Unicorn

I need to pass a large number of environment variables to Rails application ran by Unicorn web server. The sample unicorn init script has the following lines:
APP_ROOT=/home/x/my_app/current
<...>
INIT_CONF=$APP_ROOT/config/init.conf
<...>
test -f "$INIT_CONF" && . $INIT_CONF
So I created a $APP_ROOT/config/init.conf, put all my variables there like this:
VAR1=value1
VAR2=value2
I even made this file executable (not sure if it is necessary)
And restarted Unicorn. But ENV["VAR1"] returns nothing in my application...
Is it supposed to work this way? If yes, what am I doing wrong? If no, then how can I pass many env vars into Rails app in a clean way? (without polluting global environment or putting all of them in the command line)
Update My investigation showed that shell file like this:
. init.conf
echo $VAR1
works as expected. But this one:
. init.conf
ruby -e "puts ENV['VAR1']"
does not. So . imports code into the script but env vars set this way are not transferred further.
You probably have to "export" the variables from within the config file. Does it work if you put
export VAR1=value1
export VAR2=value2
into the config file?
I would consider using foreman, specifically for its use of .env files as defined here.

Resources