Add a kamal pre-build hook with some safety checks

taken directly from bc3

ref: https://app.fizzy.do/5986089/cards/3154
This commit is contained in:
Mike Dalessio
2025-11-24 14:48:03 -05:00
parent e8630f5f19
commit f05923a472
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env ruby
def exit_with_error(message)
$stderr.puts message
exit 1
end
def check_branch
if ENV["KAMAL_DESTINATION"] == "production"
current_branch = `git branch --show-current`.strip
if current_branch != "main"
exit_with_error "Only the `main` branch should be deployed to production, current branch is #{current_branch}. If this is expected, try again with `SKIP_GIT_CHECKS=1` prepended to the command"
end
end
end
def check_for_uncommitted_changes
if `git status --porcelain`.strip.length != 0
exit_with_error "You have uncommitted changes, aborting"
end
end
def check_local_and_remote_heads_match
remote_head = `git ls-remote origin --tags $(git branch --show-current) | cut -f1 | head -1`.strip
local_head = `git rev-parse HEAD`.strip
if local_head != remote_head
exit_with_error "Remote HEAD #{remote_head}, differs from local HEAD #{local_head}, aborting"
end
end
unless ENV["SKIP_GIT_CHECKS"]
check_branch
check_for_uncommitted_changes
check_local_and_remote_heads_match
end