Quick detour from our notifications journey to share a handy Kamal tip!
Have you ever encountered an error during deployment because of a missing environment variable, even though it’s set correctly? The issue might stem from how Kamal handles environment variables during its deployment process.
Here’s what happens when Kamal deploys:
The Problem: Environment variables are only included in Step #4. If your application relies on ENV.fetch('FOOBAR')
during the image-building process (like asset precompilation), it will fail in Step #1 because those variables aren’t available yet.
The Solution: Provide dummy values for the required environment variables during the build process. Here’s an example:
# Add environment variables needed to boot the application
RUN SECRET_KEY_BASE_DUMMY=1 \
FOOBAR=123abc \
./bin/rails assets:precompile
This ensures your application is properly configured without errors during asset precompilation, giving you a smoother deployment process.
Hope this helps! Let me know if you’ve run into other Kamal quirks.