
Today I Learned (TIL) About Kamal Gotchas
With a new Ruby on Rails version comes new gadgets, tools, and learnings. One of the newer tools that was included with a freshly created Rails 8 app was Kamal for application deployments. It's been awhile since I've deployed apps and Kamal already integrated with Rails seemed like the best approach.
Run kamal setup before first deploy
My first issue I faced was the DigitalOcean remote server and container registry I was deploying to did not include Docker and necessary packages. I SSH'd into the server setting up the services manually with Ubuntu snap packages and ran into very vague errors when trying to deploy. Running the following would have setup dependencies correctly without the headache:
kamal setup
When in doubt, restart
Within the first couple weeks of using Kamal, I have more than a handful of times encountered SSH connection errors and hanging servers, either through Kamal deploy process or directly trying to connect via command line to my server. The Kamal script hangs at various stages, servers become unresponsive, and other undesirable roadblocks have surfaced. The more I debug, the less answers I get. So for the time being, restarting my local computer and remote server have been the quickest fix to an immediate deploy. It's not the most elegant or best long-term solution, but it gets the job done for now for me focus on the end goal of coding and testing my products and ideas quickly. You might encounter Kamal release lock errors that can be cleared by:
kamal lock release
Data Migration Post-Deploy Hook
As app designs continue to evolve, data migrations start becoming more of a necessity, requiring more robust data migrations solutions. Fortunately, we have Ruby Gems to already handle that with minimum effort using data_migrate gem. Vibe Coding failed me in this area and suggested adding hooks to my `deploy.yml` that kept throwing errors during deployment like the following:
kamal deploy Finished all in 0.0 seconds ERROR (Kamal::ConfigurationError): unknown key: hooks
Digging into this error a little bit more, I found Kamal (v2.7) uses deploy hooks via script that is located in .kamal/hooks directory with samples. Since the `data_migrate` gem was installed on this most recent deploy, the data migration commands will need to be added to the post-deploy script:
#!/bin/sh # .kamal/hooks/post-deploy # This runs on your primary host only (handled by Kamal): kamal app exec --primary -i "./bin/rails db:migrate:with_data"
And update the file to be an executable:
chmod +x .kamal/hooks/post-deploy
Now my Kamal script successfully deploys and completes my data migrations:
Releasing the deploy lock... Finished all in 39.3 seconds Running the post-deploy hook... INFO [2a7a7fe1] Running /usr/bin/env .kamal/hooks/post-deploy as snobook@localhost INFO [2a7a7fe1] Finished in 8.904 seconds with exit status 0 (successful).
Huzzah! Happy deploying and migrating 🤘
Comments 0
Leave a Comment
No comments yet. Be the first to share your thoughts!