Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set KAMAL_DESTINATION for envify command #666

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nickhammond
Copy link
Contributor

@nickhammond nickhammond commented Feb 1, 2024

Since destinations usually map to an environment such as a Rails environment it'd be helpful to load the current destination so that you can more easily use a destination template file. I made the assumption that we load the clear variables from Kamal but right now envify just loads the template and writes the output destination file, no clear/secret loading from Kamal.

For example:

.env.template.erb

# Generated by kamal envify, to regenerate run `kamal envify -d <%= ENV['KAMAL_DESTINATION'] %>`

RAILS_ENV=<%= ENV['KAMAL_DESTINATION'] %>

API_KEY_EXAMPLE=<%= `op read "op://hey-#{ENV['KAMAL_DESTINATION']}/API_KEY_EXAMPLE/credential" -n`.strip %>

.env.staging.erb and .env.production.erb

<%= 
  ERB.new(File.read(".env.template.erb"), trim_mode: "-").result(binding)
%>

Which would generate something like this when running kamal envify -d staging:

# Generated by kamal envify, to regenerate run `kamal envify -d staging`

RAILS_ENV=staging

API_KEY_EXAMPLE=secret

#352 is related from the perspective of having a template for your destination files.

@igor-alexandrov
Copy link
Contributor

Maybe I am missing something but the result you want can be achieved with:

# .env.template.erb

API_KEY_EXAMPLE=<%= `op read "op://hey-#{ENV['KAMAL_DESTINATION']}/API_KEY_EXAMPLE/credential" -n`.strip %>

and

# .env.staging.erb
RAILS_ENV=staging
<%= ERB.new(File.read(".env.template.erb"), trim_mode: "-").result(binding) %>

@nickhammond
Copy link
Contributor Author

@igor-alexandrov ENV['KAMAL_DESTINATION'] is only populated in the hooks at the moment I believe. But yes, I'm doing something similar to your .env.staging.erb example.

@jeromedalbert
Copy link

jeromedalbert commented May 17, 2024

Yeah according to the source code ENV['KAMAL_DESTINATION'] seems to be available only for hooks.

I am wondering if it would make sense to make ENV['KAMAL_DESTINATION'] available globally, not just for envify. I have found the need to use ENV['KAMAL_DESTINATION'] in my deploy.yml so I don't have to hardcode the word "production" / "staging" / etc in my yaml files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants