kamal icon indicating copy to clipboard operation
kamal copied to clipboard

Environment variable from terminal overwriting .env.erb value

Open fabiendv opened this issue 1 year ago • 2 comments

There is an issue where the value of an environment variable set in the terminal is overwriting the value specified in the .env.erb file. The expected behavior is that the value defined in .env.erb should take precedence and overwrite any value set in the terminal.

Desired behavior

The value specified in the .env.erb file should overwrite the value set in the terminal.

How to Reproduce

Define a value in your .env.erb file:

MY_VALUE=test

In the terminal, export a different value for the same variable:

export MY_VALUE=wrongvalue

To make sure your terminal knows the value, you can edit the .bashrc.

Run the following command:

kamal envify

Check the value of the environment variable on the host machine (.kamal/env/roles/myfile.rb)

The environment variable on the host machine retains the terminal value (wrongvalue).

Expected outcome

The environment variable on the host machine should have the value from .env.erb (test).

Root cause

Screenshot 2024-06-24 at 2 29 46 PM

Version

Kamal 1.7.1

fabiendv avatar Jun 24 '24 12:06 fabiendv

this is working as expected. Kamal uses dotenv under the hood, and being able to grab environment variables from an export is a default behavior.

acidtib avatar Jun 24 '24 20:06 acidtib

this is working as expected. Kamal uses dotenv under the hood, and being able to grab environment variables from an export is a default behavior.

It can be surprising for developers when a value specified in .env.myenv is not reflected on the host machine. I believe that variables in .env.myenv should take precedence over terminal values.

However, if this behavior is indeed intended, let's close this issue.

fabiendv avatar Jun 25 '24 06:06 fabiendv

This should be resolved in Kamal 2, though the secrets are now in .kamal/secrets rather than .env.

You can get either behaviour:

#.kamal/secrets

FOO=$FOO # reads from environment
FOO=value # reads value directly

djmb avatar Sep 30 '24 07:09 djmb