Git Hook Continuous Deployment
I picked this technique up while working on a pico-8 project, because I wanted to develop from an unsupported platform. The method of action is very simple, we use the post-receive git hook to check if it should build and to initiate the build.
the post-receive hook can receive multiple updates at a time so you will need to iterate over them. in bash that will look like:
while read oldrev newrev refname do # do stuff here done
it's nice to set it up to only build on updates to certain branches or to sort builds by branch. to that end, you can get the branch that this commit is happening on with:
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
serverside you will probably have a bare repository that you cannot build from. this
is where a worktree comes in which you can specify with the appropriate flag.
please note that the
-f flag is important when you will introduce build artifacts.
git --work-tree=$worktree checkout -f $newrev
It should be noted that this executes code from the permissions level of the git user. and may not be suitable for deployments where untrusted users have access to changing the git hook. A more secure approach would send a webhook to your build service which will fetch the cude using a given deploy key. at the time my gitea instance was old and did not have web hooks available but its a really nice tool in the right circumstance