[Kamal 2] Are ENV variables set in .env available in .kamal/secrets? #977
-
After initializing a new project with Kamal # config/deploy.yml
# ...
registry:
username: my-username
password:
- KAMAL_REGISTRY_PASSWORD # .kamal/secrets
KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD # .env
KAMAL_REGISTRY_PASSWORD="<REDACTED>" Running $ kamal registry login
INFO [078e4b73] Running docker login -u [REDACTED] -p [REDACTED] as me@localhost
DEBUG [078e4b73] Command: docker login -u [REDACTED] -p [REDACTED]
DEBUG [078e4b73] flag needs an argument: 'p' in -p
ERROR (SSHKit::Command::Failed): docker exit status: 256
docker stdout: Nothing written
docker stderr: flag needs an argument: 'p' in -p This leads me to believe that |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 31 replies
-
@jessevdp There are some WIP docs for 2.0, here are a few helpful pages: .env to Kamal secrets: https://github.com/basecamp/kamal-site/blob/kamal-2/docs/upgrading/secrets-changes.md |
Beta Was this translation helpful? Give feedback.
-
Hi, With Kamal
With Kamal
... and when I run
(There is nothing weird at line 51: just another secret: Any clue? |
Beta Was this translation helpful? Give feedback.
-
@jessevdp Are you using |
Beta Was this translation helpful? Give feedback.
-
This is the solution I went with. Nothing else seemed to work other than setting them in ~/.zshrc but that conflicted with my development env. .kamal/secret APPSIGNAL_PUSH_API_KEY=$(cat .env.production | grep APPSIGNAL_PUSH_API_KEY | cut -d '=' -f 2)
ELASTIC_APM_SERVER_URL=$(cat .env.production | grep ELASTIC_APM_SERVER_URL | cut -d '=' -f 2)
ELASTICSEARCH_URL=$(cat .env.production | grep ELASTICSEARCH_URL | cut -d '=' -f 2)
KAMAL_REGISTRY_PASSWORD=$(cat .env.production | grep KAMAL_REGISTRY_PASSWORD | cut -d '=' -f 2)
LEGISCAN_API_KEY=$(cat .env.production | grep LEGISCAN_API_KEY | cut -d '=' -f 2)
LEGISCAN_DATABASE_URL=$(cat .env.production | grep LEGISCAN_DATABASE_URL | cut -d '=' -f 2)
RAILS_MASTER_KEY=$(cat config/credentials/production.key)
VOTER_DATA_URL=$(cat .env.production | grep VOTER_DATA_URL | cut -d '=' -f 2)
S3_ACCESS_KEY_ID=$(cat .env.production | grep AWS_ACCESS_KEY_ID | cut -d '=' -f 2)
S3_SECRET_ACCESS=$(cat .env.production | grep AWS_SECRET_ACCESS_KEY | cut -d '=' -f 2) |
Beta Was this translation helpful? Give feedback.
-
I prefer using Rails # lib/tasks/credentials.rake
namespace :credentials do
desc "Read a specific credential"
task read: :environment do
key_path = ENV['KEY'].to_s.split(',').map(&:to_sym)
value = Rails.application.credentials.dig(*key_path)
puts value
end
end This task is then utilized in the Kamal secrets file: # .kamal/secrets
# Read secrets based on the specific environment
KAMAL_REGISTRY_PASSWORD=$(RAILS_ENV=development KEY=kamal,registry_password bin/rails credentials:read)
MYSQL_ROOT_PASSWORD=$(RAILS_ENV=production KEY=database,password bin/rails credentials:read)
# From bin/rails credentials:edit --environment=production
RAILS_MASTER_KEY=$(cat config/credentials/production.key) This approach offers several advantages:
|
Beta Was this translation helpful? Give feedback.
-
In Kamal 2, the following line causes confusion, as it implies that the key is being loaded from the .env file. This is due to the text referencing "SECRET_FROM_ENV".
|
Beta Was this translation helpful? Give feedback.
-
I'm not sure I'm "at the right desk" but anyways here goes: I found My deploys have started to fail - and I don't really know where to start debug 😢
the config/deploy.yml actually looks more like this:
In
I suspect the missing ENV's are due to Kamal not loading the ENV "early on" but that's just guesswork... |
Beta Was this translation helpful? Give feedback.
-
@jessevdp thx for noting but with R8 and 'no-build' - and your point (This is a rails & built-time thing, not a Kamal thing.) - where does that leave me? If all is well in development* then I reasoned that it had to be during the 'docker build' step (which I believe is a Kamal thing) Solution:
I hope, some day, I'll find a way to avoid the credentials alltogether Again @jessevdp - thx for pointing the catalyzing finger forcing me on my way 🫶 * I don't do assets:precompile - only to drive my point
|
Beta Was this translation helpful? Give feedback.
-
Hello! I am having a similar issue.
I am not sure what is the issue as I am setting
In the host machine I can see the variable if I run Not really sure if it's a mapping problem between the container and the host machine or what is going on. |
Beta Was this translation helpful? Give feedback.
-
You can pass secrets into the build process It's documented here,Kamal builders Example, In deploy.yml
In your Dockerfile
But keep in mind that the environment variable is only available for that one command. After that's finished it's gone |
Beta Was this translation helpful? Give feedback.
Kamal just loads .kamal/secrets, .kamal/secrets.production, .kamal/secrets-common instead of .env. You can interpolate or pull from your actual ENV, it's up to you.
The upgrade guide is also out which goes over this https://kamal-deploy.org/docs/upgrading/overview/