diff --git a/.kamal/hooks/pre-build b/.kamal/hooks/pre-build new file mode 100755 index 000000000..99de8a824 --- /dev/null +++ b/.kamal/hooks/pre-build @@ -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